Skip to content

Commit

Permalink
add benchmark file and run it in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 23, 2025
1 parent 6e469b6 commit f03fa2d
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ jobs:
run: composer run-script test
- name: Run codestyle sniffer
run: composer run-script codestyle
- name: Run benchmark
run: php tests/benchmark.php
183 changes: 183 additions & 0 deletions tests/benchmark-mocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

/*
* phpcs:disable PSR1.Files.SideEffects
* phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

define('ABSPATH', dirname(__DIR__, 1));
define('HOUR_IN_SECONDS', 3600);

$options = [];
$hooks = [];

function is_admin()
{
return false;
}

function add_action($hook, $callback, $c = 10, $d = 1)
{
global $hooks;
$hooks[$hook] ??= [];
$hooks[$hook][] = $callback;
}

function do_action($hook, ...$args)
{
global $hooks;
$actions = $hooks[$hook] ?? [];
foreach ($actions as $a) {
$a();
}
}

function add_filter($hook, $callback, $c = 10, $d = 1)
{
add_action($hook, $callback, $c, $d);
}

function apply_filters($hook, $value, $prio = 10, $args = 2)
{
global $hooks;

$filters = $hooks[$hook] ?? [];
foreach ($filters as $cb) {
$value = $cb($value);
}

return $value;
}

function add_shortcode($a, $b)
{
}

function number_format_i18n($number, $decimals = 0)
{
return number_format($number, $decimals);
}

function register_activation_hook($file, $callback)
{
}

function register_deactivation_hook($file, $callback)
{
}

function update_option($option_name, $value, $autoload = false)
{
global $options;
$options[$option_name] = $value;
}

function get_option($option_name, $default = null)
{
global $options;
return $options[$option_name] ?? $default;
}

function get_transient($name)
{
return null;
}

function set_transient($name, $value, $ttl)
{
}

function delete_transient($name)
{
}

function get_role($role)
{
return null;
}

function get_site_url()
{
return '';
}

function site_url()
{
return '';
}

function is_multisite()
{
return false;
}

function wp_next_scheduled($event)
{
return false;
}

function wp_schedule_event($timestamp, $recurrence, $hook, $args = [])
{
}

function wp_upload_dir()
{
return [
'basedir' => '/tmp',
];
}

function wp_remote_get($url)
{
return null;
}

function wp_remote_retrieve_response_code($response)
{
return '';
}

function wp_remote_retrieve_headers($response)
{
return [];
}

function is_wp_error($thing)
{
return false;
}

function __($string, $text_domain)
{
return $string;
}

function register_post_type($name, $args)
{
}

function wp_register_script($handle, $src, $deps = [], $version = '')
{
}

function plugins_url($file, $path)
{
return $path;
}

class wpdb_mock
{
public $prefix = '';
public function query($sql)
{
return null;
}

public function prepare($query, ...$params)
{
return $query;
}
}

global $wpdb;
$wpdb = new wpdb_mock();
23 changes: 23 additions & 0 deletions tests/benchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

require __DIR__ . '/benchmark-mocks.php';

// make sure we're not running through all migrations
update_option('mc4wp_version', '999.1.1');

$memory = memory_get_usage();
$time_start = microtime(true);

require dirname(__DIR__, 1) . '/mailchimp-for-wp.php';

do_action('plugins_loaded');
do_action('setup_theme');
do_action('after_setup_theme');
do_action('init');
do_action('wp_loaded');

$time = round((microtime(true) - $time_start) * 1000, 2);
$memory_used = (memory_get_usage() - $memory) >> 10;

echo "Memory: $memory_used KB\n";
echo "Time: $time ms\n";

0 comments on commit f03fa2d

Please sign in to comment.