ZpeMetric { ZpeMetric() { File directory = new File(String.valueOf(getFilePath())); directory.mkdir(); if (statsEnabled) { Integer interval = Integer.parseInt(System.getProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL, ZPE_WRITE_INTERVAL)); Timer timer = FETCH_TIMER; if (timer == null) { synchronized (TIMER_LOCK) { timer = FETCH_TIMER; if (timer == null) { timer = new Timer(); timer.schedule(new SchedulerService(), interval, interval); FETCH_TIMER = timer; } } } } } ZpeMetric(); void increment(String metricName, String domainName); DomainMetrics getMetrics(String domainName); void writeToFile(); static final String ZPE_METRIC_FILE_PATH; static final String ZPE_WRITE_INTERVAL; public ConcurrentHashMap<String, AtomicIntegerArray> counter; }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void testZpeMetric() throws IOException { System.setProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL, "5000"); final String metricDirPath = "/tmp/zpe-metrics"; File metricsDir = new File(metricDirPath); metricsDir.mkdirs(); System.setProperty(ZpeConsts.ZPE_PROP_METRIC_FILE_PATH, metricDirPath); final String TEST_DOMAIN = "test"; ZpeMetric.statsEnabled = true; ZpeMetric test = new ZpeMetric(); File dir = new File(test.getFilePath()); if (dir.exists()) { File[] files = dir.listFiles(); if (files != null) { for (File file : files) { if (!file.isDirectory()) { file.delete(); } } } } else { dir.mkdirs(); } test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); try { Thread.sleep(4000); } catch (InterruptedException ignored) { } test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN); try { Thread.sleep(2000); } catch (InterruptedException ignored) { } boolean sysDomainMetricVerified = false; boolean testDomainMetricVerified = false; File[] files = dir.listFiles(); assertNotNull(files); for (File file : files) { String filepath = test.getFilePath() + file.getName(); Path path = Paths.get(filepath); DomainMetrics domainMetrics = JSON.fromBytes(Files.readAllBytes(path), DomainMetrics.class); List<DomainMetric> metricList = domainMetrics.getMetricList(); for (DomainMetric metric : metricList) { if (metric.getMetricType().toString().equals(ZpeConsts.ZPE_METRIC_NAME)) { if (domainMetrics.getDomainName().equals("sys.auth")) { assertEquals(10, metric.getMetricVal()); sysDomainMetricVerified = true; } else if (domainMetrics.getDomainName().equals("test")) { assertEquals(2, metric.getMetricVal()); testDomainMetricVerified = true; } } } } assertTrue(sysDomainMetricVerified); assertTrue(testDomainMetricVerified); System.clearProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL); }