Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
simplemetrics/src/test/java/com/j256/simplemetrics/example/BasicExample.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (55 sloc)
2.36 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |