2019-06-16 09:16:50 +02:00
|
|
|
#!/usr/bin/env php
|
2024-06-04 07:13:55 +02:00
|
|
|
<?php declare(strict_types=1);
|
2024-05-28 20:58:12 +02:00
|
|
|
/**
|
|
|
|
* PrivateBin
|
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
|
|
|
*/
|
2019-06-16 09:16:50 +02:00
|
|
|
|
2024-05-28 20:58:12 +02:00
|
|
|
define('ITERATIONS', 100000);
|
2019-06-16 09:16:50 +02:00
|
|
|
|
|
|
|
require dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
|
|
|
use Identicon\Generator\GdGenerator;
|
|
|
|
use Identicon\Generator\ImageMagickGenerator;
|
|
|
|
use Identicon\Generator\SvgGenerator;
|
|
|
|
use Identicon\Identicon;
|
2022-10-30 09:04:27 +01:00
|
|
|
use Jdenticon\Identicon as Jdenticon;
|
2019-06-16 09:16:50 +02:00
|
|
|
use PrivateBin\Vizhash16x16;
|
|
|
|
|
|
|
|
$vizhash = new Vizhash16x16();
|
|
|
|
$identiconGenerators = array(
|
|
|
|
'identicon GD' => new Identicon(new GdGenerator()),
|
|
|
|
'identicon ImageMagick' => new Identicon(new ImageMagickGenerator()),
|
2022-10-30 09:04:27 +01:00
|
|
|
'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(
|
2022-11-07 19:42:20 +01:00
|
|
|
'jdenticon' => 'png',
|
|
|
|
'jdenticon ImageMagick' => 'png',
|
2022-10-30 09:04:27 +01:00
|
|
|
'jdenticon SVG' => 'svg',
|
2019-06-16 09:16:50 +02:00
|
|
|
);
|
|
|
|
$results = array(
|
|
|
|
'vizhash' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
|
|
|
'identicon GD' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
|
|
|
'identicon ImageMagick' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
|
|
|
'identicon SVG' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
2022-10-30 09:04:27 +01:00
|
|
|
),
|
2022-11-07 19:42:20 +01:00
|
|
|
'jdenticon' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
|
|
|
'jdenticon ImageMagick' => array(
|
2022-10-30 09:04:27 +01:00
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
|
|
|
'jdenticon SVG' => array(
|
|
|
|
'lengths' => array(),
|
|
|
|
'time' => 0
|
|
|
|
),
|
2019-06-16 09:16:50 +02:00
|
|
|
);
|
|
|
|
$hmacs = array();
|
|
|
|
|
|
|
|
echo 'generate ', ITERATIONS, ' hmacs and pre-populate the result array, so tests wont be slowed down', PHP_EOL;
|
|
|
|
for ($i = 0; $i < ITERATIONS; ++$i) {
|
|
|
|
$hmacs[$i] = hash_hmac('sha512', '127.0.0.1', bin2hex(random_bytes(256)));
|
2022-10-30 09:04:27 +01:00
|
|
|
foreach (array_keys($results) as $test) {
|
|
|
|
$results[$test]['lengths'][$i] = 0;
|
|
|
|
}
|
2019-06-16 09:16:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo 'run vizhash tests', PHP_EOL;
|
|
|
|
$start = microtime(true);
|
|
|
|
foreach ($hmacs as $i => $hmac) {
|
|
|
|
$data = 'data:image/png;base64,' . base64_encode(
|
|
|
|
$vizhash->generate($hmac)
|
|
|
|
);
|
|
|
|
$results['vizhash']['lengths'][$i] = strlen($data);
|
|
|
|
}
|
|
|
|
$results['vizhash']['time'] = microtime(true) - $start;
|
|
|
|
|
|
|
|
foreach ($identiconGenerators as $key => $identicon) {
|
|
|
|
echo 'run ', $key,' tests', PHP_EOL;
|
|
|
|
$start = microtime(true);
|
|
|
|
foreach ($hmacs as $i => $hmac) {
|
|
|
|
$data = $identicon->getImageDataUri($hmac, 16);
|
|
|
|
$results[$key]['lengths'][$i] = strlen($data);
|
|
|
|
}
|
|
|
|
$results[$key]['time'] = microtime(true) - $start;
|
|
|
|
}
|
|
|
|
|
2022-10-30 09:04:27 +01:00
|
|
|
foreach ($jdenticonGenerators as $key => $format) {
|
|
|
|
echo 'run ', $key,' tests', PHP_EOL;
|
2022-11-07 19:42:20 +01:00
|
|
|
if ($key === 'jdenticon ImageMagick') {
|
|
|
|
$jdenticon->enableImageMagick = true;
|
|
|
|
} else {
|
|
|
|
$jdenticon->enableImageMagick = false;
|
|
|
|
}
|
2022-10-30 09:04:27 +01:00
|
|
|
$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;
|
|
|
|
}
|
2019-06-16 09:16:50 +02:00
|
|
|
|
|
|
|
|
2022-10-30 09:04:27 +01:00
|
|
|
define(
|
|
|
|
'PADDING_LENGTH',
|
|
|
|
max(
|
|
|
|
array_map(
|
|
|
|
function ($key) {
|
|
|
|
return strlen($key);
|
|
|
|
},
|
|
|
|
array_keys($results)
|
|
|
|
)
|
|
|
|
) + 1
|
|
|
|
);
|
|
|
|
|
2019-06-16 09:16:50 +02:00
|
|
|
function format_result_line($generator, $min, $max, $avg, $sec) {
|
|
|
|
echo str_pad($generator, PADDING_LENGTH, ' '), "\t",
|
|
|
|
str_pad($min, 4, ' ', STR_PAD_LEFT), "\t",
|
|
|
|
str_pad($max, 4, ' ', STR_PAD_LEFT), "\t",
|
|
|
|
str_pad($avg, 4, ' ', STR_PAD_LEFT), "\t",
|
|
|
|
str_pad($sec, 7, ' ', STR_PAD_LEFT), PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo PHP_EOL;
|
|
|
|
format_result_line('Generator:', 'min', 'max', 'avg', 'seconds');
|
2022-10-30 09:04:27 +01:00
|
|
|
format_result_line(
|
|
|
|
str_repeat('─', PADDING_LENGTH), str_repeat('─', 4), str_repeat('─', 4),
|
|
|
|
str_repeat('─', 4), str_repeat('─', 7)
|
|
|
|
);
|
2019-06-16 09:16:50 +02:00
|
|
|
foreach ($results as $generator => $result) {
|
|
|
|
sort($result['lengths']);
|
|
|
|
format_result_line(
|
|
|
|
$generator . ':',
|
|
|
|
$result['lengths'][0],
|
|
|
|
$result['lengths'][ITERATIONS-1],
|
|
|
|
round(array_sum($result['lengths']) / ITERATIONS),
|
|
|
|
round($result['time'], 3)
|
|
|
|
);
|
|
|
|
}
|