Download - WordPress performance tuning
http://lynt.cz01.05.2023 1
WordPress + performance
Vladimir [email protected]
@smitkaLynt services s.r.o.
http://lynt.cz01.05.2023 2
Hidden advertisement
• We launched a new blog about automatization in PPC few days ago
http://ppc-scripts.eu
http://lynt.cz01.05.2023 5
Speech no. 2 – DIY• What mentioned services do?– Caching of generated page on their servers– Resources optimization – combine&minify JS & CSS, image
optimization
http://lynt.cz/blog/wordpress-in-the-czech-complex-research
http://lynt.cz01.05.2023 6
Image optimization
• Appropriate dimensions• Check if thumbnails are really small
• Use appropriate formats• „Web graphics“ – 8 bit PNG, SVG• Photos – JPG (quality 75 is OK, 60 in
Photoshop)• Videos – MP4, FLV, GIF!
http://lynt.cz01.05.2023 7
Image optimization – 24 bit PNG
• Usually used because of alpha channel• They are often used unnecessarily – alpha channel is
also supported by 8 bit PNG (produced images are much smaller), Photoshop couldn‘t produce it recently (new Photoshop CC can)
• https://tinypng.com/
• TruePNG - http://css-ig.net/articles/truepng• AdvDef -http://advancemame.sourceforge.net/comp-download.html
http://lynt.cz01.05.2023 8
Image optimization - plugins
• https://wordpress.org/plugins/ewww-image-optimizer/ + https://ewww.io
• https://wordpress.org/plugins/shortpixel-image-optimiser/ - 100 images/month for free
• https://wordpress.org/plugins/kraken-image-optimizer/ - subscription
http://lynt.cz01.05.2023 9
CSS sprites & data URI• What is the problem?• HTTP headers are 0.5 - 1KB length in average (in case of small
images headers are bigger than useful data)• Connection time to the server is negligible• Our goal is to reduce number of requests to the server:
A) If you have more images you can merge them to one bigger image (http://draeton.github.io/stitches/)
B) You can load particular small images with data URI:<img src="data:image/png;base64,…data v base64…">
http://lynt.cz01.05.2023 10
JS & CSS optimization
• Goal – reduction of requests to server, reduction of size (again)
• You can combine more CSS and JS files into one + minify them
• https://wordpress.org/plugins/autoptimize/• https://wordpress.org/plugins/bwp-minify/
http://lynt.cz01.05.2023 12
Page cache
• Saves processed page into static HTML file for future load
• https://wordpress.org/plugins/wp-super-cache/
• https://wordpress.org/plugins/w3-total-cache/
http://lynt.cz01.05.2023 15
Speech no. 3 – the real one
Problem diagnostics:https://gtmetrix.com/
Application Resources
http://lynt.cz01.05.2023 16
http://lynt.cz01.05.2023 17
Resources loading under hood
• http://www.webpagetest.org/
http://lynt.cz01.05.2023 18
Resources loading – common problems
• Expires headers (mod_expires) – ensure that resources don‘t need to be downloaded again (else browser will use heuristic analysis)
• GZIP compression (mod_deflate) – usually saves about 30-70% size of text files
• Keep Alive – keeps connections open, there is no need to establish connection again, it uses little more RAM
http://lynt.cz01.05.2023 19
Keep Alive
30ms
Keep Alive turning off may be critical on sites with many resources - especially on mobile devices with long latency
Ping 300ms, 150 resources, 5 simultaneous connections:2x300x150/5 = 18s waiting for connection + ending
Why so many redirects?
http://lynt.cz01.05.2023 20
HTTPS & SPDY & HTTP/2
HTTP/2• encrypt,• compress,• keep alive,• binary protocol
Neither combining CSS and JS, nor using sprites is necessary. Using these techniques may little slow down your site with HTTP/2.
Reconnecting isn‘t a problem – only required resources are downloaded.
http://lynt.cz01.05.2023 21
What slows TTFB down
• MySQL queries• Unusage of cache• HTTP requests (e.g. update checks)
• More plugins = more requests
http://lynt.cz01.05.2023 22
Effect of plugins• Test on VPS from WEDOS (1 core, 4G RAM)
ab -n 1000 -c 4 http://domain
• Clean WP:Requests per second: 15.93 [#/sec]Time per request: 251.095 [ms]
• WP + SliderRevolution + CF7 + Yoast SEO Requests per second: 4.61 [#/sec]Time per request: 868.450 [ms]
• WP + SliderRevolution + CF7 + Yoast SEO + WPML + JetpackRequests per second: 2.94 [#/sec] Time per request: 1360.454 [ms]
Congrats, we were able to degrade
performance more than 5 times in few
minutes!
http://lynt.cz01.05.2023 23
Motivation: our webserver
• WP + some common pluginsRequests per second: 319.26 [#/sec]Time per request: 12.529 [ms]Ping 2,5 ms
35 ms
Dirty caching tricks are used, of course. We will talk about it later.
http://lynt.cz01.05.2023 24
P3 Profiler
• https://wordpress.org/plugins/p3-profiler/• The simplest, provides less information, bad accuracy• It can help detect a problematic plugin
http://lynt.cz01.05.2023 25
Query monitor
• https://wordpress.org/plugins/query-monitor/• Detailed overview what happens
in WP• Great helper during development
(e.g. shows active conditions)
http://lynt.cz01.05.2023 26
Query Monitor – DB queries
List of all queries
Queries by function
Queries by component
http://lynt.cz01.05.2023 27
Query Monitor – more info
HTTP requests based on wp_remote_X functioncurl, file_get_contents etc. are not captured [you can capture them by traffic analysis (tcpdump, wireshark)]
Some important constants are missinge.g. SAVEQUERIES – another IO req.
Writes into file:IO operations
Minified libraries won‘t be used:
more data
HTTP requests
http://lynt.cz01.05.2023 28
Looking from the other side
• We have examined website from application look
• Now it is time to look where apps are running
http://lynt.cz01.05.2023 29
Server infrastructure3 parts – HTTP server, DB server a StorageEach with different requirements…
DB
Storage
WEBLOADBALANCER
Cloud
× 2
http://lynt.cz01.05.2023 30
Monitoring – looking for bottle neckhtop
Munin
http://munin-monitoring.org/http://www.zabbix.com/http://www.librenms.org/
htopiotopiftopnmon
http://lynt.cz01.05.2023 31
PHP versions
• OpCode cache• Object cache
Newer PHP versions are faster and can use OpCache more effectively (it costs
more RAM)
PHP version performance – Apache Benchmark/WP
http://lynt.cz01.05.2023 32
OpCode cache
• PHP language is compiled to Bytecode during every request
• OpCache saves result for future use, there is no need of recompilation
• You need install it (PHP < 5.5)
• APC• Xcache• Zend OpCache (part of PHP 5.5)
http://lynt.cz01.05.2023 33
Object Cache• Usually part of OpCache module + standalone• User can save data into it for future use• You need to enable it and allocate storage• You need to install object backend drop-in into WP
– APC– Xcache– APCu (for usage with Zend OpCache)– Redis– Memcached
• WP will use it for transient variables etc. – it can reduce DB queries rapidly
71 of them is Slider Revolution
http://lynt.cz01.05.2023 34
Some points from Query Monitor
• You'll probably find that menu and some widgets don‘t use cache
• http://afterburner.voceplatforms.com/back-end.html#voce-widget-cache
• http://afterburner.voceplatforms.com/back-end.html#voce-cached-nav
• There are many more tips and tools on this website
http://lynt.cz01.05.2023 35
HTTP server
• Many options to run PHP• Apache (prefork) + mod_php• Apache (mpm event/worker) + PHP-FPM, FastCGI• Nginx + PHP-FPM
Apache Apache - .htaccess
Nginx0
2
4
6
8
10
12
Apache vs Nginx
req/
s
Apache Apache - .htaccess
Nginx Nginx + Microcache
0
50
100
150
200
250
300
Apache vs Nginx + Microcache (unfair comparism)
req/
s
AllowOverride None
http://lynt.cz01.05.2023 36
/wp-content/uploads/revslider/classicslider/bike.jpg
Hey, is .htacces
s here?
And here?Here?Or
here?
Here it is!
Apache is not convenient to server static files
http://lynt.cz01.05.2023 37
Back to our webserver• We used microcache to achieve excellent results – Nginx will
remember result of request for a while (seconds, minutes)• This technic is useful in case of peak traffic – e.g after new
content release
• Microcache: Requests per second: 319.26 [#/sec] • WP Supercache: Requests per second: 270.84 [#/sec] • No caching *: Requests per second: 13.52 [#/sec]
* Test was performed with hundreds other requests on production environment
http://lynt.cz01.05.2023 38
The last benchmark
• Test to the WEDOS VPS with more concurrency ab -n 1000 -c 40 http://domain
• Requests per second: 1191.49 [#/sec] (mean)• Time per request: 33.572 [ms] (mean)• Time per request: 0.839 [ms] (mean, across all
concurrent requests)Important note:„Using cache isn‘t so easy, you need to resolver cache invalidation!“
Very good result with cheap VPS
http://lynt.cz01.05.2023 40
Blackfire.io
• Performance profiling, great visualization• Requires to install agent and PHP extension - tutorial• You can run profiling from Chrome with Blackfire
Companion
• Hack version for free• Premium version 82,5€/month
(DB queries and HTTP requestsanalysis, teams,longer data retention)
http://lynt.cz01.05.2023 42
Localization takes more than 30%?
• WP uses slow PHP implemetation of GetText
• Solution:• https://
github.com/LyntServices/WP-lang-cache (my very old hack to WP core, I have more modifications to use ObjectCache)
• https://wordpress.org/plugins/mo-cache/
http://lynt.cz01.05.2023 44
Why is do_action so slow?
From PHP5.6 you can use optimized Argument Unpacking functionality:…$the_['acceped_args']
Call_user_func_array function itself isn‘t problem – problem is hundreds and thousands calls of this function (or any other function).
http://lynt.cz01.05.2023 45
Big problem - demo
Recursion – shortcodes in shortcodes in
shorcodes …
Huh, so many calls!
Big grey bar – use of object cache makes it worse – it
will call many userializations
http://lynt.cz01.05.2023 47
Another problem – with fix!
Fix:extension=json.so
Native JSON extension was missing!9.2s => 0.0248s = 370x better performance with one line
http://lynt.cz01.05.2023 48
Dead end of profiling
• When you profile WP in Blackfire you usually stop in two cases:
1) do_action – common and heavy used WP function (like apply_filters), you cannot track cause and effect of particular call in Blackfire
2) Grey bar – performance is lost inside function – code isn‘t visible
• Now debugging is come
http://lynt.cz01.05.2023 49
Xdebug
• PHP extension: http://xdebug.org/• Wizard helps you to download the correct
versions and what to insert into PHP config according your phpinfo() output: http://xdebug.org/wizard.php
• Great config setting explanation: https://gist.github.com/IngmarBoddington/5311858 xdebug.remote_enable = 1
xdebug.profiler_enable_trigger = 1xdebug.trace_format = 1xdebug.trace_enable_trigger = 1
http://lynt.cz01.05.2023 50
Xdebug – what to do?• Allows to go through code step by step from IDE• Trace-log generation• Code coverage analysis• Profiling (more overhead than Blackfire)
xdebug.profiler_enable_trigger = 1?XDEBUG_PROFILE
• You can use WebGrind for visualization: https://github.com/jokkedk/webgrind
• Xdebug Helper – Chrome extension: https://chrome.google.com/webstore/detail/xdebug-helper/eadndfjplgieldjbigjakmdgkmoaaaoc
http://lynt.cz01.05.2023 52
Trace• Tracelog – huge amount of data• Useful for resolving memory problems• https://github.com/corretge/xdebug-trace-gui
linespent time
Difference of consumed memory
Memory consuption during script execution
http://lynt.cz01.05.2023 53
Tracing part of codexdebug_start_trace('for.xt');
for ($i=0;$i<3;$i++){ echo rand(0,$i);}
xdebug_stop_trace();
TRACE START [2015-10-01 11:49:49]2 2 1 0.005554 2230882 3 0 0.005621 223128 rand 0 D:\htdocs\wp4\info.php 72 3 1 0.005740 2231282 4 0 0.005770 223128 rand 0 D:\htdocs\wp4\info.php 72 4 1 0.005879 2231282 5 0 0.005908 223128 rand 0 D:\htdocs\wp4\info.php 72 5 1 0.006016 2231282 6 0 0.006044 223096 xdebug_stop_trace 0 D:\htdocs\wp4\info.php 9 0.006130 223120TRACE END [2015-10-01 11:49:49]
http://lynt.cz01.05.2023 55
Père Fouras: Pensée du jour
„This is the end of web developers era, era of system adminstrators has begun.“
http://lynt.cz01.05.2023 56
Summary – performance analysis• How it shows from outside?
– https://gtmetrix.com/– http://www.webpagetest.org/– Developer console in browser
• What slows WP down?– P3 profiler – tentative detection of slow plugins– Query monitor – particular DB queries and more
• Profiling and debugging– Blackfire.io– Xdebug (visialization with webgrind)
http://lynt.cz01.05.2023 57
This is the end, folks.
Small gift: Blackfire.io Premium for a month:
DODWEDOS102015
valid until 19.10.2015
You can use this coupon during monthly subscription checkout
You can follow me on twitter @smitka and visit our blog http://lynt.cz/blog