web performance first aid - lisug
TRANSCRIPT
alanseiden.comAlan Seiden Consulting
Web Performance First Aid
Web Performance First Aid Alan Seiden Consulting
My focus
Advancing PHP on IBM i
• PHP project leader, Zend/IBM Toolkit
• Contributor, Zend Framework DB2/i enhancements
• Developer, Best Web Solution, IBM/Common
• Authority, subsecond web performance on IBM i/iSeries
2
Web Performance First Aid Alan Seiden Consulting
Contact information
Alan Seiden
201-447-2437
alanseiden.comtwitter: @alanseiden
3
Web Performance First Aid Alan Seiden Consulting
Where to download these slides
From my sitehttp://alanseiden.com/presentations
On SlideSharehttp://slideshare.net/aseiden
The latest version will be available on both sites
4
Web Performance First Aid Alan Seiden Consulting
What we’ll discuss today
• Why web performance matters• Performance big picture• Tools that show issues visually • Tips and configurations• Several PHP speed tips• Q&A
5
Web Performance First Aid Alan Seiden Consulting
Why performance matters
6
Web Performance First Aid Alan Seiden Consulting
Speed = money
• Google’s experiment
Google Search lists 10 results per page
They tested pages of 30 results• Traffic and revenue dropped by 20%• Why?
• 10 results took .4 seconds to generate• 30 results took .9 seconds
• A ½ second delay caused a 20% drop in traffic Source: http://glinden.blogspot.com/2006/11/
marissa-mayer-at-web-20.html
7
Web Performance First Aid Alan Seiden Consulting
Speed = competitiveness
• Search engine ranking Since early 2010, Google’s search algorithm has included speed Faster sites rank higher, all else being equal
• Consumers lose patience with slow sites With many choices, users give up on sites that drag Patience has decreased since the 1990s
• Slower sites often waste bandwidth and CPU Costly infrastructure driven by performance mistakes
8
Web Performance First Aid Alan Seiden Consulting
User experience affected by speed
• If slow enough, site appears “broken” Site may time out with error message User not sure if site is up
• Too long a wait: user loses train of thought
• Speed inspires confidence
9
Web Performance First Aid Alan Seiden Consulting
This talk can get you started
• Based on my own consulting experience
• I’ve been brought in when poor web performance is about to terminate a project Important: a calm, objective, confident attitude
• Restoring confidence is enjoyable Watch the response times steadily improve Most dramatic: home page went from 101 seconds to subsecond
• Additional motivation for me: proving that the client made a good decision to run site on IBM i
10
Web Performance First Aid Alan Seiden Consulting
Understanding HTTP (web) flow
11
Web Performance First Aid Alan Seiden Consulting
Request-response protocol
• Client (browser) requests a file; server responds• One file at a time (at most 2–6 in parallel)• Browser requests HTML file, then as it parses HTML,
finds other file names to request (images, css, js...)
12
Web Performance First Aid Alan Seiden Consulting
Each request passes through several layers
13
Web Performance First Aid Alan Seiden Consulting
You might guess one top strategy
Reduce the number and size of HTTP requests
Each HTTP request travels through several layers
A common-sense performance strategy suggests itself
14
Web Performance First Aid Alan Seiden Consulting
Tools
15
Web Performance First Aid Alan Seiden Consulting
Performance tools help in many ways
• Visualize HTTP requests• Find ways to eliminate requests or
shrink responses• Test more easily• Capture “before and after” results
For your own documentation For a report to management
16
Web Performance First Aid Alan Seiden Consulting
Favorite performance browser tools
• Web Developer Toolbar http://chrispederick.com/work/web-developer/
• Live HTTP Headers http://livehttpheaders.mozdev.org/
• Firebug https://addons.mozilla.org/firefox/addon/firebug/
• FirePHP http://firephp.org
• Page Speed from Google http://code.google.com/speed/page-speed/ http://code.google.com/speed/page-speed/download.html
17
Web Performance First Aid Alan Seiden Consulting
Web Developer Toolbar
• Web Developer Toolbar http://chrispederick.com/work/web-developer/
• Favorite peformance testing trick: Disable cache—to test “first time in” performance
18
Web Performance First Aid Alan Seiden Consulting
Live HTTP Headers
• Live HTTP Headers http://livehttpheaders.mozdev.org/ Shows the “flow” of HTTP request/response Example: Capture the URL launched before redirect
19
Web Performance First Aid Alan Seiden Consulting
Firebug
• Firebug https://addons.mozilla.org/firefox/addon/firebug/ Along with FirePHP (PHP only) and Page Speed, empowers
anyone for performance
20
Web Performance First Aid Alan Seiden Consulting
Firebug “Net” tab example
21
Web Performance First Aid Alan Seiden Consulting
Page Speed
• Page Speed from Google http://code.google.com/speed/page-speed/ http://code.google.com/speed/page-speed/download.html Analyzes your site, suggests performance techniques
22
Web Performance First Aid Alan Seiden Consulting
Use compression
23
Web Performance First Aid Alan Seiden Consulting
Compression reduces file size
• Called gzip or mod_deflate, the same for our purposes
• Compresses, speeds up html, javascript, css, favicons, anything text-based
24
Web Performance First Aid Alan Seiden Consulting
Netflix saw improvement with gzip/deflate
• Saw 13-25% performance improvement• Cut outbound traffic in half
That saves money for a busy site such as Netflix
• Details: http://www.slideshare.net/billwscott/improving-netflix-
performance-experience
• It really works!
25
Web Performance First Aid Alan Seiden Consulting
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 compressAddOutputFilterByType DEFLATE application/x-httpd-php application/
json text/css application/x-javascript application/javascript text/html
• Tutorial on my blog: http://www.alanseiden.com/2010/08/13/maximize-zend-server-
performance-with-apache-compression/• Apache reference:
http://httpd.apache.org/docs/2.0/mod/mod_deflate.html
26
Web Performance First Aid Alan Seiden Consulting
Reduce HTTP requests
27
Web Performance First Aid Alan Seiden Consulting
HTTP requests are costly
• Each request makes a round trip to server and back
• Each HTTP request consumes bandwidth and CPU
• In-network tests do not measure end-user performance outside the network Users could have unpredictable DSL or mobile connections Firewalls and proxy servers may sit between the web server and
end user• I’ve seen some convoluted network configurations
28
Web Performance First Aid Alan Seiden Consulting
Requests cause “blocking” in browser
• Browsers typically limit themselves to 2 parallel requests to a given server
• File requests stack up, blocked by prev. requests•
• Above, even “not modified” files caused blocking• Solution: reduce number of images or improve
caching via “Expires” headers http://httpd.apache.org/docs/2.0/mod/mod_expires.html
29
Web Performance First Aid Alan Seiden Consulting
More ways to reduce “blocking”
• If many .js or .css files are used: Combine them into fewer files Move contents of smaller .js or .css files inline to your pages,
eliminating those external files Page Speed tool will help you decide
30
Web Performance First Aid Alan Seiden Consulting
Create a favicon for your site
• Browsers always look for a file called favicon.ico in your document root
• Those little icons that appear in the browser
• Once found, will be “remembered” by browser• If not found, will be requested every time• How to create a favicon:
http://www.alanseiden.com/2007/05/25/brand-your-site-with-a-favicon/
31
Web Performance First Aid Alan Seiden Consulting
Keep-alive
32
Web Performance First Aid Alan Seiden Consulting
Keep HTTP connections alive
• “Keep-alive” setting in Apache (or any web server)• The HTTP job will stay open, waiting for you
Good when downloading many images, css, js files
• Configurable by number of seconds to keep connection open, number of files to be downloaded KeepAlive On KeepAliveTimeout 15 (Details: http://httpd.apache.org/docs/2.0/mod/
core.html#keepalive)
• Don’t overdo it—you are locking out other users from that HTTP job while it’s dedicated to you
33
Web Performance First Aid Alan Seiden Consulting
Connecting takes time
• Clues that Keepalive is off “Connection: close”, “Connecting”
• Example bottom right: 3.6 seconds “Connecting” (longer than average but it really happened)
34
Web Performance First Aid Alan Seiden Consulting
What you see when Keep-alive is on
• Firebug’s “Net” tab shows “Connection: Keep-Alive”, and, here, timeout=300 seconds (5 minutes)
• Zero seconds to connect• Keep-alive is working!
35
Web Performance First Aid Alan Seiden Consulting
Ajax: friend or foe?
36
Web Performance First Aid Alan Seiden Consulting
AJAX=Asynchronous Javascript And XML
• AJAX updates parts of a page without reloading the whole page
• Not always XML. These days, JSON too• Requests and file sizes are generally small• Meant to bring SPEED to the web
• Potential problems if overused
37
Web Performance First Aid Alan Seiden Consulting
AJAX mistake #1
• Too much of a good thing Requiring several AJAX requests to complete before the page
itself can load fully Too many HTTP requests at once I’ve seen a situation where 4 AJAX requests were embedded in
a page load• The AJAX doesn’t even start till the page loads• Causes “blocking” as the requests pile up, waiting for the previous
ones to complete
Solution: when page first loads, embed AJAX content in the page• Re-use logic on the server side when building page• Subsequent updates can be done with AJAX
38
Web Performance First Aid Alan Seiden Consulting
AJAX mistake #2
• Duplicate requests Might go unnoticed with javascript library tools (Dojo, jQuery...) Happens more than you would expect! Common, actually
39
Web Performance First Aid Alan Seiden Consulting
AJAX mistake #3
• Dynamically generating static content (don’t do that)• Solutions:
Change to static files Cache URLs (e.g. with Zend Data Cache if using PHP, or Apache
caching) See example below, before and after caching• (Apologies for blurring: protecting confidentiality)
40
Web Performance First Aid Alan Seiden Consulting
Persistent db connections
41
Web Performance First Aid Alan Seiden Consulting
Persistence = connection pool
• On IBM i, job initialization is relatively slow Gives us good things—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
42
Web Performance First Aid Alan Seiden Consulting
Use db2_pconnect()
•resource 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
• How db2_pconnect() reuses connections Connections defined by database, username, and password Tries to reuse an existing connection matching these 3 params May create new connection, anyway, if heavy system load db2_close() on a persistent connection does nothing db2_pclose() forces the conn to close
43
Web Performance First Aid Alan Seiden Consulting
db2_pconnect() example with library list
$database = 'MYDB';
$user = 'MYUSER';$password = 'MYPASS';
// set library list (works the same for connect or pconnect)
$options = array('i5_naming' => DB2_I5_NAMING_ON, 'i5_libl' => 'MYLIB1 MYLIB2'
);$conn = db2_pconnect($database, $user, $password, $options);
if ($conn) {
echo "Connection succeeded.";} else { echo "Connection failed.";
}// MYTABLE will be found, if in library MYLIB1 or MYLIB2
$stmt=db2_exec($conn,"SELECT * FROM MYTABLE");
44
Web Performance First Aid Alan Seiden Consulting
Rules for using persistence
• Because connections are shared when defined with an identical database, user, and password, please: Avoid unpredictable results by also specifying the same $options
for these connections Promote sharing of jobs by minimizing the number of user
profiles that you connect with• Each user profile creates a new set of database jobs• Each set of jobs consumes system resources
• More information: “DB2 and PHP Best Practices on IBM i” at http://alanseiden.com/
presentations
45
Web Performance First Aid Alan Seiden Consulting
Reduce IFS reads/writes
46
Web Performance First Aid Alan Seiden Consulting
IFS is a weak performer on IBM i
• IBM i reverses the normal dynamic Database (DB2) is fast; stream file access (IFS) is slow
• “Slow” is a relative term. Depends on system and how many files accessed Not as important as reducing HTTP access or other network
activity, for example
• Here are several tips...
47
Web Performance First Aid Alan Seiden Consulting
Tips to reduce IFS accesses
• Turn off or reduce logging to files in production Large log files can be a culprit on some systems
• Make files cacheable for long periods of time by setting “expires” headers http://httpd.apache.org/docs/2.0/mod/mod_expires.html
• If using a scripting language such as PHP, use an opcode cache (see Optimizer+ discussion later in presentation)
• In the UNIX world, stream file access is called “stat”. Any tips for reducing “stats” will be effective on IBM i Example: in Apache, turn off checking for .htaccess files if you’re not using them,
with “AllowOverride none”• # disable htaccess checks<Directory /> AllowOverride none </Directory>
48
Web Performance First Aid Alan Seiden Consulting
System specs,work management
49
Web Performance First Aid Alan Seiden Consulting
System specs
• Memory and CPU speed will make a difference
• Power 7 machines help
• IBM i v7.1 speeds up complex DB2 queries
• If you think your machine may be underpowered, ask a professional to evaluate (see next slide)
• I’ve seen some underpowered machines, sometimes not enough memory for their interactive workload, even before adding a web workload
50
Web Performance First Aid Alan Seiden Consulting
Work management
• Memory pools, routing
• A presentation by Jim Oberholtzer (atallc.net),“PHP and the Systems Administrator,” addresses work management Jim suggests adding a second memory pool for subsystems
QHTTPSVR and ZENDSVR See his presentation for details
51
Web Performance First Aid Alan Seiden Consulting
PHP-specific speed tips
52
Web Performance First Aid Alan Seiden Consulting
Zend Server for IBM i
• Download http://www.zend.com/products/server/downloads Click on the “IBM i” tab
• Two versions Zend Server Community Edition (CE): no charge regular Zend Server (extra features) has a charge The download is the same; license is different More about this on next slide
53
Web Performance First Aid Alan Seiden Consulting
Ensure you have a Zend Server license
• Full trial license comes with download• After license expires, you can still use basic “CE”
features
• But without a valid license, you experience a performance penalty Zend Server looks for a license that isn’t there
• See next slide for solution...
54
Web Performance First Aid Alan Seiden Consulting
Get a license: either premium or CE
• Even the no-charge license will protect you from performance degradation
• One customer told me adding a license gave his site “a big speed boost”
• Request CE license here: http://www.zend.com/en/products/server/ibm/license You’ll need your IBM i serial number (DSPSYSVAL QSRLNBR)
• Enter the license in Zend Server license page
55
Web Performance First Aid Alan Seiden Consulting
Make the most of Optimizer+
• Optimizer+: “opcode cache” that speeds PHP execution The first time a PHP script runs, Optimizer+ stores the compiled version
in memory, speeding future runs
• Works well with its default settings
• What’s configurable? Amount of memory to allocate to Optimizer+ Number of scripts to keep in memory Timestamp-checking interval
• How often to check if a script has been changed
• Next slide: details on the timestamp-checking interval
56
Web Performance First Aid Alan Seiden Consulting
Timestamp checking by Optimizer+
• Timestamp validation frequency: how often Optimizer+ will check if a script has been changed Ensures you are not running stale code Default: 2 seconds
• The default (2 secs) is usually OK• For max speed, disable timestamp validation
Use in production only Usually only a small improvement But I’ve seen BIG improvement on systems with slow IFS access Caution if you turn off timestamp validation:
• Changes to your app NOT applied till you clear optimizer cache• Good reason to keep timestamp validation on in development env.
57
Web Performance First Aid Alan Seiden Consulting
How to turn off timestamp validation
• From Zend Server GUI Server Setup->Optimizer+ Directives Zend_optimizerplus.validate_timestamps = “Off”; Restart PHP
58
Web Performance First Aid Alan Seiden Consulting
Turn off unused PHP extensions
• Extensions provide extra functionality to PHP Each one uses memory
• Many are enabled by default Turn off those that you do not use Do not turn off extensions needed by Zend Server’s GUI
• See my blog for exact instructions http://www.alanseiden.com/2011/08/08/php-performance-tip-
disable-unused-extensions-with-zend-server/
59
Web Performance First Aid Alan Seiden Consulting
Premium features that can help
• Zend Monitor and Code Trace identify bottlenecks Particularly helpful when app
is built on a framework
• Zend Job Queue offloads long-running processes to run asynchronously Example: sending an email message or crunching large datasets Details:
• “PHP Batch Jobs on IBM i” from http://alanseiden.com/presentations
60
Web Performance First Aid Alan Seiden Consulting
New open source PHP IBM i toolkit
• Ships with Zend Server 5.6+ Look for PTFs and hotfixes on download page Many improvements since first release
• Young i Professionals site http://youngiprofessionals.com/wiki/XMLSERVICE
• Zend Forum http://forums.zend.com/viewtopic.php?f=113&t=41648 “PHP on IBM i”->”Zend Server for IBM i”->”New Toolkit” Many tips there
• How to share DB2 connection with toolkit: http://forums.zend.com/viewtopic.php?f=113&t=46688 Persistent connection is the fastest
61
Web Performance First Aid Alan Seiden Consulting
Resources, Q&A
62
Web Performance First Aid Alan Seiden Consulting
Resources
• My webinar w/ Mike Pavlak, “Performance Tuning PHP on IBM i”• http://www.zend.com/en/resources/webinars/i5-os
• Yahoo!’s rules for good performance• http://developer.yahoo.com/performance/rules.html
• Performance Calendar• http://calendar.perfplanet.com/
• Meetup groups: live and remote• http://web-performance.meetup.com/
• Steve Souders (formerly Yahoo!, now Google)• http://stevesouders.com• @souders• Books: High Performance Web Sites, Even Faster Web Sites
63
Web Performance First Aid Alan Seiden Consulting
Q&A
• Share your experiences
• What performance situations have you faced?
• Are you handling any web performance problems now?
64
Web Performance First Aid Alan Seiden Consulting
Alan’s upcoming appearances
http://alanseiden.com/presentations/upcoming
Oct. 17 Webcast iPro Developer Online ConferenceOct. 22-25 ZendCon Santa Clara, Calif.Dec. 11 Webcast Zend: ZF2 for IBM i
65
Web Performance First Aid Alan Seiden Consulting
Contact | Get tips
Free tips newsletter: alanseiden.com/tips
Alan Seiden Alan Seiden ConsultingHo-Ho-Kus, NJ
alanseiden.com
66
[email protected] ● 201-447-2437 ● twitter: @alanseiden