Search in sources :

Example 6 with Instrumentation

use of org.apache.hadoop.lib.service.Instrumentation in project hadoop by apache.

the class TestInstrumentationService method service.

@Test
@TestDir
@SuppressWarnings("unchecked")
public void service() throws Exception {
    String dir = TestDirHelper.getTestDir().getAbsolutePath();
    String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
    Configuration conf = new Configuration(false);
    conf.set("server.services", services);
    Server server = new Server("server", dir, dir, dir, dir, conf);
    server.init();
    Instrumentation instrumentation = server.get(Instrumentation.class);
    assertNotNull(instrumentation);
    instrumentation.incr("g", "c", 1);
    instrumentation.incr("g", "c", 2);
    instrumentation.incr("g", "c1", 2);
    Instrumentation.Cron cron = instrumentation.createCron();
    cron.start();
    sleep(100);
    cron.stop();
    instrumentation.addCron("g", "t", cron);
    cron = instrumentation.createCron();
    cron.start();
    sleep(200);
    cron.stop();
    instrumentation.addCron("g", "t", cron);
    Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {

        @Override
        public String getValue() {
            return "foo";
        }
    };
    instrumentation.addVariable("g", "v", var);
    Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {

        @Override
        public Long getValue() {
            return 1L;
        }
    };
    instrumentation.addSampler("g", "s", 10, varToSample);
    Map<String, ?> snapshot = instrumentation.getSnapshot();
    assertNotNull(snapshot.get("os-env"));
    assertNotNull(snapshot.get("sys-props"));
    assertNotNull(snapshot.get("jvm"));
    assertNotNull(snapshot.get("counters"));
    assertNotNull(snapshot.get("timers"));
    assertNotNull(snapshot.get("variables"));
    assertNotNull(snapshot.get("samplers"));
    assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
    assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
    assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
    assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));
    StringWriter writer = new StringWriter();
    JSONObject.writeJSONString(snapshot, writer);
    writer.close();
    server.destroy();
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) Server(org.apache.hadoop.lib.server.Server) Instrumentation(org.apache.hadoop.lib.service.Instrumentation) StringWriter(java.io.StringWriter) JSONObject(org.json.simple.JSONObject) Map(java.util.Map) TestDir(org.apache.hadoop.test.TestDir) Test(org.junit.Test)

Aggregations

Instrumentation (org.apache.hadoop.lib.service.Instrumentation)6 Map (java.util.Map)3 JSONObject (org.json.simple.JSONObject)3 IOException (java.io.IOException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Configuration (org.apache.hadoop.conf.Configuration)2 FileSystem (org.apache.hadoop.fs.FileSystem)2 Server (org.apache.hadoop.lib.server.Server)2 ServiceException (org.apache.hadoop.lib.server.ServiceException)2 FileSystemAccessException (org.apache.hadoop.lib.service.FileSystemAccessException)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 TestDir (org.apache.hadoop.test.TestDir)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 AccessControlException (java.security.AccessControlException)1 List (java.util.List)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1