InstrumentationService extends BaseService implements Instrumentation { @Override @SuppressWarnings("unchecked") public void init() throws ServiceException { timersSize = getServiceConfig().getInt(CONF_TIMERS_SIZE, 10); counterLock = new ReentrantLock(); timerLock = new ReentrantLock(); variableLock = new ReentrantLock(); samplerLock = new ReentrantLock(); Map<String, VariableHolder> jvmVariables = new ConcurrentHashMap<>(); counters = new ConcurrentHashMap<>(); timers = new ConcurrentHashMap<>(); variables = new ConcurrentHashMap<>(); samplers = new ConcurrentHashMap<>(); samplersList = new ArrayList<>(); all = new LinkedHashMap<>(); all.put("os-env", System.getenv()); all.put("sys-props", (Map<String, ?>) (Map) System.getProperties()); all.put("jvm", jvmVariables); all.put("counters", (Map) counters); all.put("timers", (Map) timers); all.put("variables", (Map) variables); all.put("samplers", (Map) samplers); jvmVariables.put("free.memory", new VariableHolder<>(new Instrumentation.Variable<Long>() { @Override public Long getValue() { return Runtime.getRuntime().freeMemory(); } })); jvmVariables.put("max.memory", new VariableHolder<>(new Instrumentation.Variable<Long>() { @Override public Long getValue() { return Runtime.getRuntime().maxMemory(); } })); jvmVariables.put("total.memory", new VariableHolder<>(new Instrumentation.Variable<Long>() { @Override public Long getValue() { return Runtime.getRuntime().totalMemory(); } })); } InstrumentationService(); @Override @SuppressWarnings("unchecked") void init(); @Override void postInit(); @Override Class getInterface(); @Override Cron createCron(); @Override void incr(String group, String name, long count); @Override void addCron(String group, String name, Instrumentation.Cron cron); @Override void addVariable(String group, String name, Variable<?> variable); @Override void addSampler(String group, String name, int samplingSize,
      Variable<Long> variable); @Override Map<String, Map<String, ?>> getSnapshot(); static final String PREFIX; static final String CONF_TIMERS_SIZE; }
@Test public void sampler() throws Exception { final long value[] = new long[1]; Instrumentation.Variable<Long> var = new Instrumentation.Variable<Long>() { @Override public Long getValue() { return value[0]; } }; InstrumentationService.Sampler sampler = new InstrumentationService.Sampler(); sampler.init(4, var); assertEquals(sampler.getRate(), 0f, 0.0001); sampler.sample(); assertEquals(sampler.getRate(), 0f, 0.0001); value[0] = 1; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1) / 2, 0.0001); value[0] = 2; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1 + 2) / 3, 0.0001); value[0] = 3; sampler.sample(); assertEquals(sampler.getRate(), (0d + 1 + 2 + 3) / 4, 0.0001); value[0] = 4; sampler.sample(); assertEquals(sampler.getRate(), (4d + 1 + 2 + 3) / 4, 0.0001); JSONObject json = (JSONObject) new JSONParser().parse(sampler.toJSONString()); assertEquals(json.size(), 2); assertEquals(json.get("sampler"), sampler.getRate()); assertEquals(json.get("size"), 4L); StringWriter writer = new StringWriter(); sampler.writeJSONString(writer); writer.close(); json = (JSONObject) new JSONParser().parse(writer.toString()); assertEquals(json.size(), 2); assertEquals(json.get("sampler"), sampler.getRate()); assertEquals(json.get("size"), 4L); }