speed_test_async_multi.cpp
1.83 KB
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
/**
* Redox test
* ----------
* Increment a key on Redis using an asynchronous command on a timer.
*/
#include <iostream>
#include <vector>
#include "../src/redox.hpp"
using namespace std;
using namespace redox;
double time_s() {
unsigned long ms = chrono::system_clock::now().time_since_epoch() / chrono::microseconds(1);
return (double)ms / 1e6;
}
int main(int argc, char* argv[]) {
Redox rdx = {"localhost", 6379};
rdx.run();
if(rdx.command_blocking("SET simple_loop:count 0")) {
cout << "Reset the counter to zero." << endl;
} else {
cerr << "Failed to reset counter." << endl;
return 1;
}
string cmd_str = "INCR simple_loop:count";
double freq = 10000; // Hz
double dt = 1 / freq; // s
double t = 5; // s
int parallel = 100;
cout << "Sending \"" << cmd_str << "\" asynchronously every "
<< dt << "s for " << t << "s..." << endl;
double t0 = time_s();
atomic_int count(0);
vector<Command<int>*> commands;
for(int i = 0; i < parallel; i++) {
commands.push_back(rdx.command<int>(
cmd_str,
[&count, &rdx](const string &cmd, const int& value) { count++; },
[](const string& cmd, int status) { cerr << "Bad reply: " << status << endl; },
dt
));
}
// Wait for t time, then stop the command.
this_thread::sleep_for(chrono::microseconds((int)(t*1e6)));
for(auto c : commands) rdx.cancel(c);
// Get the final value of the counter
auto get_cmd = rdx.command_blocking<string>("GET simple_loop:count");
long final_count = stol(get_cmd->reply());
get_cmd->free();
rdx.stop();
double t_elapsed = time_s() - t0;
double actual_freq = (double)count / t_elapsed;
cout << "Sent " << count << " commands in " << t_elapsed << "s, "
<< "that's " << actual_freq << " commands/s." << endl;
cout << "Final value of counter: " << final_count << endl;
return 0;
}