-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
CallMetricRecorderTest.java
111 lines (98 loc) · 3.91 KB
/
CallMetricRecorderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2019 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.services;
import static com.google.common.truth.Truth.assertThat;
import com.google.common.truth.Truth;
import io.grpc.Context;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link CallMetricRecorder}. */
@RunWith(JUnit4.class)
public class CallMetricRecorderTest {
private final CallMetricRecorder recorder = new CallMetricRecorder();
@Test
public void dumpGivesEmptyResultWhenNoSavedMetricValues() {
assertThat(recorder.finalizeAndDump()).isEmpty();
}
@Test
public void dumpDumpsAllSavedMetricValues() {
recorder.recordUtilizationMetric("util1", 154353.423);
recorder.recordUtilizationMetric("util2", 0.1367);
recorder.recordUtilizationMetric("util3", 1437.34);
recorder.recordCallMetric("cost1", 37465.12);
recorder.recordCallMetric("cost2", 10293.0);
recorder.recordCallMetric("cost3", 1.0);
recorder.recordCpuUtilizationMetric(0.1928);
recorder.recordMemoryUtilizationMetric(47.4);
MetricReport dump = recorder.finalizeAndDump2();
Truth.assertThat(dump.getUtilizationMetrics())
.containsExactly("util1", 154353.423, "util2", 0.1367, "util3", 1437.34);
Truth.assertThat(dump.getRequestCostMetrics())
.containsExactly("cost1", 37465.12, "cost2", 10293.0, "cost3", 1.0);
Truth.assertThat(dump.getCpuUtilization()).isEqualTo(0.1928);
Truth.assertThat(dump.getMemoryUtilization()).isEqualTo(47.4);
}
@Test
public void noMetricsRecordedAfterSnapshot() {
Map<String, Double> initDump = recorder.finalizeAndDump();
recorder.recordUtilizationMetric("cost", 154353.423);
assertThat(recorder.finalizeAndDump()).isEqualTo(initDump);
}
@Test
public void lastValueWinForMetricsWithSameName() {
recorder.recordCallMetric("cost1", 3412.5435);
recorder.recordCallMetric("cost2", 6441.341);
recorder.recordCallMetric("cost1", 6441.341);
recorder.recordCallMetric("cost1", 4654.67);
recorder.recordCallMetric("cost2", 75.83);
recorder.recordMemoryUtilizationMetric(1.3);
recorder.recordMemoryUtilizationMetric(3.1);
recorder.recordUtilizationMetric("util1", 28374.21);
recorder.recordMemoryUtilizationMetric(9384.0);
recorder.recordUtilizationMetric("util1", 84323.3);
MetricReport dump = recorder.finalizeAndDump2();
Truth.assertThat(dump.getRequestCostMetrics())
.containsExactly("cost1", 4654.67, "cost2", 75.83);
Truth.assertThat(dump.getMemoryUtilization()).isEqualTo(9384.0);
Truth.assertThat(dump.getUtilizationMetrics())
.containsExactly("util1", 84323.3);
Truth.assertThat(dump.getCpuUtilization()).isEqualTo(0);
}
@Test
public void getCurrent_sameEnabledInstance() {
CallMetricRecorder recorder = new CallMetricRecorder();
Context ctx = Context.ROOT.withValue(CallMetricRecorder.CONTEXT_KEY, recorder);
Context origCtx = ctx.attach();
try {
assertThat(CallMetricRecorder.getCurrent()).isSameInstanceAs(recorder);
assertThat(recorder.isDisabled()).isFalse();
} finally {
ctx.detach(origCtx);
}
}
@Test
public void getCurrent_blankContext() {
Context blankCtx = Context.ROOT;
Context origCtx = blankCtx.attach();
try {
assertThat(CallMetricRecorder.getCurrent().isDisabled()).isTrue();
} finally {
blankCtx.detach(origCtx);
}
}
}