This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 10
/
operation_aggregator_test.cc
506 lines (456 loc) · 13.4 KB
/
operation_aggregator_test.cc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Copyright 2016 Google Inc. All Rights Reserved.
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.
==============================================================================*/
#include "src/operation_aggregator.h"
#include "gmock/gmock.h"
#include "google/protobuf/text_format.h"
#include "google/protobuf/util/message_differencer.h"
#include "gtest/gtest.h"
using ::google::api::MetricDescriptor;
using ::google::api::servicecontrol::v1::Distribution;
using ::google::api::servicecontrol::v1::MetricValue;
using ::google::api::servicecontrol::v1::Operation;
using ::google::protobuf::TextFormat;
using ::google::protobuf::util::MessageDifferencer;
using std::string;
namespace google {
namespace service_control_client {
namespace {
const char kMetric[] = "library.googleapis.com/rpc/client/count";
const char kUSD[] = "USD";
const char kOperation1[] = R"(
operation_id: "some-operation-id"
operation_name: "google.example.library.v1.LibraryService.CreateShelf"
consumer_id: "project:some-consumer"
labels {
key: "library.googleapis.com/resource_type"
value: "book"
}
labels {
key: "library.googleapis.com/resource_id"
value: "projects/123/books/456"
}
labels {
key: "library.googleapis.com/region"
value: "us-central1"
}
labels {
key: "library.googleapis.com/zone"
value: "us-central1-a"
}
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 3000
nanos: 4000
}
log_entries {
timestamp {
seconds: 700
nanos: 600
}
severity: INFO
name: "system_event"
text_payload: "Sample text log message 0"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
labels {
key: "method"
value: "list"
}
start_time {
seconds: 100
}
end_time {
seconds: 300
}
int64_value: 1000
}
}
)";
// operation2 is after operation1.
const char kOperation2[] = R"(
operation_id: "some-operation-id"
operation_name: "google.example.library.v1.LibraryService.CreateShelf"
consumer_id: "project:some-consumer"
labels {
key: "library.googleapis.com/resource_type"
value: "book"
}
labels {
key: "library.googleapis.com/resource_id"
value: "projects/123/books/456"
}
labels {
key: "library.googleapis.com/region"
value: "us-central1"
}
labels {
key: "library.googleapis.com/zone"
value: "us-central1-a"
}
start_time {
seconds: 2000
nanos: 2000
}
end_time {
seconds: 4000
nanos: 4000
}
log_entries {
timestamp {
seconds: 700
nanos: 600
}
severity: INFO
name: "system_event"
text_payload: "Sample text log message 1"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
labels {
key: "method"
value: "list"
}
start_time {
seconds: 200
}
end_time {
seconds: 400
}
int64_value: 2000
}
}
)";
// Merge operation 1 into operation 2, assuming they have cumulative metrics.
const char kCumulativeMerged12[] = R"(
operation_id: "some-operation-id"
operation_name: "google.example.library.v1.LibraryService.CreateShelf"
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 4000
nanos: 4000
}
labels {
key: "library.googleapis.com/region"
value: "us-central1"
}
labels {
key: "library.googleapis.com/resource_id"
value: "projects/123/books/456"
}
labels {
key: "library.googleapis.com/resource_type"
value: "book"
}
labels {
key: "library.googleapis.com/zone"
value: "us-central1-a"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
labels {
key: "method"
value: "list"
}
start_time {
seconds: 200
}
end_time {
seconds: 400
}
int64_value: 2000
}
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 0"
name: "system_event"
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 1"
name: "system_event"
}
)";
// Merge operation 1 into operation 2, assuming they have delta metrics.
const char kDeltaMerged12[] = R"(
operation_id: "some-operation-id"
operation_name: "google.example.library.v1.LibraryService.CreateShelf"
consumer_id: "project:some-consumer"
start_time {
seconds: 1000
nanos: 2000
}
end_time {
seconds: 4000
nanos: 4000
}
labels {
key: "library.googleapis.com/region"
value: "us-central1"
}
labels {
key: "library.googleapis.com/resource_id"
value: "projects/123/books/456"
}
labels {
key: "library.googleapis.com/resource_type"
value: "book"
}
labels {
key: "library.googleapis.com/zone"
value: "us-central1-a"
}
metric_value_sets {
metric_name: "library.googleapis.com/rpc/client/count"
metric_values {
labels {
key: "method"
value: "list"
}
start_time {
seconds: 100
}
end_time {
seconds: 400
}
int64_value: 3000
}
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 0"
name: "system_event"
}
log_entries {
severity: INFO
timestamp {
seconds: 700
nanos: 600
}
text_payload: "Sample text log message 1"
name: "system_event"
}
)";
// Distribution for [-1, 1, 3, 5]
const char kDistribution[] = R"(
count: 4
mean: 2
sum_of_squared_deviation: 20
minimum: -1
maximum: 5
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 1
}
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
bucket_counts: 1
)";
// Distribution for [-1, 1, 3, 5] and [-1, 1, 3, 5]
const char kSumDistribution[] = R"(
count: 8
mean: 2
sum_of_squared_deviation: 40
minimum: -1
maximum: 5
exponential_buckets {
num_finite_buckets: 2
growth_factor: 2
scale: 1
}
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
bucket_counts: 2
)";
class OperationAggregatorTest : public ::testing::Test {
protected:
virtual void SetUp() {
ASSERT_TRUE(TextFormat::ParseFromString(kOperation1, &operation1_));
ASSERT_TRUE(TextFormat::ParseFromString(kOperation2, &operation2_));
ASSERT_TRUE(TextFormat::ParseFromString(kCumulativeMerged12,
&cumulateive_merged12_));
ASSERT_TRUE(TextFormat::ParseFromString(kDeltaMerged12, &delta_merged12_));
}
// Set the first metric value of the first metric value set to be double.
void SetDobuleValue(double value, Operation* operation) {
ABSL_CHECK(operation->metric_value_sets_size() > 0 &&
operation->metric_value_sets(0).metric_values_size() > 0);
operation->mutable_metric_value_sets(0)
->mutable_metric_values(0)
->set_double_value(value);
}
// Set the first metric value of the first metric value set to be double.
void SetDistributionValue(const Distribution& value, Operation* operation) {
ABSL_CHECK(operation->metric_value_sets_size() > 0 &&
operation->metric_value_sets(0).metric_values_size() > 0);
*(operation->mutable_metric_value_sets(0)
->mutable_metric_values(0)
->mutable_distribution_value()) = value;
}
Operation operation1_;
Operation operation2_;
Operation cumulateive_merged12_;
Operation delta_merged12_;
MetricValue metric_value_;
const std::unordered_map<string, MetricDescriptor::MetricKind>
cumulative_metric_kind_ = {{kMetric, MetricDescriptor::CUMULATIVE}};
const std::unordered_map<string, MetricDescriptor::MetricKind>
delta_metric_kind_ = {{kMetric, MetricDescriptor::DELTA}};
};
// Clears the start time of operation and metric values.
void ClearStartEndTime(Operation* operation) {
operation->clear_start_time();
operation->clear_end_time();
MetricValue* value1 =
operation->mutable_metric_value_sets(0)->mutable_metric_values(0);
value1->clear_start_time();
value1->clear_end_time();
}
TEST_F(OperationAggregatorTest, Cumulative_MergeOperation1AndOperation2) {
OperationAggregator iop(operation1_, &cumulative_metric_kind_);
iop.MergeOperation(operation2_);
EXPECT_TRUE(MessageDifferencer::Equals(iop.ToOperationProto(),
cumulateive_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_MergeOperation1AndOperation2) {
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_MergeOperation2AndOperation1) {
// Merge order does not matter.
// log_entries is a repeated field, the order is different if added in
// different order.
operation1_.clear_log_entries();
operation2_.clear_log_entries();
delta_merged12_.clear_log_entries();
OperationAggregator iop(operation2_, &delta_metric_kind_);
iop.MergeOperation(operation1_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest,
DefaultMetricKind_MergeOperation1AndOperation2) {
std::unordered_map<string, MetricDescriptor::MetricKind> empty_map;
OperationAggregator iop(operation1_, &empty_map);
iop.MergeOperation(operation2_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_InconsistentMetricValue) {
OperationAggregator iop(operation1_, &delta_metric_kind_);
MetricValue* value =
operation2_.mutable_metric_value_sets(0)->mutable_metric_values(0);
value->set_double_value(110.0);
value->mutable_start_time()->set_seconds(100);
value->mutable_start_time()->set_seconds(200);
iop.MergeOperation(operation2_);
// Because the the two operations have different metric value type, they
// won't be merged, so iop should still be the same as operation1_.
// However, log entries are still merged.
*(delta_merged12_.mutable_metric_value_sets(0)) =
operation1_.metric_value_sets(0);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_FirstOperationMissingStartEndTime) {
ClearStartEndTime(&operation1_);
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
// The merged operation should have the same time as operation2_.
*(delta_merged12_.mutable_start_time()) = operation2_.start_time();
*(delta_merged12_.mutable_end_time()) = operation2_.end_time();
const MetricValue& value2 = operation2_.metric_value_sets(0).metric_values(0);
MetricValue* merged_value =
delta_merged12_.mutable_metric_value_sets(0)->mutable_metric_values(0);
*(merged_value->mutable_start_time()) = value2.start_time();
*(merged_value->mutable_end_time()) = value2.end_time();
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_SecondOperationMissingStartEndTime) {
ClearStartEndTime(&operation2_);
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
// The merged operation should have the same time as operation1_.
*(delta_merged12_.mutable_start_time()) = operation1_.start_time();
*(delta_merged12_.mutable_end_time()) = operation1_.end_time();
const MetricValue& value1 = operation1_.metric_value_sets(0).metric_values(0);
MetricValue* merged_value =
delta_merged12_.mutable_metric_value_sets(0)->mutable_metric_values(0);
*(merged_value->mutable_start_time()) = value1.start_time();
*(merged_value->mutable_end_time()) = value1.end_time();
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, Delta_BothOperationsMissingStartEndTime) {
ClearStartEndTime(&operation1_);
ClearStartEndTime(&operation2_);
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
// The merged operation should have no start and end time.
ClearStartEndTime(&delta_merged12_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, DeltaMetricKind_DoubleValue) {
SetDobuleValue(10, &operation1_);
SetDobuleValue(20, &operation2_);
SetDobuleValue(30, &delta_merged12_);
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
TEST_F(OperationAggregatorTest, DeltaMetricKind_DistributionValue) {
Distribution distribution;
Distribution sum;
ASSERT_TRUE(TextFormat::ParseFromString(kDistribution, &distribution));
ASSERT_TRUE(TextFormat::ParseFromString(kSumDistribution, &sum));
SetDistributionValue(distribution, &operation1_);
SetDistributionValue(distribution, &operation2_);
SetDistributionValue(sum, &delta_merged12_);
OperationAggregator iop(operation1_, &delta_metric_kind_);
iop.MergeOperation(operation2_);
EXPECT_TRUE(
MessageDifferencer::Equals(iop.ToOperationProto(), delta_merged12_));
}
} // namespace
} // namespace service_control_client
} // namespace google