From 677d0452a781cf02a06f8268d24681a855bb60a2 Mon Sep 17 00:00:00 2001 From: JiaShuo Date: Fri, 5 Jun 2020 20:30:26 +0800 Subject: [PATCH] update test code --- .../xiaomi/infra/pegasus/metrics/Falcon.java | 6 +- .../infra/pegasus/metrics/MetricsPool.java | 4 +- .../infra/pegasus/metrics/Prometheus.java | 15 ++++- .../infra/pegasus/client/TestBasic.java | 61 +++++++++++-------- src/test/resource/pegasus.properties | 3 +- 5 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/xiaomi/infra/pegasus/metrics/Falcon.java b/src/main/java/com/xiaomi/infra/pegasus/metrics/Falcon.java index ce26e5b5..1dc14885 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/metrics/Falcon.java +++ b/src/main/java/com/xiaomi/infra/pegasus/metrics/Falcon.java @@ -55,7 +55,7 @@ public void genJsonsFromMeter(String name, Meter meter, StringBuilder output) falconMetric.counterType = "GAUGE"; falconMetric.metric = name + ".cps-1sec"; - falconMetric.tags = getTableTag(name, defaultTags); + falconMetric.tags = getTableTag(name, defaultTags, "@"); falconMetric.value = meter.getMeanRate(); oneMetricToJson(falconMetric, output); } @@ -66,13 +66,13 @@ public void genJsonsFromHistogram(String name, Histogram hist, StringBuilder out Snapshot s = hist.getSnapshot(); falconMetric.metric = name + ".p99"; - falconMetric.tags = getTableTag(name, defaultTags); + falconMetric.tags = getTableTag(name, defaultTags, "@"); falconMetric.value = s.get99thPercentile(); oneMetricToJson(falconMetric, output); output.append(','); falconMetric.metric = name + ".p999"; - falconMetric.tags = getTableTag(name, defaultTags); + falconMetric.tags = getTableTag(name, defaultTags, "@"); falconMetric.value = s.get999thPercentile(); oneMetricToJson(falconMetric, output); } diff --git a/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java b/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java index 8c7c2601..9623fb6f 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java +++ b/src/main/java/com/xiaomi/infra/pegasus/metrics/MetricsPool.java @@ -43,11 +43,11 @@ public void setHistorgram(String counterName, long value) { registry.histogram(counterName).update(value); } - public static String getTableTag(String counterName, String defaultTags) { + public static String getTableTag(String counterName, String defaultTags, String regex) { if (defaultTags.contains("table=")) { return defaultTags; } - String[] result = counterName.split("@"); + String[] result = counterName.split(regex); if (result.length >= 2) { return defaultTags.equals("") ? ("table=" + result[1]) diff --git a/src/main/java/com/xiaomi/infra/pegasus/metrics/Prometheus.java b/src/main/java/com/xiaomi/infra/pegasus/metrics/Prometheus.java index 0afaa489..1572012f 100644 --- a/src/main/java/com/xiaomi/infra/pegasus/metrics/Prometheus.java +++ b/src/main/java/com/xiaomi/infra/pegasus/metrics/Prometheus.java @@ -8,6 +8,8 @@ import com.codahale.metrics.Snapshot; import io.prometheus.client.Collector; import io.prometheus.client.GaugeMetricFamily; +import io.prometheus.client.exporter.HTTPServer; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -54,7 +56,7 @@ public String updateMetric() { } private void updateQPSMetric(Map.Entry meter, String name) { - Map labels = getLabel(getTableTag(name, defaultTags)); + Map labels = getLabel(getTableTag(name, defaultTags, ":")); if (!metrics.containsKey(name)) { // assert labels != null; GaugeMetricFamily labeledGauge = @@ -67,7 +69,7 @@ private void updateQPSMetric(Map.Entry meter, String name) { } private void updateLatencyMetric(Map.Entry meter, String name) { - Map labels = getLabel(getTableTag(name, defaultTags)); + Map labels = getLabel(getTableTag(name, defaultTags, ":")); ArrayList labelList = new ArrayList<>(labels.values()); Snapshot snapshot = meter.getValue().getSnapshot(); double value = @@ -104,7 +106,14 @@ private String format(String name) { } public void start() { - collectorTask.scheduleAtFixedRate(() -> updateMetric(), 0, 10, TimeUnit.SECONDS); + try { + register(); + new HTTPServer(9091); + collectorTask.scheduleAtFixedRate(this::updateMetric, 0, 10, TimeUnit.SECONDS); + System.out.println("XXXX"); + } catch (IOException e) { + e.printStackTrace(); + } } public void stop() { diff --git a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java index 86aa56aa..2ccebe42 100644 --- a/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java +++ b/src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java @@ -136,44 +136,53 @@ public void testSetGetDel() throws PException { try { // set - client.set( - tableName, hashKey, "basic_test_sort_key_1".getBytes(), "basic_test_value_1".getBytes()); + while (true) { + Thread.sleep(1000); - // check exist - boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertTrue(exist); + client.set( + tableName, + hashKey, + "basic_test_sort_key_1".getBytes(), + "basic_test_value_1".getBytes()); - exist = client.exist(tableName, hashKey, "basic_test_sort_key_2".getBytes()); - Assert.assertFalse(exist); + // check exist + boolean exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertTrue(exist); - // check sortkey count - long sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(1, sortKeyCount); + exist = client.exist(tableName, hashKey, "basic_test_sort_key_2".getBytes()); + Assert.assertFalse(exist); - // get - byte[] value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertArrayEquals("basic_test_value_1".getBytes(), value); + // check sortkey count + long sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(1, sortKeyCount); - value = client.get(tableName, hashKey, "basic_test_sort_key_2".getBytes()); - Assert.assertEquals(null, value); + // get + byte[] value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertArrayEquals("basic_test_value_1".getBytes(), value); - // del - client.del(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + value = client.get(tableName, hashKey, "basic_test_sort_key_2".getBytes()); + Assert.assertEquals(null, value); - // check exist - exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertFalse(exist); + // del + client.del(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - // check sortkey count - sortKeyCount = client.sortKeyCount(tableName, hashKey); - Assert.assertEquals(0, sortKeyCount); + // check exist + exist = client.exist(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertFalse(exist); - // check deleted - value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); - Assert.assertEquals(null, value); + // check sortkey count + sortKeyCount = client.sortKeyCount(tableName, hashKey); + Assert.assertEquals(0, sortKeyCount); + + // check deleted + value = client.get(tableName, hashKey, "basic_test_sort_key_1".getBytes()); + Assert.assertEquals(null, value); + } } catch (PException e) { e.printStackTrace(); Assert.assertTrue(false); + } catch (InterruptedException e) { + e.printStackTrace(); } PegasusClientFactory.closeSingletonClient(); diff --git a/src/test/resource/pegasus.properties b/src/test/resource/pegasus.properties index 6282eea7..ec4da28e 100644 --- a/src/test/resource/pegasus.properties +++ b/src/test/resource/pegasus.properties @@ -1,6 +1,7 @@ meta_servers = 127.0.0.1:34601,127.0.0.1:34602,127.0.0.1:34603 operation_timeout = 10000 async_workers = 2 -enable_perf_counter = false +enable_perf_counter = true +push_counter_type = prometheus perf_counter_tags = cluster=onebox,app=unit_test push_counter_interval_secs = 10