java instumentation_Java Instrumentation.addVariable方法代码示例

import org.apache.hadoop.lib.service.Instrumentation; //导入方法依赖的package包/类

@Test

@TestDir

@SuppressWarnings("unchecked")

public void service() throws Exception {

String dir = TestDirHelper.getTestDir().getAbsolutePath();

String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));

Configuration conf = new Configuration(false);

conf.set("server.services", services);

Server server = new Server("server", dir, dir, dir, dir, conf);

server.init();

Instrumentation instrumentation = server.get(Instrumentation.class);

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 var = new Instrumentation.Variable() {

@Override

public String getValue() {

return "foo";

}

};

instrumentation.addVariable("g", "v", var);

Instrumentation.Variable varToSample = new Instrumentation.Variable() {

@Override

public Long getValue() {

return 1L;

}

};

instrumentation.addSampler("g", "s", 10, varToSample);

Map snapshot = instrumentation.getSnapshot();

assertNotNull(snapshot.get("os-env"));

assertNotNull(snapshot.get("sys-props"));

assertNotNull(snapshot.get("jvm"));

assertNotNull(snapshot.get("counters"));

assertNotNull(snapshot.get("timers"));

assertNotNull(snapshot.get("variables"));

assertNotNull(snapshot.get("samplers"));

assertNotNull(((Map) snapshot.get("os-env")).get("PATH"));

assertNotNull(((Map) snapshot.get("sys-props")).get("java.version"));

assertNotNull(((Map) snapshot.get("jvm")).get("free.memory"));

assertNotNull(((Map) snapshot.get("jvm")).get("max.memory"));

assertNotNull(((Map) snapshot.get("jvm")).get("total.memory"));

assertNotNull(((Map>) snapshot.get("counters")).get("g"));

assertNotNull(((Map>) snapshot.get("timers")).get("g"));

assertNotNull(((Map>) snapshot.get("variables")).get("g"));

assertNotNull(((Map>) snapshot.get("samplers")).get("g"));

assertNotNull(((Map>) snapshot.get("counters")).get("g").get("c"));

assertNotNull(((Map>) snapshot.get("counters")).get("g").get("c1"));

assertNotNull(((Map>) snapshot.get("timers")).get("g").get("t"));

assertNotNull(((Map>) snapshot.get("variables")).get("g").get("v"));

assertNotNull(((Map>) snapshot.get("samplers")).get("g").get("s"));

StringWriter writer = new StringWriter();

JSONObject.writeJSONString(snapshot, writer);

writer.close();

server.destroy();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值