1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5. public class HeaderPerformanceMonitor {
  6. private static final int MEASUREMENT_INTERVAL = 1000; // milliseconds
  7. private static final String STAGING_ENVIRONMENT = "staging"; //Environment to monitor
  8. private static Map<String, Long> headerResponseTimes = new HashMap<>(); // Stores response times for headers
  9. private static Timer timer = new Timer();
  10. private static boolean isRunning = false;
  11. public static void startMonitoring() {
  12. if (isRunning) {
  13. System.out.println("Monitoring already running.");
  14. return;
  15. }
  16. isRunning = true;
  17. timer.scheduleAtFixedRate(new TimerTask() {
  18. @Override
  19. public void run() {
  20. measureHeaderResponseTimes();
  21. }
  22. }, 0, MEASUREMENT_INTERVAL);
  23. System.out.println("Header performance monitoring started for " + STAGING_ENVIRONMENT);
  24. }
  25. public static void stopMonitoring() {
  26. if (!isRunning) {
  27. System.out.println("Monitoring not running.");
  28. return;
  29. }
  30. isRunning = false;
  31. timer.cancel();
  32. System.out.println("Header performance monitoring stopped.");
  33. }
  34. private static void measureHeaderResponseTimes() {
  35. try {
  36. // Simulate making a request to a staging endpoint. Replace with your actual endpoint.
  37. long startTime = System.currentTimeMillis();
  38. String responseHeaders = getResponseHeadersFromStagingEnvironment(); // Function to get response headers
  39. long endTime = System.currentTimeMillis();
  40. if (responseHeaders != null) {
  41. for (Map.Entry<String, Long> entry : headerResponseTimes.entrySet()) {
  42. long existingTime = entry.getValue();
  43. headerResponseTimes.put(entry.getKey(), (endTime - startTime) + existingTime); // Accumulate time
  44. }
  45. headerResponseTimes.put("Total", (endTime - startTime)); //Track Total time
  46. } else {
  47. System.err.println("Error: Could not retrieve response headers from staging environment."); //Error Logging
  48. }
  49. } catch (Exception e) {
  50. System.err.println("Error during header performance measurement: " + e.getMessage()); //Error Logging
  51. e.printStackTrace();
  52. }
  53. }
  54. private static String getResponseHeadersFromStagingEnvironment() {
  55. // Replace this with your actual logic to fetch headers from the staging environment.
  56. // This is a placeholder. You'd typically use a library like HttpClient, OkHttp, or RestAssured.
  57. // Example using a mock:
  58. if (STAGING_ENVIRONMENT.equals("staging")) {
  59. return "Content-Type: application/json\n" +
  60. "X-Custom-Header: value\n" +
  61. "Server: StagingServer";
  62. } else {
  63. return null; // Simulate an error
  64. }
  65. }
  66. public static void main(String[] args) throws InterruptedException {
  67. startMonitoring();
  68. Thread.sleep(60000); // Run for 60 seconds.
  69. stopMonitoring();
  70. System.out.println("Final Header Response Times: " + headerResponseTimes);
  71. }
  72. }

Add your comment