BufferPoolMetricSet implements MetricSet { @Override public Map<MetricName, Metric> getMetrics() { final Map<MetricName, Metric> gauges = new HashMap<MetricName, Metric>(); for (String pool : POOLS) { for (int i = 0; i < ATTRIBUTES.length; i++) { final String attribute = ATTRIBUTES[i]; final String name = NAMES[i]; try { final ObjectName on = new ObjectName("java.nio:type=BufferPool,name=" + pool); mBeanServerConnection.getMBeanInfo(on); gauges.put(MetricRegistry.name(pool, name), new JmxAttributeGauge(mBeanServerConnection, on, attribute)); } catch (JMException ignored) { LOGGER.debug("Unable to load buffer pool MBeans, possibly running on Java 6"); } catch (IOException ioException) { LOGGER.debug("Unable to load buffer pool MBeans, an exception occur, cause of", ioException); } } } return Collections.unmodifiableMap(gauges); } BufferPoolMetricSet(MBeanServerConnection mBeanServerConnection); @Override Map<MetricName, Metric> getMetrics(); @Override long lastUpdateTime();  }
@Test public void includesGaugesForDirectAndMappedPools() throws Exception { assertThat(buffers.getMetrics().keySet()) .containsOnly(DIRECT_COUNT, DIRECT_USED, DIRECT_CAPACITY, MAPPED_COUNT, MAPPED_USED, MAPPED_CAPACITY); }
@Test public void ignoresGaugesForObjectsWhichCannotBeFound() throws Exception { when(mBeanServer.getMBeanInfo(mapped)).thenThrow(new InstanceNotFoundException()); assertThat(buffers.getMetrics().keySet()) .containsOnly(DIRECT_COUNT, DIRECT_USED, DIRECT_CAPACITY); }
@Test public void includesAGaugeForDirectCount() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(DIRECT_COUNT); when(mBeanServer.getAttribute(direct, "Count")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForDirectMemoryUsed() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(DIRECT_USED); when(mBeanServer.getAttribute(direct, "MemoryUsed")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForDirectCapacity() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(DIRECT_CAPACITY); when(mBeanServer.getAttribute(direct, "TotalCapacity")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedCount() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(MAPPED_COUNT); when(mBeanServer.getAttribute(mapped, "Count")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedMemoryUsed() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(MAPPED_USED); when(mBeanServer.getAttribute(mapped, "MemoryUsed")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }
@Test public void includesAGaugeForMappedCapacity() throws Exception { final Gauge gauge = (Gauge) buffers.getMetrics().get(MAPPED_CAPACITY); when(mBeanServer.getAttribute(mapped, "TotalCapacity")).thenReturn(100); assertThat(gauge.getValue()) .isEqualTo(100); }