|  | 
| 1 | 1 | package spp.demo; | 
| 2 | 2 | 
 | 
|  | 3 | +import com.codahale.metrics.ConsoleReporter; | 
|  | 4 | +import com.codahale.metrics.MetricRegistry; | 
|  | 5 | +import com.codahale.metrics.Timer; | 
| 3 | 6 | import io.micronaut.runtime.Micronaut; | 
| 4 | 7 | import spp.demo.command.AddBreakpoint; | 
| 5 | 8 | import spp.demo.command.AddLog; | 
| 6 | 9 | import spp.demo.command.TailLogs; | 
| 7 | 10 | 
 | 
| 8 | 11 | import java.net.HttpURLConnection; | 
|  | 12 | +import java.net.MalformedURLException; | 
| 9 | 13 | import java.net.URL; | 
| 10 |  | -import java.util.Map; | 
| 11 | 14 | import java.util.concurrent.Executor; | 
| 12 | 15 | import java.util.concurrent.Executors; | 
| 13 | 16 | 
 | 
| 14 | 17 | public class Main { | 
| 15 | 18 | 
 | 
| 16 | 19 |  private static final Executor executor = Executors.newCachedThreadPool(); | 
|  | 20 | + private static final MetricRegistry metricRegistry = new MetricRegistry(); | 
| 17 | 21 | 
 | 
| 18 | 22 |  public static void main(String[] args) throws Exception { | 
| 19 | 23 |  Micronaut.run(Main.class, args); | 
| 20 | 24 | 
 | 
|  | 25 | + ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build(); | 
|  | 26 | + | 
| 21 | 27 |  while (true) { | 
| 22 | 28 |  executeDemos(); | 
| 23 | 29 |  Thread.sleep(1000); | 
| 24 | 30 | 
 | 
| 25 |  | - Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces(); | 
| 26 |  | - for (Map.Entry<Thread, StackTraceElement[]> entry : threads.entrySet()) { | 
| 27 |  | - Thread thread = entry.getKey(); | 
| 28 |  | - StackTraceElement[] stackTraceElements = entry.getValue(); | 
| 29 |  | - System.out.println("Thread: " + thread.getName()); | 
| 30 |  | - for (StackTraceElement stackTraceElement : stackTraceElements) { | 
| 31 |  | - System.out.println(" " + stackTraceElement); | 
| 32 |  | - } | 
| 33 |  | - } | 
|  | 31 | + reporter.report(); | 
| 34 | 32 | 
 | 
| 35 | 33 |  int threadCount = Thread.activeCount(); | 
| 36 | 34 |  System.out.println("Thread count: " + threadCount); | 
| @@ -92,15 +90,27 @@ public static void triggerEndpoints() { | 
| 92 | 90 |  } | 
| 93 | 91 | 
 | 
| 94 | 92 |  private static void callEndpoint(String endpoint) { | 
|  | 93 | + Timer.Context timer = metricRegistry.timer(endpoint).time(); | 
|  | 94 | + URL url; | 
|  | 95 | + try { | 
|  | 96 | + url = new URL("http://localhost:8080" + endpoint); | 
|  | 97 | + } catch (MalformedURLException e) { | 
|  | 98 | + throw new RuntimeException(e); | 
|  | 99 | + } | 
|  | 100 | + | 
| 95 | 101 |  executor.execute(() -> { | 
|  | 102 | + HttpURLConnection connection = null; | 
| 96 | 103 |  try { | 
| 97 |  | - URL url = new URL("http://localhost:8080" + endpoint); | 
| 98 |  | - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | 
|  | 104 | + connection = (HttpURLConnection) url.openConnection(); | 
| 99 | 105 |  connection.setConnectTimeout(5000); | 
| 100 | 106 |  connection.setReadTimeout(5000); | 
| 101 | 107 |  connection.getResponseCode(); | 
| 102 |  | - connection.disconnect(); | 
| 103 | 108 |  } catch (Exception ignore) { | 
|  | 109 | + } finally { | 
|  | 110 | + if (connection != null) { | 
|  | 111 | + connection.disconnect(); | 
|  | 112 | + } | 
|  | 113 | + timer.close(); | 
| 104 | 114 |  } | 
| 105 | 115 |  }); | 
| 106 | 116 |  } | 
|  | 
0 commit comments