Counters extends AbstractCounters<Counters.Counter, Counters.Group> { public synchronized Counter findCounter(String group, String name) { if (name.equals("MAP_INPUT_BYTES")) { LOG.warn("Counter name MAP_INPUT_BYTES is deprecated. " + "Use FileInputFormatCounters as group name and " + " BYTES_READ as counter name instead"); return findCounter(FileInputFormatCounter.BYTES_READ); } String newGroupKey = getNewGroupKey(group); if (newGroupKey != null) { group = newGroupKey; } return getGroup(group).getCounterForName(name); } Counters();  Counters(org.apache.hadoop.mapreduce.Counters newCounters); synchronized Group getGroup(String groupName); @SuppressWarnings("unchecked") synchronized Collection<String> getGroupNames(); synchronized String makeCompactString(); synchronized Counter findCounter(String group, String name); @Deprecated Counter findCounter(String group, int id, String name); void incrCounter(Enum<?> key, long amount); void incrCounter(String group, String counter, long amount); synchronized long getCounter(Enum<?> key); synchronized void incrAllCounters(Counters other); int size(); static Counters sum(Counters a, Counters b); void log(Logger log); String makeEscapedCompactString(); static Counters fromEscapedCompactString(String compactString); static final int MAX_COUNTER_LIMIT; static final int MAX_GROUP_LIMIT; }
@SuppressWarnings("deprecation") @Test public void testCounterValue() { Counters counters = new Counters(); 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 = counters.findCounter("foo", "bar"); counter.setValue(initValue); 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()); } }
@SuppressWarnings("rawtypes") @Test public void testFrameworkCounter() { GroupFactory groupFactory = new GroupFactoryForTest(); FrameworkGroupFactory frameworkGroupFactory = groupFactory.newFrameworkGroupFactory(JobCounter.class); Group group = (Group) frameworkGroupFactory.newGroup("JobCounter"); FrameworkCounterGroup counterGroup = (FrameworkCounterGroup) group.getUnderlyingGroup(); org.apache.hadoop.mapreduce.Counter count1 = counterGroup.findCounter(JobCounter.NUM_FAILED_MAPS.toString()); Assert.assertNotNull(count1); org.apache.hadoop.mapreduce.Counter count2 = counterGroup.findCounter("Unknown"); Assert.assertNull(count2); }
@SuppressWarnings("rawtypes") @Test public void testTaskCounter() { GroupFactory groupFactory = new GroupFactoryForTest(); FrameworkGroupFactory frameworkGroupFactory = groupFactory.newFrameworkGroupFactory(TaskCounter.class); Group group = (Group) frameworkGroupFactory.newGroup("TaskCounter"); FrameworkCounterGroup counterGroup = (FrameworkCounterGroup) group.getUnderlyingGroup(); org.apache.hadoop.mapreduce.Counter count1 = counterGroup.findCounter( TaskCounter.PHYSICAL_MEMORY_BYTES.toString()); Assert.assertNotNull(count1); count1.increment(10); count1.increment(10); Assert.assertEquals(20, count1.getValue()); org.apache.hadoop.mapreduce.Counter count2 = counterGroup.findCounter( TaskCounter.MAP_PHYSICAL_MEMORY_BYTES_MAX.toString()); Assert.assertNotNull(count2); count2.increment(5); count2.increment(10); Assert.assertEquals(10, count2.getValue()); }
@Test public void testFilesystemCounter() { GroupFactory groupFactory = new GroupFactoryForTest(); Group fsGroup = groupFactory.newFileSystemGroup(); org.apache.hadoop.mapreduce.Counter count1 = fsGroup.findCounter("ANY_BYTES_READ"); Assert.assertNotNull(count1); org.apache.hadoop.mapreduce.Counter count2 = fsGroup.findCounter("Unknown"); Assert.assertNull(count2); }