I created the 2 files included in the above download for the company I work for. They were nice enough to grant me permission to release them on my website. These files are primarily intended for benchmarking production code which interfaces with a database or other external data store. When you have a front-end PHP or back-end Perl script that has performance problems in production, it can be difficult to track down exactly which SQL query or function block is responsible. These 2 files provide a very simple way of starting and stopping multiple timers throughout your code. These timers have millisecond precision (via the Time::HiRes CPAN module in Perl) and are logged to a tab-delimited .tsv file for eventual analysis.
By allowing the benchmarking code to run in production for several hundred or several thousand iterations, you can collect statistics on what your programs are doing in real-life situations. Once the .tsv file has collected a sufficient amount of data, you can get average run-times for each of your timers via a simple awk shell command (or by opening the .tsv file in a spreadsheet program).
Source Code for the PHP Benchmark class
<?php/** * This file demonstrates usage of the benchmark.php * Benchmarking Class in a hypothetical PHP Session * login script. */// First, include the benchmark class.// It's a singleton, so there's no need to instantiate it.require_once('/path/to/benchmark.php');// Then, if desired, you can change the filenames and file locations// of the header file and analysis file.Benchmark::$logFilename = '/tmp/customer_login_analysis.tsv';Benchmark::$headerFilename = '/tmp/customer_login_header.tsv';// Make a note in the benchmark file to record who is logging inBenchmark::injectValue($loginName, 'Customer Login Name');// Set your initial timer to time the entire login processBenchmark::timerStart('timer1', 'Login process: overall');// Next, we time our first SQL queryBenchmark::timerStart('timer2', 'SQL query 1');$sql = "SELECT name, address, email, phone FROM accounts WHERE login = '{$loginName}'";list($name, $address, $email, $phone) = custom_fetch($sql);// End the timer2 timerBenchmark::timerStop('timer2');// Next, we time an SQL insertBenchmark::timerStart('timer3', 'SQL query 2');$sql = "INSERT INTO login_history (login, timestamp) VALUES ('{$loginName}', CURRENT_TIMESTAMP)";custom_insert($sql);// End the timer3 timerBenchmark::timerStop('timer3');// End the original timer for the overall login processBenchmark::timerStop('timer1');// Finally, write all the benchmarking data to a fileBenchmark::printStats();?>
Calling Benchmark::printStats() creates 2 files:
Once you have data loaded in your analysis files, you can use the awk shell program to loop through the file and find an average for each of your timers. A sample awk command to pull averages for our sample customer_login_analysis.tsv file is provided below. awk comes pre-installed on all Linux and OS-X systems. If you're using Windows, you can try using gawk.
Sample awk command:
Output from the above command: