Skip to content

Commit 3e34f84

Browse files
run plugin benchmark over http with and without opcache
1 parent 01c4895 commit 3e34f84

File tree

5 files changed

+105
-31
lines changed

5 files changed

+105
-31
lines changed

.github/workflows/php.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
- name: Run codestyle sniffer
2020
run: composer run-script codestyle
2121
- name: Run benchmark
22-
run: php tests/benchmark.php
22+
run: php tests/benchmarks/run.php

tests/benchmark.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

tests/benchmark-mocks.php renamed to tests/benchmarks/mocks.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
66
*/
77

8-
define('ABSPATH', dirname(__DIR__, 1));
8+
define('ABSPATH', dirname(__DIR__, 2));
99
define('HOUR_IN_SECONDS', 3600);
1010

1111
$options = [];
@@ -147,24 +147,29 @@ function is_wp_error($thing)
147147
return false;
148148
}
149149

150-
function __($string, $text_domain)
150+
function __($text, $domain)
151151
{
152-
return $string;
152+
return $text;
153153
}
154154

155-
function register_post_type($name, $args)
155+
function _e($text, $domain)
156156
{
157+
echo $text;
157158
}
158159

159-
function wp_register_script($handle, $src, $deps = [], $version = '')
160+
function register_post_type()
160161
{
161162
}
162163

163-
function plugins_url($file, $path)
164+
function wp_register_script($handle, $src, $deps = [], $in_footer = true)
164165
{
165-
return $path;
166166
}
167167

168+
function plugins_url($path, $plugin = '')
169+
{
170+
}
171+
172+
168173
class wpdb_mock
169174
{
170175
public $prefix = '';

tests/benchmarks/plugin.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
// phpcs:disable PSR1.Files.SideEffects
4+
5+
// if query parameter is set, disable opcache until end of request
6+
if (isset($_GET['disable-opcache']) && (int) $_GET['disable-opcache'] === 1) {
7+
ini_set('opcache.enable', 0);
8+
ini_set('opcache.enable_cli', 0);
9+
}
10+
11+
require __DIR__ . '/mocks.php';
12+
13+
// make sure we're not running through all migrations
14+
update_option('mc4wp_version', '999.1.1');
15+
16+
$memory = memory_get_usage();
17+
$time_start = microtime(true);
18+
19+
// require main plugin file
20+
require dirname(__DIR__, 2) . '/mailchimp-for-wp.php';
21+
22+
do_action('plugins_loaded');
23+
do_action('setup_theme');
24+
do_action('after_setup_theme');
25+
do_action('init');
26+
do_action('wp_loaded');
27+
do_action('parse_request');
28+
29+
$time = (microtime(true) - $time_start) * 1000.0;
30+
$memory_used = (memory_get_usage() - $memory) >> 10;
31+
32+
header("Content-Type: application/json");
33+
echo json_encode(['memory' => $memory_used, 'time' => $time]);

tests/benchmarks/run.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
// phpcs:disable PSR1.Files.SideEffects
4+
5+
function analyze(array $values): array
6+
{
7+
$sum = 0.0;
8+
$min = PHP_FLOAT_MAX;
9+
$max = PHP_FLOAT_MIN;
10+
foreach ($values as $v) {
11+
$sum += $v;
12+
$min = min($min, $v);
13+
$max = max($max, $v);
14+
}
15+
$mean = $sum / count($values);
16+
return [round($min, 3), round($mean, 3), round($max, 3)];
17+
}
18+
19+
function bench(int $n = 100, bool $opcache = true): void
20+
{
21+
// run benchmark
22+
$memories = [];
23+
$times = [];
24+
$url = $opcache ? 'http://localhost:8080/plugin.php' : 'http://localhost:8080/plugin.php?disable-opcache=1';
25+
26+
// make a single request to test whether HTTP server is up
27+
// but also to warm the opcache
28+
$body = file_get_contents($url);
29+
if ($body === false) {
30+
throw new Exception("Error making HTTP request. Is the HTTP server running?");
31+
}
32+
33+
// run for 5 seconds
34+
$time_until = time() + 5;
35+
while (time() < $time_until) {
36+
$body = file_get_contents($url);
37+
$data = json_decode($body);
38+
$memories[] = $data->memory;
39+
$times[] = $data->time;
40+
}
41+
42+
[$time_min, $time_mean, $time_max] = analyze($times);
43+
44+
echo $opcache ? "with opcache: " : "without opcache: ";
45+
echo "min: $time_min\tmean: $time_mean\tmax: $time_max\n";
46+
}
47+
48+
$root = __DIR__;
49+
$ph = popen("php -S localhost:8080 -q -t {$root} &2>/dev/null", "r");
50+
if (!$ph) {
51+
echo "Error starting HTTP server\n";
52+
exit(1);
53+
}
54+
sleep(2);
55+
56+
bench(100, true);
57+
bench(100, false);
58+
59+
pclose($ph);

0 commit comments

Comments
 (0)