public void service() throws Exception {
String dir = getTestDir().getAbsolutePath();
String services = StringUtils.toString(Arrays.asList(InstrumentationService.class.getName()), ",");
XConfiguration conf = new XConfiguration();
conf.set("server.services", services);
Server server = new Server("server", dir, dir, dir, dir, conf);
server.init();
Instrumentation instrumentation = server.get(Instrumentation.class);
Assert.assertNotNull(instrumentation);
instrumentation.incr("g", "c", 1);
instrumentation.incr("g", "c", 2);
instrumentation.incr("g", "c1", 2);
Instrumentation.Cron cron = instrumentation.createCron();
cron.start();
sleep(100);
cron.stop();
instrumentation.addCron("g", "t", cron);
cron = instrumentation.createCron();
cron.start();
sleep(200);
cron.stop();
instrumentation.addCron("g", "t", cron);
Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
@Override
public String getValue() {
return "foo";
}
};
instrumentation.addVariable("g", "v", var);
Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
@Override
public Long getValue() {
return 1L;
}
};
instrumentation.addSampler("g", "s", 10, varToSample);
Map<String, ?> snapshot = instrumentation.getSnapshot();
Assert.assertNotNull(snapshot.get("os-env"));
Assert.assertNotNull(snapshot.get("sys-props"));
Assert.assertNotNull(snapshot.get("jvm"));
Assert.assertNotNull(snapshot.get("counters"));
Assert.assertNotNull(snapshot.get("timers"));
Assert.assertNotNull(snapshot.get("variables"));
Assert.assertNotNull(snapshot.get("samplers"));
Assert.assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
Assert.assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
Assert.assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
Assert.assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));
StringWriter writer = new StringWriter();
JSONObject.writeJSONString(snapshot, writer);
writer.close();
server.destroy();
}