-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPipeliningDemo.cpp
111 lines (99 loc) · 3.23 KB
/
PipeliningDemo.cpp
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
/*
* Copyright (c) 2008-2023, Hazelcast, 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 <random>
#include <hazelcast/client/hazelcast_client.h>
#include <hazelcast/client/pipelining.h>
using namespace std;
using namespace hazelcast::client;
using namespace hazelcast::util;
/**
* A demonstration of the performance impact of using pipeling.
*
* For the benchmark we compare simple imap::get calls with a pipelined
* approach.
*/
class PipeliningDemo
{
public:
PipeliningDemo()
: client_(hazelcast::new_client().get())
, map_(client_.get_map("map").get())
, gen_(rd_())
{}
void init()
{
for (int l = 0; l < keyDomain; l++) {
map_->put(l, std::to_string(l)).get();
}
}
void pipelined(int depth)
{
cout << "Starting pipelined with depth:" << depth << endl;
int64_t startMs = current_time_millis();
for (int i = 0; i < iterations; i++) {
std::shared_ptr<pipelining<string>> p =
pipelining<string>::create(depth);
for (long k = 0; k < getsPerIteration; k++) {
int key = dist_(gen_) % keyDomain;
p->add(map_->get<int, std::string>(key));
}
// wait for completion
auto results = p->results();
// and verification we got the appropriate number of results.
if ((int)results.size() != getsPerIteration) {
throw hazelcast::client::exception::illegal_state(
"pipelined", "Incorrect number of results");
}
}
int64_t endMs = current_time_millis();
cout << "Pipelined with depth:" << depth
<< ", duration:" << (endMs - startMs) << " ms" << endl;
}
void non_pipelined()
{
cout << "Starting non pipelined" << endl;
int64_t startMs = current_time_millis();
for (int i = 0; i < iterations; i++) {
for (long k = 0; k < getsPerIteration; k++) {
int key = dist_(gen_) % keyDomain;
map_->get<int, std::string>(key).get();
}
}
int64_t endMs = current_time_millis();
cout << "Non pipelined duration:" << (endMs - startMs) << " ms" << endl;
}
private:
hazelcast_client client_;
std::shared_ptr<imap> map_;
static const int keyDomain = 100000;
static const int iterations = 500;
static const int getsPerIteration = 1000;
std::random_device rd_;
std::mt19937 gen_;
std::uniform_int_distribution<int> dist_;
};
int
main()
{
PipeliningDemo main;
main.init();
main.pipelined(5);
main.pipelined(10);
main.pipelined(100);
main.non_pipelined();
cout << "Finished" << endl;
return 0;
}