| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Tag Groups |
| 5 |
* @author Christoph Amthor |
| 6 |
* @copyright 2018 Christoph Amthor (@ Chatty Mango, chattymango.com) |
| 7 |
* @license GPL-3.0+ |
| 8 |
*/ |
| 9 |
|
| 10 |
// phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace, Squiz.Classes.ValidClassName.NotCamelCaps, PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
| 11 |
if (!class_exists('TagGroups_Settings_Ajax')) { |
| 12 |
/** |
| 13 |
* |
| 14 |
*/ |
| 15 |
class TagGroups_Settings_Ajax |
| 16 |
{ |
| 17 |
/** |
| 18 |
* Runs selected routines for benchmarking |
| 19 |
* |
| 20 |
* @param void |
| 21 |
* @return string HTML |
| 22 |
* @since 1.23.0 |
| 23 |
*/ |
| 24 |
public static function ajax_benchmark() |
| 25 |
{ |
| 26 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- AJAX handler |
| 27 |
if (!isset($_POST['task'])) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$benchmark = array(); |
| 32 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- AJAX handler |
| 33 |
switch ($_POST['task']) { |
| 34 |
case "cache": |
| 35 |
if (TagGroups_Utilities::is_premium_plan()) { |
| 36 |
$tag_group_object_cache = TagGroups_Options::get_option( |
| 37 |
'tag_group_object_cache', |
| 38 |
TagGroups_Object_Cache::WP_OPTIONS |
| 39 |
); |
| 40 |
|
| 41 |
$object_cache_options = array( |
| 42 |
1 => __('Transients', 'tag-groups'), |
| 43 |
2 => __('Database', 'tag-groups'), |
| 44 |
3 => __('Filesystem', 'tag-groups'), |
| 45 |
9 => __('WP Object Cache', 'tag-groups'), |
| 46 |
); |
| 47 |
|
| 48 |
$benchmark['name'] = __('Cache', 'tag-groups') . ' ' . __('(1000x read, 100x write)', 'tag-groups'); |
| 49 |
$benchmark['value'] = array(); |
| 50 |
$cache_key = md5('benchmark'); |
| 51 |
|
| 52 |
foreach ($object_cache_options as $object_cache_option_id => $object_cache_option_name) { |
| 53 |
$benchmark['value'][$object_cache_option_id] = ''; |
| 54 |
|
| 55 |
TagGroups_Options::update_option('tag_group_object_cache', $object_cache_option_id); |
| 56 |
|
| 57 |
$result = apply_filters('tag_groups_hook_cache_get', false, $cache_key . '-efficacy-test'); |
| 58 |
|
| 59 |
if ($result === false) { |
| 60 |
$benchmark['value'][$object_cache_option_id] .= sprintf( |
| 61 |
'%s: %s', |
| 62 |
$object_cache_option_name, |
| 63 |
__('ineffective', 'tag-groups') |
| 64 |
); |
| 65 |
} else { |
| 66 |
$start_time = microtime(true); |
| 67 |
$cache_key_loop = $cache_key . '-0'; |
| 68 |
|
| 69 |
for ($i = 0; $i < 1000; $i++) { |
| 70 |
if ($i % 10 == 0) { |
| 71 |
$cache_key_loop = $cache_key . '-' . $i; |
| 72 |
do_action('tag_groups_hook_cache_set', $cache_key_loop, 'sample content'); |
| 73 |
} |
| 74 |
|
| 75 |
$result = apply_filters('tag_groups_hook_cache_get', false, $cache_key_loop); |
| 76 |
|
| 77 |
if ($result != 'sample content') { |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ($result != 'sample content') { |
| 83 |
$benchmark['value'][$object_cache_option_id] = sprintf( |
| 84 |
'%s: error', |
| 85 |
$object_cache_option_name |
| 86 |
); |
| 87 |
} else { |
| 88 |
$selected = $object_cache_option_id == $tag_group_object_cache ? '(selected)' : ''; |
| 89 |
|
| 90 |
$benchmark['value'][$object_cache_option_id] .= sprintf( |
| 91 |
'%s: %d ms %s', |
| 92 |
$object_cache_option_name, |
| 93 |
1000 * (microtime(true) - $start_time), |
| 94 |
$selected |
| 95 |
); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$benchmark['value'] = implode('<br/>', $benchmark['value']); |
| 101 |
|
| 102 |
TagGroups_Options::update_option('tag_group_object_cache', $tag_group_object_cache); |
| 103 |
} |
| 104 |
|
| 105 |
break; |
| 106 |
} |
| 107 |
echo wp_json_encode($benchmark) ; |
| 108 |
wp_die(); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|