1
18
19 package orca
20
21 import (
22 "testing"
23
24 "github.com/google/go-cmp/cmp"
25 "google.golang.org/grpc/internal/grpctest"
26 )
27
28 type s struct {
29 grpctest.Tester
30 }
31
32 func Test(t *testing.T) {
33 grpctest.RunSubTests(t, s{})
34 }
35
36 func (s) TestServerMetrics_Setters(t *testing.T) {
37 smr := NewServerMetricsRecorder()
38
39 smr.SetCPUUtilization(0.1)
40 smr.SetMemoryUtilization(0.2)
41 smr.SetApplicationUtilization(0.3)
42 smr.SetQPS(0.4)
43 smr.SetEPS(0.5)
44 smr.SetNamedUtilization("x", 0.6)
45
46 want := &ServerMetrics{
47 CPUUtilization: 0.1,
48 MemUtilization: 0.2,
49 AppUtilization: 0.3,
50 QPS: 0.4,
51 EPS: 0.5,
52 Utilization: map[string]float64{"x": 0.6},
53 NamedMetrics: map[string]float64{},
54 RequestCost: map[string]float64{},
55 }
56
57 got := smr.ServerMetrics()
58 if d := cmp.Diff(got, want); d != "" {
59 t.Fatalf("unexpected server metrics: -got +want: %v", d)
60 }
61 }
62
63 func (s) TestServerMetrics_Deleters(t *testing.T) {
64 smr := NewServerMetricsRecorder()
65
66 smr.SetCPUUtilization(0.1)
67 smr.SetMemoryUtilization(0.2)
68 smr.SetApplicationUtilization(0.3)
69 smr.SetQPS(0.4)
70 smr.SetEPS(0.5)
71 smr.SetNamedUtilization("x", 0.6)
72 smr.SetNamedUtilization("y", 0.7)
73
74
75 smr.DeleteCPUUtilization()
76 smr.DeleteMemoryUtilization()
77 smr.DeleteApplicationUtilization()
78 smr.DeleteQPS()
79 smr.DeleteEPS()
80 smr.DeleteNamedUtilization("x")
81
82 want := &ServerMetrics{
83 CPUUtilization: -1,
84 MemUtilization: -1,
85 AppUtilization: -1,
86 QPS: -1,
87 EPS: -1,
88 Utilization: map[string]float64{"y": 0.7},
89 NamedMetrics: map[string]float64{},
90 RequestCost: map[string]float64{},
91 }
92
93 got := smr.ServerMetrics()
94 if d := cmp.Diff(got, want); d != "" {
95 t.Fatalf("unexpected server metrics: -got +want: %v", d)
96 }
97 }
98
99 func (s) TestServerMetrics_Setters_Range(t *testing.T) {
100 smr := NewServerMetricsRecorder()
101
102 smr.SetCPUUtilization(0.1)
103 smr.SetMemoryUtilization(0.2)
104 smr.SetApplicationUtilization(0.3)
105 smr.SetQPS(0.4)
106 smr.SetEPS(0.5)
107 smr.SetNamedUtilization("x", 0.6)
108
109
110 smr.SetCPUUtilization(-2)
111 smr.SetMemoryUtilization(-3)
112 smr.SetApplicationUtilization(-4)
113 smr.SetQPS(-0.1)
114 smr.SetEPS(-0.6)
115 smr.SetNamedUtilization("x", -2)
116
117
118 smr.SetMemoryUtilization(1.1)
119 smr.SetNamedUtilization("x", 1.1)
120
121 want := &ServerMetrics{
122 CPUUtilization: 0.1,
123 MemUtilization: 0.2,
124 AppUtilization: 0.3,
125 QPS: 0.4,
126 EPS: 0.5,
127 Utilization: map[string]float64{"x": 0.6},
128 NamedMetrics: map[string]float64{},
129 RequestCost: map[string]float64{},
130 }
131
132 got := smr.ServerMetrics()
133 if d := cmp.Diff(got, want); d != "" {
134 t.Fatalf("unexpected server metrics: -got +want: %v", d)
135 }
136 }
137
138 func (s) TestServerMetrics_Merge(t *testing.T) {
139 sm1 := &ServerMetrics{
140 CPUUtilization: 0.1,
141 MemUtilization: 0.2,
142 AppUtilization: 0.3,
143 QPS: -1,
144 EPS: 0,
145 Utilization: map[string]float64{"x": 0.6},
146 NamedMetrics: map[string]float64{"y": 0.2},
147 RequestCost: map[string]float64{"a": 0.1},
148 }
149
150 sm2 := &ServerMetrics{
151 CPUUtilization: -1,
152 AppUtilization: 0,
153 QPS: 0.9,
154 EPS: 20,
155 Utilization: map[string]float64{"x": 0.5, "y": 0.4},
156 NamedMetrics: map[string]float64{"x": 0.1},
157 RequestCost: map[string]float64{"a": 0.2},
158 }
159
160 want := &ServerMetrics{
161 CPUUtilization: 0.1,
162 MemUtilization: 0,
163 AppUtilization: 0,
164 QPS: 0.9,
165 EPS: 20,
166 Utilization: map[string]float64{"x": 0.5, "y": 0.4},
167 NamedMetrics: map[string]float64{"x": 0.1, "y": 0.2},
168 RequestCost: map[string]float64{"a": 0.2},
169 }
170
171 sm1.merge(sm2)
172 if d := cmp.Diff(sm1, want); d != "" {
173 t.Fatalf("unexpected server metrics: -got +want: %v", d)
174 }
175 }
176
View as plain text