This presentation reviews of the many aspects of PHP performance that can impact day-to-day living. It explores basic concepts for resolution when PHP performance has got you down. The focus is on Zend Server configuration options including, but not limited to: caching, Apache settings, PHP syntax fundamentals, diagnosing bottlenecks, and DB2/SQL optimization.
Take control of your SAP testing with UiPath Test Suite
Fundamentals of performance tuning PHP on IBM i
1. Fundamentals of
performance tuning PHP
on IBM i
Mike Pavlak – Zend, a Rogue Wave company
Alan Seiden – Alan Seiden Consulting, Club Seiden
July 27, 2016
2. 2
Agenda
• PHP performance with Mike
– Basics
– Data cache
• Alan’s awesome stuff
– Apache threads
– Session locking
– Compression
– DB2 query optimization
– Persistent connections
– Unique performance tools of IBM i
• Wrap up
4. 4
Don’t panic!
• Grab your towel, pocket your babelfish and …
• PHP performance is RARELY the culprit
• Many factors impact performance
– Hardware
• Processor class
• Memory
– Operating System level
• Currently support 7.1, 7.2, & 7.3
– DB2 Data Access plans
• Triangulate
– Is the whole system busy?
– Just web workload slowing?
5. 5
Some contributors to performance
• Additive workload
• Transitional workload
• Organic growth
• Network infrastructure
• Entropy
• Get out in front of these before you need to
6. 6
PHP log file
• Every time PHP encounters an error (warning & notice)
– Open log file in IFS
– Write error
– Close log file
• Over time, this file gets BIG!
– Have seen in excess of 1.2 GB
– How long does it take IFS to open large files? (OS level)
• Log file should be “clipped” periodically
• Delete or rename the log file
7. 7
Rename PHP log file
• Zend Server will create more
– REN OBJ('/usr/local/zendsvr/var/log/php.log')
NEWOBJ(phplog20120306.txt)
9. 11
Bit twiddling 101 …
• Consider this VERY carefully, what is the most expensive resource?
• Single quotes vs. double quotes
• Echo vs. Print
• Sprint() instead of “”
• Unset variables for memory management
• Close database connection if you are done unless pconnect
• $row[‘id’] is faster than $row[id];
• Static pages (html) are OK!!!
• Not everything has to be a Framework
– Fastest to slowest:
• Construct
• Function
• Static method
• Object
• Framework
10. 12
Don’t write PHP like RPG
• RPG applications often are built with main file then chains
– SETLL then READE ORDDETAIL
• CHAIN PRODUCT
• CHAIN INVENT
• CHAIN ONPLAN
• Consider a single SQL statement
– Join files are more efficient that chains, in most cases
– SQL optimizers are genius
– Learn good indexing strategy
– Go to SQL sessions at conferences
– Ask Mike Cain, he’ll tell you!
• Great Redbooks:
– Database modernization
– RPG and Application Modernization
11. 13
Fast CGI
• Mechanism for communicating between Apache and PHP
• Built in with 10 server jobs at the time of installation
• This should conform to concurrent processes
• Watch this as more workload is migrated to web
12. 14
Fast CGI continued …
• Changing this
– is something that should be considered
– but not taken lightly (for example system resources)
• File to edit:
– /www/zendsvr/conf/fastcgi.conf
– SetEnv="PHP_FCGI_CHILDREN=10"
• But don’t forget …
– StartProcesses = “2” * PHP_FCGI_CHILDREN = 8 is 16 total worker
jobs
– youngiprofessionals.com/wiki/FastCGI
– systeminetwork.com/article/other-languages/fastcgi-boosts-php-
performance-on-ibm-i-66195
14. 16
Data access is powerful
• Green screen applications take DB2 access for granted
• Traditional application
– Heads down data entry
– Occasionally press <F4> for “point and shoot” window
– Performance hit ONLY when tapping <F4>
• Web page
– AJAX or probably a drop down list
– 20 input capable fields where half are drop down lists means
• 10 SQL executions run before user even starts on page
17. 19
Rules for using data cache
• This is a spice, not an main dish
• Types of data to cache:
– Frequently accessed, rarely changes
• List of states, warehouse codes, customer types …
– Hard to compute numbers
• YTD sales 2013, 2014, 2015 (2016 should be calculated, maybe)
• Cache expiration
– Each data element can have it’s own expiration interval
– Can expire a specific element programmatically
• Add this to maintenance application (new warehouse code)
– Can expire entire cache from admin GUI
18. 20
Data cache – pros and cons
• Pros
– Reduce load on DB2
– Improve application response time
– Great for dashboards & data entry
• Cons
– Need to track where data elements are cached
– Develop a consistent approach to use
– May use a little RAM, but manageable
– Overuse and lack of knowledge can create a negative perspective
• White paper “A practical guide to data caching with Zend Server”
– zend.com/topics/Zend-Server-Data-Caching-Whitepaper-0106-T-WP-R1-EN.pdf
21. 24
If you increase fastcgi child jobs …
• You may also need more Apache HTTP threads
• Apache’s ThreadsPerChild
– /www/zendsvr6/conf/httpd.conf
– Default: ThreadsPerChild 40
– Increase to number of expected HTTP connections
• PHP requests
• CSS
• Javascript
• AJAX requests
• Speaking of AJAX, see next slides for optimization for heavy AJAX use …
23. 26
Reduce session locking
• Sessions can keep track of user who is logged in, for example
– Cookie in browser tells PHP which session file to use
– session_start() initiates session in PHP
• Opens and locks the file for updating
– Located in /tmp by default
– Access data via $_SESSION
array
24. 27
AJAX requests burden sessions
• With AJAX, one page is > 1 request
– Multiple requests (AJAX/JSON) in one page
– If PHP sessions used (session_start), all requests from one browser use
same PHP session on back-end
Above see result of numerous PHP requests launched in one
page. Note the “waterfall” shape; each waits for the previous to
finish.
25. 28
Solution to AJAX session locking
• As soon as your application finishes writing data to the session, close it
– session_write_close() ends the lock
– You can still read $_SESSION array
• Comparison: WITHOUT session_write_close():
WITH session_write_close(): Parallel, faster! e.g. 834ms instead of 1.84ms
27. 30
More Apache config: mod_deflate
• Called gzip or mod_deflate, the same for our purposes
• Compresses, speeds up html, javascript, css, favicons, anything text-based
29. 33
My compression test
• http://your-server:10080/Samples/SQL_access/DB2_SQL_example.php
• Before compression: 31.0kb; loaded in 250ms
• After compression: 4.4kb; loaded in 109ms
• That’s 14% of the size and 50% of the time
30. 34
Details of deflate/gzip compression
• Apache directives (sample)
# Load IBM i's module that performs compression
LoadModule deflate_module
/QSYS.LIB/QHTTPSVR.LIB/QZSRCORE.SRVPGM
# Specify content types to compress
AddOutputFilterByType DEFLATE application/x-httpd-php application/json
text/css application/x-javascript application/javascript text/html
• Tutorial on my blog:
– alanseiden.com/2010/08/13/maximize-zend-server-performance-with-
apache-compression/
• Apache reference:
– httpd.apache.org/docs/2.0/mod/mod_deflate.html
32. 36
DB2 query optimization
• I’ll share a couple of favorite IBM i tools
– Index Advisor
– SQL Plan Cache
• See IBM’s book IBM i Database Performance and Query Optimization
– ibm.com/support/knowledgecenter/ssw_ibm_i_71/rzajq/rzajq.pdf
33. 37
Index Advisor
• Now in web-based Navigator as well as thick client
• Recommends indexes across all queries
34. 38
SQL Plan Cache
• See what queries are REALLY running, who’s running them, and how long
they take
36. 40
Use persistent connections
• On IBM i, job initialization takes longest
– Gives us auditing, logging, security
– Solution: a pool of pre-initialized jobs
• Pre-started DB2 jobs save time
– Generally run in subsystem QSYSWRK, job name QSQSRVR
– These prestart jobs can be configured with CHGPJE command
• In PHP, persistent connections reuse initialized jobs
– db2_pconnect()
– Dramatic speed boost
37. 41
db2_pconnect() to connect
persistently
• db2_pconnect ( string $database , string $username , string $password [,
array $options ] )
• Persistent is much faster than non-persistent
– db2_pconnect can reuse connections, reducing the time needed to
connect (after the first time) to almost zero
• More information:
– “DB2 and PHP Best Practices on IBM i” at
alanseiden.com/presentations
38. 42
DB2 server prestart job configuration
• Prestart jobs named QSQSRVR run in QSYSWRK
• Or, if remote DRDA, QRWTSRVR in QUSRWRK
• Configurable pool of jobs
CHGPJE SBSD(QSYS/QSYSWRK) PGM(QSYS/QSQSRVR)
STRJOBS(*YES) INLJOBS(xx) THRESHOLD(xx)
ADLJOBS(xx) MAXUSE(xx or *NOMAX)
• Defaults are somewhat low
– Initial jobs = 5, threshold = 2, adljobs = 2. Maxuse = 200 (*NOMAX may
be better)
• More information about QSQSRVR prestart jobs
– mcpressonline.com/tips-techniques/database/techtip-grab-control-of-
the-db2-qsqsrvr-jobs.html
40. 44
IBM i system tools
• WRKACTJOB is a good start
• Hover over CPU % and press F16 to sort by CPU
• Provides a general view of what’s running
• Look at call stacks of “misbehaving” jobs
42. 46
Performance tools
• User-friendly, web-based tools that are either free or low cost
– Performance Data Investigator is free
– Job Watcher is low cost
• Performance Tools licensed program (GO LICPGM)
– 7.x: 5770PT1
• Install latest level of these group PTFs
– 7.1: SF99368 (HTTP Server), SF99572 (Java), SF99701 (Database),
SF99145 (Performance Tools)
– 7.2: SF99713 (HTTP Server), SF99716 (Java), SF99702 (Database),
SF99145 (Performance Tools)
– 7.3: SF99722 (HTTP Server), SF99725 (Java), SF99703 (Database)
43. 47
Resources for IBM i performance
• Dawn May
– Hear Dawn speak at COMMON and other conferences
– ibmsystemsmag.blogs.com/i_can/
• Lab Services in Rochester
– Many experts there. I’ve worked with Stacy Benfield
– ibmsystemsmag.blogs.com/i_can/2010/08/i-can-benefit-from-ibm-
systems-lab-services-and-training.html
• IBM i on Power Performance FAQ
– ibm.co/1RdkDn4
Stay tuned for speaker info
44. 48
Alan Seiden
• Founder of Alan Seiden Consulting and Club Seiden
• “Performance guru of PHP on IBM i”
• Leader, Zend’s PHP Toolkit for IBM i
• Alan Seiden Consulting is a team of experts available for
mentoring/troubleshooting/project advice/development.
alanseiden.com, alan@alanseiden.com
45. 49
Zend services
• Audit – Find the holes in an application
– Performance
– Architecture
– Security
• Training – Get better
– Smart Start
– Online
• Architect Advisor – Professionals on call
• Zend Server Plus
– PHP and more!
46. ATTEND
Become a PHP authority.
Connect with experts.
Register now.
CELEBRATE
10 years of PHP on IBM i.
Join us for the party!
SPONSOR
Spotlight your best in
enterprise PHP.
sponsors@zendcon.com
Visit zendcon.com
Editor's Notes
What percentage of your mission critical software is open source?
A: 0 to 25%
B: 26 to 50%
C: 51 to 75%
D: 75%
What percentage of your mission critical software is open source?
A: 0 to 25%
B: 26 to 50%
C: 51 to 75%
D: 75%
POLL: How long does your slowest page take to load? (a) < 1 second (b) 1-2 seconds (c) over 2 but < 5 seconds (d) over 5 seconds
What percentage of your mission critical software is open source?
A: 0 to 25%
B: 26 to 50%
C: 51 to 75%
D: 75%