Search in sources :

Example 1 with JmxCounterMBean

use of com.codahale.metrics.jmx.JmxReporter.JmxCounterMBean in project LogHub by fbacchella.

the class TestIntegrated method runStart.

@Test(timeout = 5000)
public void runStart() throws ConfigException, IOException, InterruptedException, IntrospectionException, InstanceNotFoundException, MalformedObjectNameException, ReflectionException {
    loghub.Stats.reset();
    String conffile = Configuration.class.getClassLoader().getResource("test.conf").getFile();
    Start.main(new String[] { "--canexit", "-c", conffile });
    Thread.sleep(500);
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    StatsMBean stats = JMX.newMBeanProxy(mbs, StatsMBean.Implementation.NAME, StatsMBean.class);
    JmxTimerMBean allevents_timer = JMX.newMBeanProxy(mbs, new ObjectName("metrics:name=Allevents.timer"), JmxTimerMBean.class);
    JmxCounterMBean allevents_inflight = JMX.newMBeanProxy(mbs, new ObjectName("metrics:name=Allevents.inflight"), JmxCounterMBean.class);
    Socket sender = tctxt.ctx.newSocket(Method.CONNECT, Type.PUB, "inproc://listener");
    Socket receiver = tctxt.ctx.newSocket(Method.CONNECT, Type.PULL, "inproc://sender");
    AtomicLong send = new AtomicLong();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                for (int i = 0; i < 100 && tctxt.ctx.isRunning(); i++) {
                    sender.send("message " + i);
                    send.incrementAndGet();
                    Thread.sleep(1);
                }
            } catch (InterruptedException e) {
            }
            logger.debug("All events sent");
        }
    };
    t.start();
    Pattern messagePattern = Pattern.compile("\\{\"a\":1,\"b\":\"google-public-dns-a\",\"message\":\"message \\d+\"\\}");
    while (send.get() < 99 || allevents_inflight.getCount() != 0) {
        logger.debug("send: {}, in flight: {}", send.get(), allevents_inflight.getCount());
        while (receiver.getEvents() > 0) {
            logger.debug("in flight: {}", allevents_inflight.getCount());
            String content = receiver.recvStr();
            Assert.assertTrue(messagePattern.matcher(content).find());
            Thread.sleep(1);
        }
        ;
        Thread.sleep(50);
    }
    Thread.sleep(10);
    Set<ObjectName> metrics = mbs.queryNames(new ObjectName("metrics:*"), null);
    long blocked = 0;
    dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=EventWaiting.") && i.toString().endsWith(".failed"), i -> (long) i.getValue(), JmxGaugeMBean.class);
    dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=Pipeline.") && i.toString().endsWith(".failed"), i -> i.getCount(), JmxMeterMBean.class);
    dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=Pipeline.") && i.toString().endsWith(".droped"), i -> i.getCount(), JmxMeterMBean.class);
    blocked += dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=Pipeline.") && i.toString().endsWith(".blocked.in"), i -> i.getCount(), JmxMeterMBean.class);
    blocked += dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=Pipeline.") && i.toString().endsWith(".blocked.out"), i -> i.getCount(), JmxMeterMBean.class);
    dumpstatus(mbs, metrics, i -> i.toString().startsWith("metrics:name=Pipeline.") && i.toString().endsWith(".inflight"), i -> i.getCount(), JmxMeterMBean.class);
    logger.debug("dropped: " + stats.getDropped());
    logger.debug("failed: " + stats.getFailed());
    logger.debug("received: " + stats.getReceived());
    logger.debug("sent: " + stats.getSent());
    logger.debug(Arrays.toString(stats.getErrors()));
    logger.debug(Arrays.toString(stats.getExceptions()));
    long received = stats.getReceived();
    Assert.assertTrue(received > 95);
    Assert.assertEquals(0L, allevents_inflight.getCount());
    Assert.assertEquals(received, allevents_timer.getCount());
    Assert.assertEquals(received, blocked + stats.getSent());
}
Also used : Arrays(java.util.Arrays) IntrospectionException(javax.management.IntrospectionException) BeforeClass(org.junit.BeforeClass) Level(org.apache.logging.log4j.Level) Function(java.util.function.Function) ConfigException(loghub.configuration.ConfigException) MBeanServer(javax.management.MBeanServer) JMX(javax.management.JMX) ManagementFactory(java.lang.management.ManagementFactory) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) Socket(org.zeromq.ZMQ.Socket) JmxCounterMBean(com.codahale.metrics.jmx.JmxReporter.JmxCounterMBean) Type(loghub.zmq.ZMQHelper.Type) Set(java.util.Set) JmxTimerMBean(com.codahale.metrics.jmx.JmxReporter.JmxTimerMBean) IOException(java.io.IOException) Test(org.junit.Test) ObjectName(javax.management.ObjectName) StatsMBean(loghub.jmx.StatsMBean) Method(loghub.zmq.ZMQHelper.Method) MalformedObjectNameException(javax.management.MalformedObjectNameException) AtomicLong(java.util.concurrent.atomic.AtomicLong) JmxMeterMBean(com.codahale.metrics.jmx.JmxReporter.JmxMeterMBean) Logger(org.apache.logging.log4j.Logger) Rule(org.junit.Rule) Configuration(loghub.configuration.Configuration) Pattern(java.util.regex.Pattern) Assert(org.junit.Assert) LogManager(org.apache.logging.log4j.LogManager) JmxGaugeMBean(com.codahale.metrics.jmx.JmxReporter.JmxGaugeMBean) Pattern(java.util.regex.Pattern) StatsMBean(loghub.jmx.StatsMBean) ObjectName(javax.management.ObjectName) JmxTimerMBean(com.codahale.metrics.jmx.JmxReporter.JmxTimerMBean) AtomicLong(java.util.concurrent.atomic.AtomicLong) JmxMeterMBean(com.codahale.metrics.jmx.JmxReporter.JmxMeterMBean) Socket(org.zeromq.ZMQ.Socket) MBeanServer(javax.management.MBeanServer) JmxCounterMBean(com.codahale.metrics.jmx.JmxReporter.JmxCounterMBean) Test(org.junit.Test)

Aggregations

JmxCounterMBean (com.codahale.metrics.jmx.JmxReporter.JmxCounterMBean)1 JmxGaugeMBean (com.codahale.metrics.jmx.JmxReporter.JmxGaugeMBean)1 JmxMeterMBean (com.codahale.metrics.jmx.JmxReporter.JmxMeterMBean)1 JmxTimerMBean (com.codahale.metrics.jmx.JmxReporter.JmxTimerMBean)1 IOException (java.io.IOException)1 ManagementFactory (java.lang.management.ManagementFactory)1 Arrays (java.util.Arrays)1 Set (java.util.Set)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 IntrospectionException (javax.management.IntrospectionException)1 JMX (javax.management.JMX)1 MBeanServer (javax.management.MBeanServer)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 ReflectionException (javax.management.ReflectionException)1 ConfigException (loghub.configuration.ConfigException)1 Configuration (loghub.configuration.Configuration)1