Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
update test code
Browse files Browse the repository at this point in the history
  • Loading branch information
foreverneverer committed Jun 5, 2020
1 parent 2421673 commit 677d045
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/xiaomi/infra/pegasus/metrics/Falcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/xiaomi/infra/pegasus/metrics/Prometheus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +56,7 @@ public String updateMetric() {
}

private void updateQPSMetric(Map.Entry<String, Meter> meter, String name) {
Map<String, String> labels = getLabel(getTableTag(name, defaultTags));
Map<String, String> labels = getLabel(getTableTag(name, defaultTags, ":"));
if (!metrics.containsKey(name)) {
// assert labels != null;
GaugeMetricFamily labeledGauge =
Expand All @@ -67,7 +69,7 @@ private void updateQPSMetric(Map.Entry<String, Meter> meter, String name) {
}

private void updateLatencyMetric(Map.Entry<String, Histogram> meter, String name) {
Map<String, String> labels = getLabel(getTableTag(name, defaultTags));
Map<String, String> labels = getLabel(getTableTag(name, defaultTags, ":"));
ArrayList<String> labelList = new ArrayList<>(labels.values());
Snapshot snapshot = meter.getValue().getSnapshot();
double value =
Expand Down Expand Up @@ -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() {
Expand Down
61 changes: 35 additions & 26 deletions src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/test/resource/pegasus.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 677d045

Please sign in to comment.