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 #ifndef TEST_QPS_INTERARRIVAL_H
20 #define TEST_QPS_INTERARRIVAL_H
21 
22 #include <chrono>
23 #include <cmath>
24 #include <random>
25 #include <vector>
26 
27 #include <grpcpp/support/config.h>
28 
29 namespace grpc {
30 namespace testing {
31 
32 // First create classes that define a random distribution
33 // Note that this code does not include C++-specific random distribution
34 // features supported in std::random. Although this would make this code easier,
35 // this code is required to serve as the template code for other language
36 // stacks. Thus, this code only uses a uniform distribution of doubles [0,1)
37 // and then provides the distribution functions itself.
38 
39 class RandomDistInterface {
40  public:
RandomDistInterface()41   RandomDistInterface() {}
42   virtual ~RandomDistInterface() = 0;
43   // Argument to transform is a uniform double in the range [0,1)
44   virtual double transform(double uni) const = 0;
45 };
46 
~RandomDistInterface()47 inline RandomDistInterface::~RandomDistInterface() {}
48 
49 // ExpDist implements an exponential distribution, which is the
50 // interarrival distribution for a Poisson process. The parameter
51 // lambda is the mean rate of arrivals. This is the
52 // most useful distribution since it is actually additive and
53 // memoryless. It is a good representation of activity coming in from
54 // independent identical stationary sources. For more information,
55 // see http://en.wikipedia.org/wiki/Exponential_distribution
56 
57 class ExpDist final : public RandomDistInterface {
58  public:
ExpDist(double lambda)59   explicit ExpDist(double lambda) : lambda_recip_(1.0 / lambda) {}
~ExpDist()60   ~ExpDist() override {}
transform(double uni)61   double transform(double uni) const override {
62     // Note: Use 1.0-uni above to avoid NaN if uni is 0
63     return lambda_recip_ * (-log(1.0 - uni));
64   }
65 
66  private:
67   double lambda_recip_;
68 };
69 
70 // A class library for generating pseudo-random interarrival times
71 // in an efficient re-entrant way. The random table is built at construction
72 // time, and each call must include the thread id of the invoker
73 
74 class InterarrivalTimer {
75  public:
InterarrivalTimer()76   InterarrivalTimer() {}
77   void init(const RandomDistInterface& r, int threads, int entries = 1000000) {
78     std::random_device devrand;
79     std::mt19937_64 generator(devrand());
80     std::uniform_real_distribution<double> rando(0, 1);
81     for (int i = 0; i < entries; i++) {
82       random_table_.push_back(
83           static_cast<int64_t>(1e9 * r.transform(rando(generator))));
84       ;
85     }
86     // Now set up the thread positions
87     for (int i = 0; i < threads; i++) {
88       thread_posns_.push_back(random_table_.begin() + (entries * i) / threads);
89     }
90   }
~InterarrivalTimer()91   virtual ~InterarrivalTimer(){};
92 
next(int thread_num)93   int64_t next(int thread_num) {
94     auto ret = *(thread_posns_[thread_num]++);
95     if (thread_posns_[thread_num] == random_table_.end())
96       thread_posns_[thread_num] = random_table_.begin();
97     return ret;
98   }
99 
100  private:
101   typedef std::vector<int64_t> time_table;
102   std::vector<time_table::const_iterator> thread_posns_;
103   time_table random_table_;
104 };
105 }  // namespace testing
106 }  // namespace grpc
107 
108 #endif
109