diff --git a/tst/IconTest b/tst/IconTest index 7794e47..e39cc7b 100755 --- a/tst/IconTest +++ b/tst/IconTest @@ -9,6 +9,7 @@ use Identicon\Generator\GdGenerator; use Identicon\Generator\ImageMagickGenerator; use Identicon\Generator\SvgGenerator; use Identicon\Identicon; +use Jdenticon\Identicon as Jdenticon; use PrivateBin\Vizhash16x16; @@ -17,7 +18,18 @@ $vizhash = new Vizhash16x16(); $identiconGenerators = array( 'identicon GD' => new Identicon(new GdGenerator()), '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( 'vizhash' => array( @@ -35,21 +47,26 @@ $results = array( 'identicon SVG' => array( 'lengths' => array(), 'time' => 0 - ) + ), + 'jdenticon GD' => array( + 'lengths' => array(), + 'time' => 0 + ), + 'jdenticon SVG' => array( + 'lengths' => array(), + 'time' => 0 + ), ); $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))); - $results['vizhash']['lengths'][$i] = 0; - $results['identicon GD']['lengths'][$i] = 0; - $results['identicon ImageMagick']['lengths'][$i] = 0; - $results['identicon SVG']['lengths'][$i] = 0; + foreach (array_keys($results) as $test) { + $results[$test]['lengths'][$i] = 0; + } } - - echo 'run vizhash tests', PHP_EOL; $start = microtime(true); foreach ($hmacs as $i => $hmac) { @@ -60,7 +77,6 @@ foreach ($hmacs as $i => $hmac) { } $results['vizhash']['time'] = microtime(true) - $start; - foreach ($identiconGenerators as $key => $identicon) { echo 'run ', $key,' tests', PHP_EOL; $start = microtime(true); @@ -71,9 +87,30 @@ foreach ($identiconGenerators as $key => $identicon) { $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) { echo str_pad($generator, PADDING_LENGTH, ' '), "\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; 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) { sort($result['lengths']); format_result_line(