From 66cee9dbd2054a9335657a367c5f35d04b9fa8e1 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 16 Jun 2019 09:16:50 +0200 Subject: [PATCH] adding icon generator comparison test script for reference in #148 --- tst/IconTest | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 tst/IconTest diff --git a/tst/IconTest b/tst/IconTest new file mode 100755 index 00000000..7794e478 --- /dev/null +++ b/tst/IconTest @@ -0,0 +1,97 @@ +#!/usr/bin/env php + new Identicon(new GdGenerator()), + 'identicon ImageMagick' => new Identicon(new ImageMagickGenerator()), + 'identicon SVG' => new Identicon(new SvgGenerator()) +); +$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 + ) +); +$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; +} + + + +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; +} + + + +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", + 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'); +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( + $generator . ':', + $result['lengths'][0], + $result['lengths'][ITERATIONS-1], + round(array_sum($result['lengths']) / ITERATIONS), + round($result['time'], 3) + ); +}