add perf/size test for Jdenticons

This commit is contained in:
El RIDO 2022-10-30 09:04:27 +01:00
parent f5332ee6ff
commit 1892264cf0
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92

View File

@ -9,6 +9,7 @@ use Identicon\Generator\GdGenerator;
use Identicon\Generator\ImageMagickGenerator; use Identicon\Generator\ImageMagickGenerator;
use Identicon\Generator\SvgGenerator; use Identicon\Generator\SvgGenerator;
use Identicon\Identicon; use Identicon\Identicon;
use Jdenticon\Identicon as Jdenticon;
use PrivateBin\Vizhash16x16; use PrivateBin\Vizhash16x16;
@ -17,7 +18,18 @@ $vizhash = new Vizhash16x16();
$identiconGenerators = array( $identiconGenerators = array(
'identicon GD' => new Identicon(new GdGenerator()), 'identicon GD' => new Identicon(new GdGenerator()),
'identicon ImageMagick' => new Identicon(new ImageMagickGenerator()), 'identicon ImageMagick' => new Identicon(new ImageMagickGenerator()),
'identicon SVG' => new Identicon(new SvgGenerator()) 'identicon SVG' => new Identicon(new SvgGenerator()),
);
$jdenticon = new Jdenticon(array(
'size' => 16,
'style' => array(
'backgroundColor' => '#fff0', // fully transparent, for dark mode
'padding' => 0,
),
));
$jdenticonGenerators = array(
'jdenticon GD' => 'png',
'jdenticon SVG' => 'svg',
); );
$results = array( $results = array(
'vizhash' => array( 'vizhash' => array(
@ -35,21 +47,26 @@ $results = array(
'identicon SVG' => array( 'identicon SVG' => array(
'lengths' => array(), 'lengths' => array(),
'time' => 0 'time' => 0
) ),
'jdenticon GD' => array(
'lengths' => array(),
'time' => 0
),
'jdenticon SVG' => array(
'lengths' => array(),
'time' => 0
),
); );
$hmacs = array(); $hmacs = array();
echo 'generate ', ITERATIONS, ' hmacs and pre-populate the result array, so tests wont be slowed down', PHP_EOL; echo 'generate ', ITERATIONS, ' hmacs and pre-populate the result array, so tests wont be slowed down', PHP_EOL;
for ($i = 0; $i < ITERATIONS; ++$i) { for ($i = 0; $i < ITERATIONS; ++$i) {
$hmacs[$i] = hash_hmac('sha512', '127.0.0.1', bin2hex(random_bytes(256))); $hmacs[$i] = hash_hmac('sha512', '127.0.0.1', bin2hex(random_bytes(256)));
$results['vizhash']['lengths'][$i] = 0; foreach (array_keys($results) as $test) {
$results['identicon GD']['lengths'][$i] = 0; $results[$test]['lengths'][$i] = 0;
$results['identicon ImageMagick']['lengths'][$i] = 0; }
$results['identicon SVG']['lengths'][$i] = 0;
} }
echo 'run vizhash tests', PHP_EOL; echo 'run vizhash tests', PHP_EOL;
$start = microtime(true); $start = microtime(true);
foreach ($hmacs as $i => $hmac) { foreach ($hmacs as $i => $hmac) {
@ -60,7 +77,6 @@ foreach ($hmacs as $i => $hmac) {
} }
$results['vizhash']['time'] = microtime(true) - $start; $results['vizhash']['time'] = microtime(true) - $start;
foreach ($identiconGenerators as $key => $identicon) { foreach ($identiconGenerators as $key => $identicon) {
echo 'run ', $key,' tests', PHP_EOL; echo 'run ', $key,' tests', PHP_EOL;
$start = microtime(true); $start = microtime(true);
@ -71,9 +87,30 @@ foreach ($identiconGenerators as $key => $identicon) {
$results[$key]['time'] = microtime(true) - $start; $results[$key]['time'] = microtime(true) - $start;
} }
foreach ($jdenticonGenerators as $key => $format) {
echo 'run ', $key,' tests', PHP_EOL;
$start = microtime(true);
foreach ($hmacs as $i => $hmac) {
$jdenticon->setHash($hmac);
$data = $jdenticon->getImageDataUri($format);
$results[$key]['lengths'][$i] = strlen($data);
}
$results[$key]['time'] = microtime(true) - $start;
}
define('PADDING_LENGTH', max(array_map(function ($key) { return strlen($key); }, array_keys($results))) + 1); define(
'PADDING_LENGTH',
max(
array_map(
function ($key) {
return strlen($key);
},
array_keys($results)
)
) + 1
);
function format_result_line($generator, $min, $max, $avg, $sec) { function format_result_line($generator, $min, $max, $avg, $sec) {
echo str_pad($generator, PADDING_LENGTH, ' '), "\t", echo str_pad($generator, PADDING_LENGTH, ' '), "\t",
str_pad($min, 4, ' ', STR_PAD_LEFT), "\t", str_pad($min, 4, ' ', STR_PAD_LEFT), "\t",
@ -84,7 +121,10 @@ function format_result_line($generator, $min, $max, $avg, $sec) {
echo PHP_EOL; echo PHP_EOL;
format_result_line('Generator:', 'min', 'max', 'avg', 'seconds'); format_result_line('Generator:', 'min', 'max', 'avg', 'seconds');
format_result_line(str_repeat('─', PADDING_LENGTH), str_repeat('─', 4), str_repeat('─', 4), str_repeat('─', 4), str_repeat('─', 7)); format_result_line(
str_repeat('─', PADDING_LENGTH), str_repeat('─', 4), str_repeat('─', 4),
str_repeat('─', 4), str_repeat('─', 7)
);
foreach ($results as $generator => $result) { foreach ($results as $generator => $result) {
sort($result['lengths']); sort($result['lengths']);
format_result_line( format_result_line(