PHP Web Site Optimization
Jump to navigation
Jump to search
PHP Web Site Optimization Training Materials
Copyright Notice
Copyright © 2004-2023 by NobleProg Limited All rights reserved.
This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
What is Optimization? ⌘
- Reducing resource utilization
- Improving response time
- Sometimes there is a tradeoff between the two, increasing utilization to reduce response time
- In this course, general techniques for any PHP application are covered
- For some specialized applications, (e.g. WordPress) additional subject matter training is recommended
The Optimization Process ⌘
- Create a well instrumented test
- Time metrics are absolutely required
- Some resource utilization metrics are needed, others may not be
- Re-using existing monitoring metrics is a good practice, but in some cases they may not be enough
- Run the test (establish a baseline)
- Make a change
- Identify possible sources of delay
- Make *one* change (e.g. optimize a SQL query, tweak a script, add cache, add hardware)
- Repeat test and record results
Recommended Tools ⌘
- Apache JMeter - http://jmeter.apache.org/
- Load testing tool, used to create and run tests
- xdebug - https://xdebug.org/
- Debugging extension for PHP, with a built-in profiler for performance data
- Cachegrind GUI
- Works with performance data generated by xdebug
- Versions available for Linux, Windows, and MacOS
Opcode Caching ⌘
- This is built in to all versions of PHP since 5.5, by default
- Same sort of optimization Zend previously offered in their Zend Optimizer product
- Parameters for the built-in caching are manipulated using the OPcache Module
Interprocess Communication ⌘
- IPC is any means of storing variables or sending signals between processes
- Legacy forms of IPC in PHP applications:
- Shared Memory (shmop)
- Flat file
- Temp tables in databases
- Best practice forms of IPC:
- memcached
- Redis
- ZeroMQ, RabbitMQ, beanstalkd
- Gearman
- Legacy forms of IPC in PHP applications:
Parallel Processing ⌘
- The problem: PHP is synchronous in nature, it is not designed for multithreading
- Ways of doing parallel processing:
- curl_multi or sockets - OK for lower numbers of parallel operations
- popen() or exec() - requires custom code but is relatively easy
- Forking (pcntl_fork) - this is an advanced technique
- Task queueing - essential for large scale, easier to dianose problems
Database Optimization ⌘
- Know how to turn on query logging and query caching
- Database basics: keys, joins, table scans, functions
- Optimizing a known query
- Look at each table involved (MySQL: DESCRIBE tbl_name, Postgres: \d+ tbl_name)
- EXPLAIN - Available in MySQL and PostgreSQL
- Can the query use existing keys better?
- If there is a subquery, can we reduce or reuse it?
- Are we retrieving columns we don't need?
- Can we create a key to help improve the query?