1. <?php
  2. /**
  3. * Timestamp Performance Measurement with Manual Overrides
  4. */
  5. class TimestampPerformance {
  6. private $testName;
  7. private $iterations;
  8. private $startTimes = [];
  9. private $endTimes = [];
  10. private $results = [];
  11. private $manualOverrideEnabled = false;
  12. /**
  13. * Constructor
  14. * @param string $testName Name of the test
  15. * @param int $iterations Number of iterations to run
  16. */
  17. public function __construct(string $testName, int $iterations) {
  18. $this->testName = $testName;
  19. $this->iterations = $iterations;
  20. }
  21. /**
  22. * Enable manual override mode. If enabled, timestamps are manually set.
  23. * @param bool $enabled Whether to enable manual override.
  24. */
  25. public function enableManualOverride(bool $enabled): void {
  26. $this->manualOverrideEnabled = $enabled;
  27. }
  28. /**
  29. * Record the start time for a test. Can be overridden manually.
  30. * @param int $timestamp Timestamp to use as start. If manual override is enabled, this value is used.
  31. * @return bool True on success, false on failure.
  32. */
  33. public function start(int $timestamp = null): bool {
  34. if ($this->manualOverrideEnabled) {
  35. $this->startTimes[] = $timestamp;
  36. return true;
  37. } else {
  38. $startTime = microtime(true);
  39. $this->startTimes[] = $startTime;
  40. return true;
  41. }
  42. }
  43. /**
  44. * Record the end time for a test. Can be overridden manually.
  45. * @param int $timestamp Timestamp to use as end. If manual override is enabled, this value is used.
  46. * @return bool True on success, false on failure.
  47. */
  48. public function end(int $timestamp = null): bool {
  49. if ($this->manualOverrideEnabled) {
  50. $this->endTimes[] = $timestamp;
  51. return true;
  52. } else {
  53. $endTime = microtime(true);
  54. $this->endTimes[] = $endTime;
  55. return true;
  56. }
  57. }
  58. /**
  59. * Perform the test and calculate performance.
  60. * @return array An array of results.
  61. */
  62. public function run(): array {
  63. if (count($this->startTimes) !== count($this->endTimes)) {
  64. error_log("Error: Number of start and end times do not match.");
  65. return [];
  66. }
  67. $results = [];
  68. for ($i = 0; $i < $this->iterations; $i++) {
  69. $startTime = $this->startTimes[$i];
  70. $endTime = $this->endTimes[$i];
  71. $executionTime = $endTime - $startTime;
  72. $results[] = [
  73. 'iteration' => $i + 1,
  74. 'execution_time' => $executionTime
  75. ];
  76. }
  77. return $results;
  78. }
  79. /**
  80. * Get the results.
  81. * @return array
  82. */
  83. public function getResults(): array {
  84. return $this->results;
  85. }
  86. }
  87. /**
  88. * Example Usage
  89. */
  90. // Create a test object
  91. $test = new TimestampPerformance('MyTest', 100);
  92. // Enable manual override
  93. $test->enableManualOverride(true);
  94. // Simulate some work
  95. for ($i = 0; $i < 1000; $i++) {
  96. //Do something here
  97. }
  98. // Manually set start and end times for a specific iteration.
  99. $test->start(time());
  100. for ($i = 0; $i < 1000; $i++) {
  101. //Do something here
  102. }
  103. $test->end(time() + 1);
  104. // Run the test
  105. $results = $test->run();
  106. // Output the results
  107. echo "<pre>";
  108. print_r($results);
  109. echo "</pre>";
  110. //Example without manual override
  111. $test2 = new TimestampPerformance('MyTest', 100);
  112. $results2 = $test2->run();
  113. echo "<pre>";
  114. print_r($results2);
  115. echo "</pre>";
  116. ?>

Add your comment