-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBasicExample.java
64 lines (55 loc) · 2.36 KB
/
BasicExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.j256.simplemetrics.example;
import java.io.IOException;
import java.util.Random;
import com.j256.simplemetrics.manager.MetricsManager;
import com.j256.simplemetrics.metric.ControlledMetricAccum;
import com.j256.simplemetrics.metric.ControlledMetricRatio;
import com.j256.simplemetrics.persister.MetricValuesPersister;
import com.j256.simplemetrics.persister.MetricsPersisterJob;
import com.j256.simplemetrics.persister.SystemOutMetricsPersister;
/**
* Basic example which shows some of the features of the SimpleMetrics package.
*
* @author graywatson
*/
public class BasicExample {
public static void main(String[] args) throws IOException {
// instantiate our manager
MetricsManager manager = new MetricsManager();
// create a simple metrics persister that writes to System.out
SystemOutMetricsPersister metricsPersister = new SystemOutMetricsPersister();
manager.setMetricValuesPersisters(new MetricValuesPersister[] { metricsPersister });
// instantiate a couple of metrics
ControlledMetricAccum hitsMetric =
new ControlledMetricAccum("example", null, "hits", "number of hits to the cache", null);
ControlledMetricAccum missesMetric =
new ControlledMetricAccum("example", null, "misses", "number of misses to the cache", null);
ControlledMetricRatio ratioMetric =
new ControlledMetricRatio("example", null, "ratio", "ratio of hits to misses", null);
// register them with the manager
manager.registerMetric(hitsMetric);
manager.registerMetric(missesMetric);
manager.registerMetric(ratioMetric);
/*
* Start up the persisting thread to persist the metrics every so often. This is persisting every 1 second but
* you'll probably want to do a minute (60000) or something.
*/
MetricsPersisterJob persisterThread = new MetricsPersisterJob(manager, 1000, 1000, true);
// now we run our application which is just doing some random counting
Random random = new Random();
for (long i = 0; i < 1000000000L; i++) {
// ok, we don't have a cache so we'll simulate using random
if (random.nextBoolean()) {
hitsMetric.increment();
ratioMetric.adjustValue(1, 1);
} else {
missesMetric.increment();
ratioMetric.adjustValue(0, 1);
}
}
// persist at the very end in case your computer is faster than mine (or it's the future)
manager.persist();
// shutdown the persister thread
persisterThread.destroy();
}
}