Skip to content

Commit f03fa2d

Browse files
add benchmark file and run it in CI
1 parent 6e469b6 commit f03fa2d

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ jobs:
1818
run: composer run-script test
1919
- name: Run codestyle sniffer
2020
run: composer run-script codestyle
21+
- name: Run benchmark
22+
run: php tests/benchmark.php

tests/benchmark-mocks.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
3+
/*
4+
* phpcs:disable PSR1.Files.SideEffects
5+
* phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
6+
*/
7+
8+
define('ABSPATH', dirname(__DIR__, 1));
9+
define('HOUR_IN_SECONDS', 3600);
10+
11+
$options = [];
12+
$hooks = [];
13+
14+
function is_admin()
15+
{
16+
return false;
17+
}
18+
19+
function add_action($hook, $callback, $c = 10, $d = 1)
20+
{
21+
global $hooks;
22+
$hooks[$hook] ??= [];
23+
$hooks[$hook][] = $callback;
24+
}
25+
26+
function do_action($hook, ...$args)
27+
{
28+
global $hooks;
29+
$actions = $hooks[$hook] ?? [];
30+
foreach ($actions as $a) {
31+
$a();
32+
}
33+
}
34+
35+
function add_filter($hook, $callback, $c = 10, $d = 1)
36+
{
37+
add_action($hook, $callback, $c, $d);
38+
}
39+
40+
function apply_filters($hook, $value, $prio = 10, $args = 2)
41+
{
42+
global $hooks;
43+
44+
$filters = $hooks[$hook] ?? [];
45+
foreach ($filters as $cb) {
46+
$value = $cb($value);
47+
}
48+
49+
return $value;
50+
}
51+
52+
function add_shortcode($a, $b)
53+
{
54+
}
55+
56+
function number_format_i18n($number, $decimals = 0)
57+
{
58+
return number_format($number, $decimals);
59+
}
60+
61+
function register_activation_hook($file, $callback)
62+
{
63+
}
64+
65+
function register_deactivation_hook($file, $callback)
66+
{
67+
}
68+
69+
function update_option($option_name, $value, $autoload = false)
70+
{
71+
global $options;
72+
$options[$option_name] = $value;
73+
}
74+
75+
function get_option($option_name, $default = null)
76+
{
77+
global $options;
78+
return $options[$option_name] ?? $default;
79+
}
80+
81+
function get_transient($name)
82+
{
83+
return null;
84+
}
85+
86+
function set_transient($name, $value, $ttl)
87+
{
88+
}
89+
90+
function delete_transient($name)
91+
{
92+
}
93+
94+
function get_role($role)
95+
{
96+
return null;
97+
}
98+
99+
function get_site_url()
100+
{
101+
return '';
102+
}
103+
104+
function site_url()
105+
{
106+
return '';
107+
}
108+
109+
function is_multisite()
110+
{
111+
return false;
112+
}
113+
114+
function wp_next_scheduled($event)
115+
{
116+
return false;
117+
}
118+
119+
function wp_schedule_event($timestamp, $recurrence, $hook, $args = [])
120+
{
121+
}
122+
123+
function wp_upload_dir()
124+
{
125+
return [
126+
'basedir' => '/tmp',
127+
];
128+
}
129+
130+
function wp_remote_get($url)
131+
{
132+
return null;
133+
}
134+
135+
function wp_remote_retrieve_response_code($response)
136+
{
137+
return '';
138+
}
139+
140+
function wp_remote_retrieve_headers($response)
141+
{
142+
return [];
143+
}
144+
145+
function is_wp_error($thing)
146+
{
147+
return false;
148+
}
149+
150+
function __($string, $text_domain)
151+
{
152+
return $string;
153+
}
154+
155+
function register_post_type($name, $args)
156+
{
157+
}
158+
159+
function wp_register_script($handle, $src, $deps = [], $version = '')
160+
{
161+
}
162+
163+
function plugins_url($file, $path)
164+
{
165+
return $path;
166+
}
167+
168+
class wpdb_mock
169+
{
170+
public $prefix = '';
171+
public function query($sql)
172+
{
173+
return null;
174+
}
175+
176+
public function prepare($query, ...$params)
177+
{
178+
return $query;
179+
}
180+
}
181+
182+
global $wpdb;
183+
$wpdb = new wpdb_mock();

tests/benchmark.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
require __DIR__ . '/benchmark-mocks.php';
4+
5+
// make sure we're not running through all migrations
6+
update_option('mc4wp_version', '999.1.1');
7+
8+
$memory = memory_get_usage();
9+
$time_start = microtime(true);
10+
11+
require dirname(__DIR__, 1) . '/mailchimp-for-wp.php';
12+
13+
do_action('plugins_loaded');
14+
do_action('setup_theme');
15+
do_action('after_setup_theme');
16+
do_action('init');
17+
do_action('wp_loaded');
18+
19+
$time = round((microtime(true) - $time_start) * 1000, 2);
20+
$memory_used = (memory_get_usage() - $memory) >> 10;
21+
22+
echo "Memory: $memory_used KB\n";
23+
echo "Time: $time ms\n";

0 commit comments

Comments
 (0)