PerformanceTest.php 735 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Class PerformanceTest
  4. *
  5. * @created 16.10.2023
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2023 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeTest\Performance;
  11. use Closure;
  12. use function hrtime;
  13. /**
  14. *
  15. */
  16. class PerformanceTest{
  17. protected int $runs;
  18. protected int $total = 0;
  19. public function __construct(int $runs = 1000){
  20. $this->runs = $runs;
  21. }
  22. public function run(Closure $subject):self{
  23. $this->total = 0;
  24. for($i = 0; $i < $this->runs; $i++){
  25. $start = hrtime(true);
  26. $subject();
  27. $end = hrtime(true);
  28. $this->total += ($end - $start);
  29. }
  30. return $this;
  31. }
  32. public function getResult():float{
  33. return ($this->total / $this->runs / 1000000);
  34. }
  35. }