interarrival.h
Go to the documentation of this file.
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 
40  public:
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 
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:
59  explicit ExpDist(double lambda) : lambda_recip_(1.0 / lambda) {}
60  ~ExpDist() override {}
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 
75  public:
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  // Now set up the thread positions
86  for (int i = 0; i < threads; i++) {
87  thread_posns_.push_back(random_table_.begin() + (entries * i) / threads);
88  }
89  }
90  virtual ~InterarrivalTimer(){};
91 
92  int64_t next(int thread_num) {
93  auto ret = *(thread_posns_[thread_num]++);
94  if (thread_posns_[thread_num] == random_table_.end()) {
95  thread_posns_[thread_num] = random_table_.begin();
96  }
97  return ret;
98  }
99 
100  private:
101  typedef std::vector<int64_t> time_table;
102  std::vector<time_table::const_iterator> thread_posns_;
104 };
105 } // namespace testing
106 } // namespace grpc
107 
108 #endif
grpc::testing::InterarrivalTimer
Definition: interarrival.h:74
testing
Definition: aws_request_signer_test.cc:25
grpc::testing::InterarrivalTimer::init
void init(const RandomDistInterface &r, int threads, int entries=1000000)
Definition: interarrival.h:77
grpc
Definition: grpcpp/alarm.h:33
grpc::testing::RandomDistInterface::RandomDistInterface
RandomDistInterface()
Definition: interarrival.h:41
threads
static uv_thread_t * threads
Definition: threadpool.c:38
grpc::testing::InterarrivalTimer::thread_posns_
std::vector< time_table::const_iterator > thread_posns_
Definition: interarrival.h:102
grpc::testing::InterarrivalTimer::next
int64_t next(int thread_num)
Definition: interarrival.h:92
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
grpc::testing::RandomDistInterface::~RandomDistInterface
virtual ~RandomDistInterface()=0
Definition: interarrival.h:47
grpc::testing::InterarrivalTimer::time_table
std::vector< int64_t > time_table
Definition: interarrival.h:101
grpc::testing::ExpDist::lambda_recip_
double lambda_recip_
Definition: interarrival.h:67
grpc::testing::InterarrivalTimer::random_table_
time_table random_table_
Definition: interarrival.h:103
grpc::testing::ExpDist
Definition: interarrival.h:57
config.h
grpc::testing::ExpDist::ExpDist
ExpDist(double lambda)
Definition: interarrival.h:59
grpc::testing::InterarrivalTimer::InterarrivalTimer
InterarrivalTimer()
Definition: interarrival.h:76
grpc::testing::ExpDist::transform
double transform(double uni) const override
Definition: interarrival.h:61
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc::testing::RandomDistInterface::transform
virtual double transform(double uni) const =0
log
bool log
Definition: abseil-cpp/absl/synchronization/mutex.cc:310
grpc::testing::RandomDistInterface
Definition: interarrival.h:39
grpc::testing::ExpDist::~ExpDist
~ExpDist() override
Definition: interarrival.h:60
grpc::testing::InterarrivalTimer::~InterarrivalTimer
virtual ~InterarrivalTimer()
Definition: interarrival.h:90
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:21