1. <?php
  2. /**
  3. * Collects metrics for short-lived tasks.
  4. *
  5. * This script gathers data about task execution, including duration,
  6. * success/failure, and resource usage. Designed for short-lived operations.
  7. */
  8. class TaskMetricsCollector {
  9. private $taskName; // Name of the task being measured
  10. private $startTime; // Timestamp of task start
  11. private $endTime; // Timestamp of task end
  12. private $success; // Boolean indicating task success
  13. private $duration; // Task execution time in seconds
  14. private $resourcesUsed; // An array of resource usage metrics
  15. /**
  16. * Constructor
  17. * @param string $taskName The name of the task.
  18. */
  19. public function __construct(string $taskName) {
  20. $this->taskName = $taskName;
  21. $this->startTime = time(); // Record the start time
  22. $this->endTime = null; // Initialize end time
  23. $this->success = false; // Initialize success
  24. $this->duration = 0; // Initialize duration
  25. $this->resourcesUsed = []; // Initialize resource usage
  26. }
  27. /**
  28. * Start the task. Called at the beginning of the task.
  29. */
  30. public function start() {
  31. //Do nothing, just setup the start time
  32. }
  33. /**
  34. * End the task and record metrics.
  35. * @param bool $success Whether the task completed successfully.
  36. */
  37. public function end(bool $success): void {
  38. $this->endTime = time(); // Record the end time
  39. $this->success = $success; // Record success status
  40. $this->duration = round((time() - $this->startTime) / 60); // Calculate duration in minutes
  41. $this->resourcesUsed = $this->getResourceUsage(); //Get Resource Usage
  42. // Output the collected metrics (replace with your desired storage method)
  43. $this->outputMetrics();
  44. }
  45. /**
  46. * Get resource usage metrics. This is a placeholder; implement your
  47. * resource monitoring logic here.
  48. * @return array
  49. */
  50. private function getResourceUsage(): array {
  51. // Example: Simulate resource usage
  52. $cpuUsage = rand(1, 100);
  53. $memoryUsage = rand(10, 500);
  54. $networkBytes = rand(1000, 10000);
  55. return [
  56. 'cpu' => $cpuUsage,
  57. 'memory' => $memoryUsage,
  58. 'network' => $networkBytes
  59. ];
  60. }
  61. /**
  62. * Output the collected metrics. Replace with your preferred output method
  63. * (e.g., logging, database insertion, sending to a monitoring service).
  64. */
  65. private function outputMetrics(): void {
  66. echo "Task: " . $this->taskName . "\n";
  67. echo "Start Time: " . date("Y-m-d H:i:s", $this->startTime) . "\n";
  68. echo "End Time: " . date("Y-m-d H:i:s", $this->endTime) . "\n";
  69. echo "Success: " . ($this->success ? 'true' : 'false') . "\n";
  70. echo "Duration: " . $this->duration . " minutes\n";
  71. echo "Resources Used: " . json_encode($this->resourcesUsed, JSON_PRETTY_PRINT) . "\n";
  72. echo "--------------------\n";
  73. }
  74. }
  75. // Example Usage:
  76. // Create a collector for a specific task
  77. $taskCollector = new TaskMetricsCollector('short_task');
  78. // Start the task
  79. $taskCollector->start();
  80. // Simulate task execution (replace with your actual task code)
  81. sleep(5); // Simulate 5 seconds of work
  82. // End the task
  83. $taskCollector->end(true); // Indicate successful completion
  84. ?>

Add your comment