aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authorGravatar Vijay Pai <vpai@google.com>2016-02-10 14:55:33 -0800
committerGravatar Vijay Pai <vpai@google.com>2016-02-10 14:55:33 -0800
commit9982c6fc3a9bf435c963a057fcc853fec8c48e5e (patch)
tree238f31b72e613a2b16d640f60f17be92802a47f9 /test
parent42fad8e765b8bbdd2211dc2e0172a9565d395cc8 (diff)
Fix random number generators and improve code style (stop using
operator overloads)
Diffstat (limited to 'test')
-rw-r--r--test/cpp/qps/client.h9
-rw-r--r--test/cpp/qps/interarrival.h38
-rw-r--r--test/cpp/qps/qps_interarrival_test.cc8
3 files changed, 29 insertions, 26 deletions
diff --git a/test/cpp/qps/client.h b/test/cpp/qps/client.h
index 50b2bf2514..f62afffa47 100644
--- a/test/cpp/qps/client.h
+++ b/test/cpp/qps/client.h
@@ -184,7 +184,7 @@ class Client {
// Set up the load distribution based on the number of threads
const auto& load = config.load_params();
- std::unique_ptr<RandomDist> random_dist;
+ std::unique_ptr<RandomDistInterface> random_dist;
switch (load.load_case()) {
case LoadParams::kClosedLoop:
// Closed-loop doesn't use random dist at all
@@ -218,11 +218,12 @@ class Client {
closed_loop_ = false;
// set up interarrival timer according to random dist
interarrival_timer_.init(*random_dist, num_threads);
+ auto now = grpc_time_source::now();
for (size_t i = 0; i < num_threads; i++) {
next_time_.push_back(
- grpc_time_source::now() +
+ now +
std::chrono::duration_cast<grpc_time_source::duration>(
- interarrival_timer_(i)));
+ interarrival_timer_.next(i)));
}
}
}
@@ -234,7 +235,7 @@ class Client {
*time_delay = next_time_[thread_idx];
next_time_[thread_idx] +=
std::chrono::duration_cast<grpc_time_source::duration>(
- interarrival_timer_(thread_idx));
+ interarrival_timer_.next(thread_idx));
return true;
}
}
diff --git a/test/cpp/qps/interarrival.h b/test/cpp/qps/interarrival.h
index 841619e3ff..d1bfdcd756 100644
--- a/test/cpp/qps/interarrival.h
+++ b/test/cpp/qps/interarrival.h
@@ -51,15 +51,15 @@ namespace testing {
// stacks. Thus, this code only uses a uniform distribution of doubles [0,1)
// and then provides the distribution functions itself.
-class RandomDist {
+class RandomDistInterface {
public:
- RandomDist() {}
- virtual ~RandomDist() = 0;
- // Argument to operator() is a uniform double in the range [0,1)
- virtual double operator()(double uni) const = 0;
+ RandomDistInterface() {}
+ virtual ~RandomDistInterface() = 0;
+ // Argument to transform is a uniform double in the range [0,1)
+ virtual double transform(double uni) const = 0;
};
-inline RandomDist::~RandomDist() {}
+inline RandomDistInterface::~RandomDistInterface() {}
// ExpDist implements an exponential distribution, which is the
// interarrival distribution for a Poisson process. The parameter
@@ -69,11 +69,11 @@ inline RandomDist::~RandomDist() {}
// independent identical stationary sources. For more information,
// see http://en.wikipedia.org/wiki/Exponential_distribution
-class ExpDist GRPC_FINAL : public RandomDist {
+class ExpDist GRPC_FINAL : public RandomDistInterface {
public:
explicit ExpDist(double lambda) : lambda_recip_(1.0 / lambda) {}
~ExpDist() GRPC_OVERRIDE {}
- double operator()(double uni) const GRPC_OVERRIDE {
+ double transform(double uni) const GRPC_OVERRIDE {
// Note: Use 1.0-uni above to avoid NaN if uni is 0
return lambda_recip_ * (-log(1.0 - uni));
}
@@ -87,11 +87,11 @@ class ExpDist GRPC_FINAL : public RandomDist {
// mean interarrival time is (lo+hi)/2. For more information,
// see http://en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29
-class UniformDist GRPC_FINAL : public RandomDist {
+class UniformDist GRPC_FINAL : public RandomDistInterface {
public:
UniformDist(double lo, double hi) : lo_(lo), range_(hi - lo) {}
~UniformDist() GRPC_OVERRIDE {}
- double operator()(double uni) const GRPC_OVERRIDE {
+ double transform(double uni) const GRPC_OVERRIDE {
return uni * range_ + lo_;
}
@@ -106,11 +106,11 @@ class UniformDist GRPC_FINAL : public RandomDist {
// clients) will not preserve any deterministic interarrival gap across
// requests.
-class DetDist GRPC_FINAL : public RandomDist {
+class DetDist GRPC_FINAL : public RandomDistInterface {
public:
explicit DetDist(double val) : val_(val) {}
~DetDist() GRPC_OVERRIDE {}
- double operator()(double uni) const GRPC_OVERRIDE { return val_; }
+ double transform(double uni) const GRPC_OVERRIDE { return val_; }
private:
double val_;
@@ -123,12 +123,12 @@ class DetDist GRPC_FINAL : public RandomDist {
// good representation of the response times of data center jobs. See
// http://en.wikipedia.org/wiki/Pareto_distribution
-class ParetoDist GRPC_FINAL : public RandomDist {
+class ParetoDist GRPC_FINAL : public RandomDistInterface {
public:
ParetoDist(double base, double alpha)
: base_(base), alpha_recip_(1.0 / alpha) {}
~ParetoDist() GRPC_OVERRIDE {}
- double operator()(double uni) const GRPC_OVERRIDE {
+ double transform(double uni) const GRPC_OVERRIDE {
// Note: Use 1.0-uni above to avoid div by zero if uni is 0
return base_ / pow(1.0 - uni, alpha_recip_);
}
@@ -145,13 +145,15 @@ class ParetoDist GRPC_FINAL : public RandomDist {
class InterarrivalTimer {
public:
InterarrivalTimer() {}
- void init(const RandomDist& r, int threads, int entries = 1000000) {
+ void init(const RandomDistInterface& r, int threads, int entries = 1000000) {
for (int i = 0; i < entries; i++) {
// rand is the only choice that is portable across POSIX and Windows
// and that supports new and old compilers
- const double uniform_0_1 = rand() / RAND_MAX;
+ const double uniform_0_1 = static_cast<double>(rand())
+ / static_cast<double>(RAND_MAX);
random_table_.push_back(
- std::chrono::nanoseconds(static_cast<int64_t>(1e9 * r(uniform_0_1))));
+ std::chrono::nanoseconds(static_cast<int64_t>(
+ 1e9 * r.transform(uniform_0_1))));
}
// Now set up the thread positions
for (int i = 0; i < threads; i++) {
@@ -160,7 +162,7 @@ class InterarrivalTimer {
}
virtual ~InterarrivalTimer(){};
- std::chrono::nanoseconds operator()(int thread_num) {
+ std::chrono::nanoseconds next(int thread_num) {
auto ret = *(thread_posns_[thread_num]++);
if (thread_posns_[thread_num] == random_table_.end())
thread_posns_[thread_num] = random_table_.begin();
diff --git a/test/cpp/qps/qps_interarrival_test.cc b/test/cpp/qps/qps_interarrival_test.cc
index ccda28f09a..dd32bcc0e3 100644
--- a/test/cpp/qps/qps_interarrival_test.cc
+++ b/test/cpp/qps/qps_interarrival_test.cc
@@ -39,17 +39,17 @@
#include "test/cpp/qps/interarrival.h"
-using grpc::testing::RandomDist;
+using grpc::testing::RandomDistInterface;
using grpc::testing::InterarrivalTimer;
-static void RunTest(RandomDist &&r, int threads, std::string title) {
+static void RunTest(RandomDistInterface &&r, int threads, std::string title) {
InterarrivalTimer timer;
timer.init(r, threads);
gpr_histogram *h(gpr_histogram_create(0.01, 60e9));
for (int i = 0; i < 10000000; i++) {
for (int j = 0; j < threads; j++) {
- gpr_histogram_add(h, timer(j).count());
+ gpr_histogram_add(h, timer.next(j).count());
}
}
@@ -70,7 +70,7 @@ using grpc::testing::ParetoDist;
int main(int argc, char **argv) {
RunTest(ExpDist(10.0), 5, std::string("Exponential(10)"));
RunTest(DetDist(5.0), 5, std::string("Det(5)"));
- RunTest(UniformDist(0.0, 10.0), 5, std::string("Uniform(1,10)"));
+ RunTest(UniformDist(0.0, 10.0), 5, std::string("Uniform(0,10)"));
RunTest(ParetoDist(1.0, 1.0), 5, std::string("Pareto(1,1)"));
return 0;
}