[SNIPPET] Performance Tester - DEV Tool

Mixed forum with phpBB code snippets
Guides and styles

[SNIPPET] Performance Tester - DEV Tool

Post by Stoker »

The Performance Tester is a tool for developers.
Use it to test your extensions load and performance.

You need some skills to use this snippet and the support will be limited.
We need db and template in the extesion. If its not there you have to add it. Remember the services.yml

In this example I will show you how to add the performance to my Random Quote extension.

We need to edit the event/listener.php

Find:

Code: Select all

protected $table;
Add after:

Code: Select all

protected $speed_snapshots = [];

	public function speed_test_start()
	{
		$this->speed_snapshots['start'] = [
			'time' => microtime(true),
			'sql_time' => $this->db->get_sql_time(),
			'queries' => $this->db->sql_num_queries(),
			'memory' => memory_get_usage(),
		];
	}

	public function speed_test_end()
	{
		if (!isset($this->speed_snapshots['start']))
		{
			return 'Speed test start not set.';
		}

		$start = $this->speed_snapshots['start'];
		$end = [
			'time' => microtime(true),
			'sql_time' => $this->db->get_sql_time(),
			'queries' => $this->db->sql_num_queries(),
			'memory' => memory_get_usage(),
		];

		$diff_time = $end['time'] - $start['time'];
		$diff_sql_time = $end['sql_time'] - $start['sql_time'];
		$diff_php_time = $diff_time - $diff_sql_time;
		$diff_queries = $end['queries'] - $start['queries'];
		$diff_memory = $end['memory'] - $start['memory'];

		$formatted_memory = get_formatted_filesize(abs($diff_memory));
		$memory_direction = $diff_memory >= 0 ? '+' : '-';

		// Clear snapshot
		unset($this->speed_snapshots['start']);

		return sprintf(
			'<pre style="background-color:#fff;text-align:center;margin-top:4px;margin-bottom:4px;padding:2px;font-size:12px;border-top:1px solid crimson;border-bottom:1px solid crimson;">Random Quotes:<br>SQL: %.4fs | PHP: %.4fs | Total: %.4fs<br>Queries: %d | Memory: %s%s</pre>',
			$diff_sql_time,
			$diff_php_time,
			$diff_time,
			$diff_queries,
			$memory_direction,
			$formatted_memory
		);
	}
Next edit will tell where we start.
Find:

Code: Select all

$random = $this->get_random_function();
Before add:

Code: Select all

$this->speed_test_start();
Find:

Code: Select all

$this->template->assign_vars([
			'S_RANDOMQUOTES_ENABLED' => !empty($this->config['randomquotes_enable']),
			'S_RANDOMQUOTES_ENABLED_PORTAL' => !empty($this->config['randomquotes_portal_enable']),
		]);
After add:

Code: Select all

$this->template->assign_var('SPEED_DEBUG_RANDOM_QUOTE', $this->speed_test_end());
Last step is to add it to the template file.
You can use any file for this. But I suggest you use one from the extension you test.

Only admin can see it:

Code: Select all

{% if U_ACP %}
{{ SPEED_DEBUG_RANDOM_QUOTE }}
{% endif %}

Demo: https://demo.phpbb3bbcodes.com/

Screenshot:
performance_tester.png
You do not have the required permissions to view the files attached to this post.