-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests2.cpp
More file actions
255 lines (199 loc) · 6.1 KB
/
tests2.cpp
File metadata and controls
255 lines (199 loc) · 6.1 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <iostream>
#include <vector>
#include <redox.hpp>
using namespace std;
using redox::Redox;
using redox::Command;
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 = {cout, redox::log::Debug};
rdx.noWait(true);
if(!rdx.connect("localhost", 6379)) return 1;
if(rdx.set("simple_loop:count", "0")) {
cout << "Reset the counter to zero." << endl;
} else {
cerr << "Failed to reset counter." << endl;
return 1;
}
vector<string> cmd_vec = {"INCR", "simple_loop:count"};
double freq = 1000000; // Hz
double dt = 1 / freq; // s
double t = 5; // s
int parallel = 1;
cout << "Sending \"" << rdx.vecToStr(cmd_vec) << "\" 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.commandLoop<int>(
cmd_vec,
[&count, &rdx](Command<int>& c) {
if (!c.ok()) {
cerr << "Bad reply: " << c.status() << endl;
}
count++;
},
dt
));
}
// Wait for t time, then stop the command.
this_thread::sleep_for(chrono::microseconds((int)(t*1e6)));
for(auto& c : commands) c->free();
double t_elapsed = time_s() - t0;
double actual_freq = (double)count / t_elapsed;
// Get the final value of the counter
long final_count = stol(rdx.get("simple_loop:count"));
cout << "Sent " << count << " commands in " << t_elapsed << "s, "
<< "that's " << actual_freq << " commands/s." << endl;
cout << "Final value of counter: " << final_count << endl;
rdx.disconnect();
return 0;
}
#include <random>
#include <iostream>
#include <functional>
#include <memory>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
#include <thread>
#include "HUtils/HStrings.hpp"
#include "Logger/Logger.hpp"
#include "Config/GlobalConf.hpp"
#include "AsyncEvent/Redis/RedisBuffer.hpp"
#include "AsyncEvent/AEvBase/AEventAbstract.hpp"
#include "AsyncEvent/Redis/AEvRedisMod.hpp"
redis::RespData result;
asio::io_service main_io;
aev::AEvStrandPtr _ev_loop(std::make_shared<asio::strand>(main_io));
aev::AEvRedisMod redis_db(_ev_loop, "127.0.0.1", 6379);
int query_counter = 0;
void ra_handler(int err, const redis::RespData &result);
void rq_handler(int err, const redis::RespData & result)
{
if (err)
log_debug("Error num: %", err);
++query_counter;
redis_db.async_query("get test", ra_handler);
}
void ra_handler(int err, const redis::RespData & result)
{
if (err)
log_debug("Error num: %", err);
if (result.ires > 900000)
exit(0);
}
int main () {
Config::glob().read_config("main.conf");
if (Config::glob().have_error()) {
CLog::glob().write("Error loading config: %", Config::glob().error_text());
exit(0);
}
Config::glob().set_opt("logging_level", "3");
RESProto buff;
// log_main("%", sizeof(aev::AEventAbstract));
// std::string result;
if (!redis_db.connect())
exit(1);
redis_db.query_loop("incr test", [](int err, const redis::RespData & result){
if (err || result.ires > 1000000)
redis_db.disconnect();
}, 1000000);
// std::thread t ([&]() {main_io.run();});
// std::thread t1 ([&]() {main_io.run();});
main_io.run();
// t.join();
// t1.join();
}
#include <random>
#include <iostream>
#include <functional>
#include <memory>
#include <map>
#include <algorithm>
#include <string>
#include <iostream>
#include <thread>
#include "HUtils/HStrings.hpp"
#include "Logger/Logger.hpp"
#include "Config/GlobalConf.hpp"
#include "AsyncEvent/Redis/RedisBuffer.hpp"
#include "AsyncEvent/AEvBase/AEventAbstract.hpp"
#include "AsyncEvent/Redis/AEvRedisMod.hpp"
redis::RespData result;
asio::io_service main_io;
aev::AEvStrandPtr _ev_loop(std::make_shared<asio::strand>(main_io));
int respond_count_t1 = 0;
int respond_count_t2 = 0;
int respond_count_main = 0;
void f(std::string const & s)
{
std::string k {s};
}
int main () {
Config::glob().read_config("main.conf");
if (Config::glob().have_error()) {
CLog::glob().write("Error loading config: %", Config::glob().error_text());
exit(0);
}
Config::glob().set_opt("logging_level", "3");
aev::AEvRedisMod redis_db(_ev_loop);
if (!redis_db.connect("127.0.0.1", 6379))
exit(1);
auto ra_handler_t1 = [&redis_db](int err, const redis::RespData & result)
{
if (err)
log_debug("Error num: %", err);
++respond_count_t1;
// if (result.sres != "1")
// log_main("error 1 %", result.sres);
// log_main("%", result.ires);
};
auto ra_handler_t2 = [&redis_db](int err, const redis::RespData & result)
{
if (err)
log_debug("Error num: %", err);
++respond_count_t2;
// if (result.sres != "2")
// log_main("error 2 %", result.sres);
// log_main("%", result.ires);
};
auto ra_handler = [&redis_db](int err, const redis::RespData & result)
{
if (err)
log_debug("Error num: %", err);
++respond_count_main;
// if (result.sres != "0")
// log_main("error 0 %", result.sres);
// log_main("%", result.ires);
};
std::thread t {[&]() {main_io.run();}};
std::thread q1 {[&]() {
for (int i = 0; i < 1000000; ++i)
{
redis_db.async_query("incr test1", ra_handler_t1);
}
}};
std::thread q2 {[&]() {
for (int i = 0; i < 1000000; ++i)
{
redis_db.async_query("incr test2", ra_handler_t2);
}
}};
for (int i = 0; i < 1000000; ++i)
{
redis_db.async_query("incr test", ra_handler);
}
q1.join();
q2.join();
log_main("All sended.");
redis_db.disconnect();
t.join();
log_main("Resived main = %, t1 = %, t2 = %", respond_count_main, respond_count_t1, respond_count_t2);
// t1.join();
}