1
0

icon-test 4.1 KB

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