icon-test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #!/usr/bin/env php
  2. <?php declare(strict_types=1);
  3. /**
  4. * PrivateBin
  5. *
  6. * a zero-knowledge paste bin
  7. *
  8. * @link https://github.com/PrivateBin/PrivateBin
  9. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  10. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  11. */
  12. define('ITERATIONS', 100000);
  13. require dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
  14. use Identicon\Generator\GdGenerator;
  15. use Identicon\Generator\ImageMagickGenerator;
  16. use Identicon\Generator\SvgGenerator;
  17. use Identicon\Identicon;
  18. use Jdenticon\Identicon as Jdenticon;
  19. use PrivateBin\Vizhash16x16;
  20. $vizhash = new Vizhash16x16();
  21. $identiconGenerators = array(
  22. 'identicon GD' => new Identicon(new GdGenerator()),
  23. 'identicon ImageMagick' => new Identicon(new ImageMagickGenerator()),
  24. 'identicon SVG' => new Identicon(new SvgGenerator()),
  25. );
  26. $jdenticon = new Jdenticon(array(
  27. 'size' => 16,
  28. 'style' => array(
  29. 'backgroundColor' => '#fff0', // fully transparent, for dark mode
  30. 'padding' => 0,
  31. ),
  32. ));
  33. $jdenticonGenerators = array(
  34. 'jdenticon' => 'png',
  35. 'jdenticon ImageMagick' => 'png',
  36. 'jdenticon SVG' => 'svg',
  37. );
  38. $results = array(
  39. 'vizhash' => array(
  40. 'lengths' => array(),
  41. 'time' => 0
  42. ),
  43. 'identicon GD' => array(
  44. 'lengths' => array(),
  45. 'time' => 0
  46. ),
  47. 'identicon ImageMagick' => array(
  48. 'lengths' => array(),
  49. 'time' => 0
  50. ),
  51. 'identicon SVG' => array(
  52. 'lengths' => array(),
  53. 'time' => 0
  54. ),
  55. 'jdenticon' => array(
  56. 'lengths' => array(),
  57. 'time' => 0
  58. ),
  59. 'jdenticon ImageMagick' => array(
  60. 'lengths' => array(),
  61. 'time' => 0
  62. ),
  63. 'jdenticon SVG' => array(
  64. 'lengths' => array(),
  65. 'time' => 0
  66. ),
  67. );
  68. $hmacs = array();
  69. echo 'generate ', ITERATIONS, ' hmacs and pre-populate the result array, so tests wont be slowed down', PHP_EOL;
  70. for ($i = 0; $i < ITERATIONS; ++$i) {
  71. $hmacs[$i] = hash_hmac('sha512', '127.0.0.1', bin2hex(random_bytes(256)));
  72. foreach (array_keys($results) as $test) {
  73. $results[$test]['lengths'][$i] = 0;
  74. }
  75. }
  76. echo 'run vizhash tests', PHP_EOL;
  77. $start = microtime(true);
  78. foreach ($hmacs as $i => $hmac) {
  79. $data = 'data:image/png;base64,' . base64_encode(
  80. $vizhash->generate($hmac)
  81. );
  82. $results['vizhash']['lengths'][$i] = strlen($data);
  83. }
  84. $results['vizhash']['time'] = microtime(true) - $start;
  85. foreach ($identiconGenerators as $key => $identicon) {
  86. echo 'run ', $key,' tests', PHP_EOL;
  87. $start = microtime(true);
  88. foreach ($hmacs as $i => $hmac) {
  89. $data = $identicon->getImageDataUri($hmac, 16);
  90. $results[$key]['lengths'][$i] = strlen($data);
  91. }
  92. $results[$key]['time'] = microtime(true) - $start;
  93. }
  94. foreach ($jdenticonGenerators as $key => $format) {
  95. echo 'run ', $key,' tests', PHP_EOL;
  96. if ($key === 'jdenticon ImageMagick') {
  97. $jdenticon->enableImageMagick = true;
  98. } else {
  99. $jdenticon->enableImageMagick = false;
  100. }
  101. $start = microtime(true);
  102. foreach ($hmacs as $i => $hmac) {
  103. $jdenticon->setHash($hmac);
  104. $data = $jdenticon->getImageDataUri($format);
  105. $results[$key]['lengths'][$i] = strlen($data);
  106. }
  107. $results[$key]['time'] = microtime(true) - $start;
  108. }
  109. define(
  110. 'PADDING_LENGTH',
  111. max(
  112. array_map(
  113. function ($key) {
  114. return strlen($key);
  115. },
  116. array_keys($results)
  117. )
  118. ) + 1
  119. );
  120. function format_result_line($generator, $min, $max, $avg, $sec) {
  121. echo str_pad($generator, PADDING_LENGTH, ' '), "\t",
  122. str_pad($min, 4, ' ', STR_PAD_LEFT), "\t",
  123. str_pad($max, 4, ' ', STR_PAD_LEFT), "\t",
  124. str_pad($avg, 4, ' ', STR_PAD_LEFT), "\t",
  125. str_pad($sec, 7, ' ', STR_PAD_LEFT), PHP_EOL;
  126. }
  127. echo PHP_EOL;
  128. format_result_line('Generator:', 'min', 'max', 'avg', 'seconds');
  129. format_result_line(
  130. str_repeat('─', PADDING_LENGTH), str_repeat('─', 4), str_repeat('─', 4),
  131. str_repeat('─', 4), str_repeat('─', 7)
  132. );
  133. foreach ($results as $generator => $result) {
  134. sort($result['lengths']);
  135. format_result_line(
  136. $generator . ':',
  137. $result['lengths'][0],
  138. $result['lengths'][ITERATIONS-1],
  139. round(array_sum($result['lengths']) / ITERATIONS),
  140. round($result['time'], 3)
  141. );
  142. }