1. <?php
  2. class RecordPerformance {
  3. private $record_data = []; // Array to store record performance data
  4. private $rate_limit = 10; // Maximum number of requests allowed per minute
  5. private $rate_limit_window = 60; // Time window for rate limiting in seconds
  6. private $last_request_time = 0; // Timestamp of the last request
  7. /**
  8. * Records performance data for a user record.
  9. *
  10. * @param string $user_id The ID of the user record.
  11. * @param callable $process_record A function that processes the record.
  12. * @return bool True on success, false on rate limit exceeded.
  13. */
  14. public function recordPerformance(string $user_id, callable $process_record): bool {
  15. $now = time();
  16. // Rate limiting check
  17. if ($now - $this->last_request_time > $this->rate_limit_window) {
  18. // Reset rate limit if window has passed
  19. $this->last_request_time = $now;
  20. $this->recordRecordPerformance($user_id, $process_record);
  21. return true;
  22. } else {
  23. // Rate limit exceeded
  24. echo "Rate limit exceeded for user ID: " . $user_id . "\n"; // Log the event
  25. return false;
  26. }
  27. }
  28. /**
  29. * Records the performance data for a single record.
  30. *
  31. * @param string $user_id The ID of the user record.
  32. * @param callable $process_record A function that processes the record.
  33. * @return void
  34. */
  35. private function recordRecordPerformance(string $user_id, callable $process_record): void {
  36. $start_time = microtime(true);
  37. $result = $process_record(); // Execute the record processing function
  38. $end_time = microtime(true);
  39. $execution_time = $end_time - $start_time;
  40. $this->record_data[] = [
  41. 'user_id' => $user_id,
  42. 'execution_time' => $execution_time,
  43. 'timestamp' => time(),
  44. 'result' => $result // Store the result of the function
  45. ];
  46. // Optional: Log the performance data to a file or database
  47. // Example: file_put_contents('performance_log.txt', json_encode($this->record_data) . "\n", FILE_APPEND);
  48. }
  49. /**
  50. * Clears the performance data.
  51. *
  52. * @return void
  53. */
  54. public function clearData(): void {
  55. $this->record_data = [];
  56. }
  57. /**
  58. * Gets the performance data.
  59. *
  60. * @return array The performance data.
  61. */
  62. public function getPerformanceData(): array {
  63. return $this->record_data;
  64. }
  65. }
  66. // Example Usage:
  67. // Define a function to process a record
  68. function processUserRecord(string $user_id): int {
  69. // Simulate some processing time
  70. sleep(0.1);
  71. return rand(1, 100);
  72. }
  73. $performanceTracker = new RecordPerformance();
  74. // Simulate multiple requests for different user IDs
  75. for ($i = 0; $i < 20; $i++) {
  76. $user_id = 'user_' . $i;
  77. $performanceTracker->recordPerformance($user_id, [$this, 'processUserRecord']);
  78. }
  79. //Clean up data
  80. $performanceTracker->clearData();
  81. // Get and print the performance data
  82. $performanceData = $performanceTracker->getPerformanceData();
  83. print_r($performanceData);
  84. ?>

Add your comment