Skip to content

Instantly share code, notes, and snippets.

@dnorton
Created April 29, 2015 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnorton/54446665564c7516b135 to your computer and use it in GitHub Desktop.
Save dnorton/54446665564c7516b135 to your computer and use it in GitHub Desktop.
Small Test for metrics library
package com.velogica.metrics;
import com.codahale.metrics.*;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import org.junit.Test;
import rx.Observable;
import rx.functions.Action1;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
/**
* https://dropwizard.github.io/metrics/3.1.0/getting-started/
* Created by usunoda on 4/28/2015.
*/
public class MetricsTest {
static final MetricRegistry metrics = new MetricRegistry();
@Test
public void testBasicMetrics() {
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
Meter requests = metrics.meter("requests");
requests.mark();
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
fail("exception sleeping");
}
requests.mark();
assertTrue(true);
}
@Test
public void testJVMMetrics() {
ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(1, TimeUnit.SECONDS);
checkMetricSet(new GarbageCollectorMetricSet());
System.out.println("\n\n=== Memory Metrics ===\n\n");
checkMetricSet(new MemoryUsageGaugeSet());
byte[] largeByteArray = new byte[100000000];
checkMetricSet(new MemoryUsageGaugeSet());
System.out.println("\n\n=== Thread Metrics ===\n\n");
checkMetricSet(new ThreadStatesGaugeSet());
}
protected void checkMetricSet(MetricSet metricSet) {
for(Map.Entry<String, Metric> metricEntry: metricSet.getMetrics().entrySet()) {
assertNotNull(metricEntry.getValue());
if (metrics.getGauges().get("test." + metricEntry.getKey()) == null) {
metrics.register("test." + metricEntry.getKey(), metricEntry.getValue());
}
}
for (Map.Entry<String, Metric> metricEntry: metricSet.getMetrics().entrySet()) {
String metricName = "test." + metricEntry.getKey();
System.out.print(metricName + " = ");
Object value = metrics.getGauges().get(metricName).getValue();
System.out.println(value);
assertNotNull(value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment