/** * Internal to find a counter in a group. * @param counterName the name of the counter * @param displayName the display name of the counter * @return the counter that was found or added */ protected Counter findCounter(String counterName, String displayName) { Counter result = counters.get(counterName); if (result == null) { result = new Counter(counterName, displayName); counters.put(counterName, result); } return result; }
/** * Find a counter in a group. * @param counterName the name of the counter * @param displayName the display name of the counter * @return the counter that was found or added */ public Counter findCounter(String counterName, String displayName) { Counter result = counters.get(counterName); if (result == null) { result = new Counter(counterName, displayName); counters.put(counterName, result); } return result; }
public synchronized Counter findCounter(String counterName) { Counter result = counters.get(counterName); if (result == null) { String displayName = localize(counterName, counterName); result = new Counter(counterName, displayName); counters.put(counterName, result); } return result; }
public synchronized Counter findCounter(String counterName) { Counter result = counters.get(counterName); if (result == null) { String displayName = localize(counterName, counterName); result = new Counter(counterName, displayName); counters.put(counterName, result); } return result; }
static Counters fromAvro(JhCounters counters) { Counters result = new Counters(); for (JhCounterGroup g : counters.groups) { CounterGroup group = new CounterGroup(g.name.toString(), g.displayName.toString()); for (JhCounter c : g.counts) { group.addCounter(new Counter(c.name.toString(), c.displayName.toString(), c.value)); } result.addGroup(group); } return result; }
public synchronized void readFields(DataInput in) throws IOException { displayName = Text.readString(in); counters.clear(); int size = WritableUtils.readVInt(in); for(int i=0; i < size; i++) { Counter counter = new Counter(); counter.readFields(in); counters.put(counter.getName(), counter); } }
public synchronized void readFields(DataInput in) throws IOException { displayName = Text.readString(in); counters.clear(); int size = WritableUtils.readVInt(in); for(int i=0; i < size; i++) { Counter counter = new Counter(); counter.readFields(in); counters.put(counter.getName(), counter); } }
/** * Verify counter value works */ @Test public void testCounterValue() { final int NUMBER_TESTS = 100; final int NUMBER_INC = 10; final Random rand = new Random(); for (int i = 0; i < NUMBER_TESTS; i++) { long initValue = rand.nextInt(); long expectedValue = initValue; Counter counter = new Counter("foo", "bar", expectedValue); assertEquals("Counter value is not initialized correctly", expectedValue, counter.getValue()); for (int j = 0; j < NUMBER_INC; j++) { int incValue = rand.nextInt(); counter.increment(incValue); expectedValue += incValue; assertEquals("Counter value is not incremented correctly", expectedValue, counter.getValue()); } expectedValue = rand.nextInt(); counter.setValue(expectedValue); assertEquals("Counter value is not set correctly", expectedValue, counter.getValue()); } }