1f29cd729687b267db1b972c8301d15196d7dc15
[platform/upstream/grpc.git] / test / cpp / qps / qps_interarrival_test.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <chrono>
20 #include <iostream>
21
22 // Use the C histogram rather than C++ to avoid depending on proto
23 #include "test/core/util/histogram.h"
24
25 #include "test/core/util/test_config.h"
26 #include "test/cpp/qps/interarrival.h"
27 #include "test/cpp/util/test_config.h"
28
29 using grpc::testing::InterarrivalTimer;
30 using grpc::testing::RandomDistInterface;
31
32 static void RunTest(RandomDistInterface&& r, int threads,
33                     const std::string& title) {
34   InterarrivalTimer timer;
35   timer.init(r, threads);
36   grpc_histogram* h(grpc_histogram_create(0.01, 60e9));
37
38   for (int i = 0; i < 10000000; i++) {
39     for (int j = 0; j < threads; j++) {
40       grpc_histogram_add(h, timer.next(j));
41     }
42   }
43
44   std::cout << title << " Distribution" << std::endl;
45   std::cout << "Value, Percentile" << std::endl;
46   for (double pct = 0.0; pct < 100.0; pct += 1.0) {
47     std::cout << grpc_histogram_percentile(h, pct) << "," << pct << std::endl;
48   }
49
50   grpc_histogram_destroy(h);
51 }
52
53 using grpc::testing::ExpDist;
54
55 int main(int argc, char** argv) {
56   grpc::testing::TestEnvironment env(argc, argv);
57   grpc::testing::InitTest(&argc, &argv, true);
58
59   RunTest(ExpDist(10.0), 5, std::string("Exponential(10)"));
60   return 0;
61 }