|
1 | 1 | # BenchPress |
2 | | -SOON |
| 2 | + |
| 3 | +**BenchPress** is a WordPress plugin for benchmarking PHP code snippets, WordPress queries, and other critical operations. It's designed to help developers evaluate and optimize code performance by running benchmarks and capturing snapshots for comparison. |
| 4 | +* * * |
| 5 | + |
| 6 | +## Table of Contents |
| 7 | + |
| 8 | +- [Installation](#installation) |
| 9 | +- [Setup](#setup) |
| 10 | +- [Usage](#usage) |
| 11 | +- [Available Benchmarks](#available-benchmarks) |
| 12 | +- [Customizing Benchmarks](#customizing-benchmarks) |
| 13 | +- [Snapshots](#snapshots) |
| 14 | +- [Plugin Settings](#plugin-settings) |
| 15 | +- [Contributing](#contributing) |
| 16 | +- [License](#license) |
| 17 | +* * * |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +1. **Download** the plugin ZIP from the [GitHub repository](https://github.com/robertdevore/benchpress). |
| 22 | +2. **Upload** it via WordPress Admin: |
| 23 | + - Go to `Plugins` > `Add New`. |
| 24 | + - Click `Upload Plugin`, select the downloaded ZIP, and click `Install Now`. |
| 25 | +3. **Activate** the plugin via the `Plugins` page in the WordPress Admin. |
| 26 | +* * * |
| 27 | + |
| 28 | +## Setup |
| 29 | + |
| 30 | +BenchPress automatically creates a custom database table to store snapshots of benchmark results on activation. The plugin also includes settings to customize benchmark runs. To configure these: |
| 31 | + |
| 32 | +1. Go to `BenchPress > Settings` in your WordPress admin sidebar. |
| 33 | +2. Adjust loop counts, post IDs, and other options to customize how each benchmark runs. |
| 34 | + |
| 35 | +* * * |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +Once installed and configured, you can run benchmarks, view results, and save snapshots for later reference: |
| 40 | + |
| 41 | +1. **Running Benchmarks**: |
| 42 | + |
| 43 | + - Navigate to `BenchPress` in your WordPress admin menu. |
| 44 | + - Click `Refresh Tests` to run all enabled benchmarks and view results. |
| 45 | +2. **Saving Snapshots**: |
| 46 | + |
| 47 | + - On the main `BenchPress` page, click `Save Snapshot` to save a record of the current benchmark results. |
| 48 | +3. **Viewing Snapshots**: |
| 49 | + |
| 50 | + - Go to `BenchPress > Snapshots` to view all saved snapshots. Each snapshot can be viewed, downloaded, or deleted. |
| 51 | +* * * |
| 52 | + |
| 53 | +## Available Benchmarks |
| 54 | + |
| 55 | +BenchPress comes with several built-in benchmarks. Here's a quick overview: |
| 56 | + |
| 57 | +- **Switch vs. Match**: Compares the performance of PHP `switch` vs `match` statements. |
| 58 | +- **Transient vs. Direct Query**: Tests the speed of transient caching against direct database queries. |
| 59 | +- **Post Meta Access**: Compares `get_post_meta()` with `WP_Meta_Query` for retrieving post meta data. |
| 60 | +- **WP_Query by ID**: Measures query performance for retrieving single or multiple posts. |
| 61 | +- **Array Merge vs. Union**: Compares `array_merge` with the array union (`+`) operator. |
| 62 | +- **String Concatenation**: Benchmarks PHP's `.` operator vs `sprintf`. |
| 63 | +* * * |
| 64 | + |
| 65 | +## Customizing Benchmarks |
| 66 | + |
| 67 | +### Adding Custom Benchmarks |
| 68 | + |
| 69 | +BenchPress includes a `benchpress_run_all_benchmarks` filter to allow you to add custom benchmarks. Here's an example of how to add your own benchmark: |
| 70 | + ``` |
| 71 | + add_filter( 'benchpress_run_all_benchmarks', function( $benchmarks ) { |
| 72 | + // Define your custom benchmark |
| 73 | + $benchmarks[] = custom_benchmark_example(); |
| 74 | + return $benchmarks; |
| 75 | + }); |
| 76 | + |
| 77 | + // Custom benchmark function |
| 78 | + function custom_benchmark_example() { |
| 79 | + $loop_count = get_option( 'custom_benchmark_loop_count', 100000 ); |
| 80 | + $start_time = microtime( true ); |
| 81 | + |
| 82 | + for ( $i = 0; $i < $loop_count; $i++ ) { |
| 83 | + $result = $i * 2; // Example operation |
| 84 | + } |
| 85 | + |
| 86 | + $execution_time = microtime( true ) - $start_time; |
| 87 | + |
| 88 | + return [ |
| 89 | + 'name' => esc_html__( 'Custom Benchmark', 'benchpress' ), |
| 90 | + 'execution_time'=> round( $execution_time, 5 ), |
| 91 | + 'description' => sprintf( esc_html__( 'Executed a loop of %d iterations.', 'benchpress' ), $loop_count ), |
| 92 | + ]; |
| 93 | + } |
| 94 | + ``` |
| 95 | + |
| 96 | +### Removing Benchmarks |
| 97 | + |
| 98 | +If you want to remove a specific benchmark, you can use the same `benchpress_run_all_benchmarks` filter. For example, to remove the "Switch vs Match" benchmark: |
| 99 | + ``` |
| 100 | + add_filter( 'benchpress_run_all_benchmarks', function( $benchmarks ) { |
| 101 | + return array_filter( $benchmarks, function( $benchmark ) { |
| 102 | + return $benchmark['name'] !== esc_html__( 'Switch vs Match', 'benchpress' ); |
| 103 | + }); |
| 104 | + }); |
| 105 | + ``` |
| 106 | + |
| 107 | +### Accessing Benchmark Data |
| 108 | + |
| 109 | +To access saved snapshots for analysis or custom display, use the custom database table created by BenchPress, which stores each snapshot as JSON data. |
| 110 | + |
| 111 | +* * * |
| 112 | + |
| 113 | +## Snapshots |
| 114 | + |
| 115 | +Snapshots are records of benchmark results that you can refer back to later. Snapshots can be managed in the `BenchPress > Snapshots` page: |
| 116 | + |
| 117 | +- **Clear Snapshots**: Clears all stored snapshots. |
| 118 | +- **Download Snapshots**: Downloads all snapshots as a CSV file. |
| 119 | +* * * |
| 120 | + |
| 121 | +## Plugin Settings |
| 122 | + |
| 123 | +To configure BenchPress benchmarks, navigate to `BenchPress > Settings`. Options include: |
| 124 | + |
| 125 | +- **Loop Count for Benchmarks**: Set the number of iterations for each benchmark. |
| 126 | +- **Enable Benchmarks**: Select which benchmarks to run. |
| 127 | +- **WP_Query Settings**: Configure `WP_Query` options, including post types, IDs, taxonomy terms, etc. |
| 128 | +* * * |
| 129 | + |
| 130 | +## Contributing |
| 131 | + |
| 132 | +BenchPress is an open-source project, and contributions are welcome! |
| 133 | + |
| 134 | +To contribute: |
| 135 | + |
| 136 | +1. **Fork** the repository. |
| 137 | +2. **Create** a new branch for your feature or fix. |
| 138 | +3. **Submit** a pull request with a clear description of your changes. |
| 139 | +* * * |
0 commit comments