mirror of
https://github.com/wassname/ray.git
synced 2026-07-02 14:32:01 +08:00
[Metrics]Ray java worker metric registry (#9636)
* ray worker metrics gauge init * ray java metric mapping * add jni source files for gauge and tagkey * mapping all metric classes to stats object * check non-null for tags and name * lint * add symbol for native metric JNI * extern c for symbol * add tests for all metrics * Update Metric.java use metricNativePointer instead. * unify metric native stuff to one class * fix jni file * add comments for metric transform function in jni utils * move metric function to native metric file * remove unused disconnect jni * Add a metric registry for java metircs * Restore install-bazel.sh * Add some comments for metric registry * Fix thread safe problem of metrics * Fix metric tests and remove sleep code from tests * Fix comments of metrics Co-authored-by: lingxuan.zlx <skyzlxuan@gmail.com>
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
package io.ray.test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.ray.runtime.metric.Count;
|
||||
import io.ray.runtime.metric.Gauge;
|
||||
import io.ray.runtime.metric.Histogram;
|
||||
import io.ray.runtime.metric.MetricConfig;
|
||||
import io.ray.runtime.metric.Metrics;
|
||||
import io.ray.runtime.metric.Sum;
|
||||
import io.ray.runtime.metric.TagKey;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
public class MetricTest extends BaseTest {
|
||||
@@ -20,6 +23,60 @@ public class MetricTest extends BaseTest {
|
||||
return value <= other + 1e-5 && value >= other - 1e-5;
|
||||
}
|
||||
|
||||
private MetricConfig initRayMetrics(long timeIntervalMs,
|
||||
int threadPoolSize,
|
||||
long shutdownWaitTimeMs) {
|
||||
MetricConfig config = MetricConfig.builder()
|
||||
.timeIntervalMs(timeIntervalMs)
|
||||
.threadPoolSize(threadPoolSize)
|
||||
.shutdownWaitTimeMs(shutdownWaitTimeMs)
|
||||
.create();
|
||||
Metrics.init(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private Gauge registerGauge() {
|
||||
return Metrics.gauge()
|
||||
.name("metric_gauge")
|
||||
.description("gauge")
|
||||
.unit("")
|
||||
.tags(ImmutableMap.of("tag1", "value1"))
|
||||
.register();
|
||||
}
|
||||
|
||||
private Count registerCount() {
|
||||
return Metrics.count()
|
||||
.name("metric_count")
|
||||
.description("counter")
|
||||
.unit("1pc")
|
||||
.tags(ImmutableMap.of("tag1", "value1", "count_tag", "default"))
|
||||
.register();
|
||||
}
|
||||
|
||||
private Sum registerSum() {
|
||||
return Metrics.sum()
|
||||
.name("metric_sum")
|
||||
.description("sum")
|
||||
.unit("1pc")
|
||||
.tags(ImmutableMap.of("tag1", "value1", "sum_tag", "default"))
|
||||
.register();
|
||||
}
|
||||
|
||||
private Histogram registerHistogram() {
|
||||
return Metrics.histogram()
|
||||
.name("metric_histogram")
|
||||
.description("histogram")
|
||||
.unit("1pc")
|
||||
.boundaries(ImmutableList.of(10.0, 15.0, 20.0))
|
||||
.tags(ImmutableMap.of("tag1", "value1", "histogram_tag", "default"))
|
||||
.register();
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void maybeShutdownMetrics() {
|
||||
Metrics.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddGauge() {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
@@ -44,7 +101,6 @@ public class MetricTest extends BaseTest {
|
||||
count.inc(10.0);
|
||||
count.inc(20.0);
|
||||
count.record();
|
||||
Assert.assertTrue(doubleEqual(count.getValue(), 20.0));
|
||||
Assert.assertTrue(doubleEqual(count.getCount(), 30.0));
|
||||
}
|
||||
|
||||
@@ -59,7 +115,6 @@ public class MetricTest extends BaseTest {
|
||||
sum.update(10.0);
|
||||
sum.update(20.0);
|
||||
sum.record();
|
||||
Assert.assertTrue(doubleEqual(sum.getValue(), 20.0));
|
||||
Assert.assertTrue(doubleEqual(sum.getSum(), 30.0));
|
||||
}
|
||||
|
||||
@@ -74,14 +129,128 @@ public class MetricTest extends BaseTest {
|
||||
boundaries.add(15.0);
|
||||
boundaries.add(12.0);
|
||||
Histogram histogram = new Histogram("metric_histogram", "histogram", "1pc",
|
||||
boundaries, tags);
|
||||
boundaries, tags);
|
||||
for (int i = 1; i <= 200; ++i) {
|
||||
histogram.update(i * 1.0d);
|
||||
histogram.record();
|
||||
}
|
||||
Assert.assertTrue(doubleEqual(200.0d, histogram.getValue()));
|
||||
List<Double> window = histogram.getHistogramWindow();
|
||||
for (int i = 0; i < Histogram.HISTOGRAM_WINDOW_SIZE; ++i) {
|
||||
Assert.assertTrue(doubleEqual(i + 101.0d, window.get(i)));
|
||||
}
|
||||
histogram.record();
|
||||
Assert.assertTrue(doubleEqual(200.0d, histogram.getValue()));
|
||||
Assert.assertEquals(window.size(), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterGauge() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
Gauge gauge = registerGauge();
|
||||
|
||||
gauge.update(2.0);
|
||||
Assert.assertTrue(doubleEqual(gauge.getValue(), 2.0));
|
||||
gauge.update(5.0);
|
||||
Assert.assertTrue(doubleEqual(gauge.getValue(), 5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterCount() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
Count count = registerCount();
|
||||
|
||||
count.inc(10.0);
|
||||
count.inc(20.0);
|
||||
Assert.assertTrue(doubleEqual(count.getCount(), 30.0));
|
||||
count.inc(1.0);
|
||||
count.inc(2.0);
|
||||
Assert.assertTrue(doubleEqual(count.getCount(), 33.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterSum() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
Sum sum = registerSum();
|
||||
|
||||
sum.update(10.0);
|
||||
sum.update(20.0);
|
||||
Assert.assertTrue(doubleEqual(sum.getSum(), 30.0));
|
||||
sum.update(1.0);
|
||||
sum.update(2.0);
|
||||
Assert.assertTrue(doubleEqual(sum.getSum(), 33.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterHistogram() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
Histogram histogram = registerHistogram();
|
||||
|
||||
for (int i = 1; i <= 200; ++i) {
|
||||
histogram.update(i * 1.0d);
|
||||
}
|
||||
Assert.assertTrue(doubleEqual(histogram.getValue(), 200.0d));
|
||||
List<Double> window = histogram.getHistogramWindow();
|
||||
for (int i = 0; i < Histogram.HISTOGRAM_WINDOW_SIZE; ++i) {
|
||||
Assert.assertTrue(doubleEqual(i + 101.0d, window.get(i)));
|
||||
}
|
||||
Assert.assertTrue(doubleEqual(histogram.getValue(), 200.0d));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterGaugeWithConfig() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
initRayMetrics(2000L, 1, 1000L);
|
||||
Gauge gauge = registerGauge();
|
||||
|
||||
gauge.update(2.0);
|
||||
Assert.assertTrue(doubleEqual(gauge.getValue(), 2.0));
|
||||
gauge.update(5.0);
|
||||
Assert.assertTrue(doubleEqual(gauge.getValue(), 5.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterCountWithConfig() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
initRayMetrics(2000L, 1, 1000L);
|
||||
Count count = registerCount();
|
||||
|
||||
count.inc(10.0);
|
||||
count.inc(20.0);
|
||||
Assert.assertTrue(doubleEqual(count.getCount(), 30.0));
|
||||
count.inc(1.0);
|
||||
count.inc(2.0);
|
||||
Assert.assertTrue(doubleEqual(count.getCount(), 33.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterSumWithConfig() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
initRayMetrics(2000L, 1, 1000L);
|
||||
Sum sum = registerSum();
|
||||
|
||||
sum.update(10.0);
|
||||
sum.update(20.0);
|
||||
Assert.assertTrue(doubleEqual(sum.getSum(), 30.0));
|
||||
sum.update(1.0);
|
||||
sum.update(2.0);
|
||||
Assert.assertTrue(doubleEqual(sum.getSum(), 33.0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterHistogramWithConfig() throws InterruptedException {
|
||||
TestUtils.skipTestUnderSingleProcess();
|
||||
initRayMetrics(2000L, 1, 1000L);
|
||||
Histogram histogram = registerHistogram();
|
||||
|
||||
for (int i = 1; i <= 200; ++i) {
|
||||
histogram.update(i * 1.0d);
|
||||
}
|
||||
Assert.assertTrue(doubleEqual(histogram.getValue(), 200.0d));
|
||||
List<Double> window = histogram.getHistogramWindow();
|
||||
for (int i = 0; i < Histogram.HISTOGRAM_WINDOW_SIZE; ++i) {
|
||||
Assert.assertTrue(doubleEqual(i + 101.0d, window.get(i)));
|
||||
}
|
||||
Assert.assertTrue(doubleEqual(histogram.getValue(), 200.0d));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user