diff --git a/CMakeLists.txt b/CMakeLists.txt index b47407f..e21ad02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -133,14 +133,17 @@ if (examples) add_executable(pub_sub examples/pub_sub.cpp) target_link_libraries(pub_sub redox) - add_executable(speed_test_pubsub examples/speed_test_pubsub ${SRC_ALL}) + add_executable(speed_test_pubsub examples/speed_test_pubsub.cpp) target_link_libraries(speed_test_pubsub redox) + add_executable(jitter_test examples/jitter_test.cpp) + target_link_libraries(jitter_test redox) + add_custom_target(examples) add_dependencies(examples basic basic_threaded lpush_benchmark speed_test_async speed_test_sync speed_test_async_multi data_types multi_client binary_data pub_sub - speed_test_pubsub + speed_test_pubsub jitter_test ) endif() diff --git a/examples/jitter_test.cpp b/examples/jitter_test.cpp new file mode 100644 index 0000000..02339b2 --- /dev/null +++ b/examples/jitter_test.cpp @@ -0,0 +1,137 @@ +/* +* Test for analyzing the jitter of commands. +*/ + +#include +#include +#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[]) { + + string usage_string = "Usage: " + string(argv[0]) + " --(set-async|get-async|set-sync|get-sync)"; + if(argc != 2) { + cerr << usage_string<< endl; + return 1; + } + + Redox rdx; + if(!rdx.connect("localhost", 6379)) return 1; + + double freq = 1000; // Hz + double dt = 1 / freq; // s + int iter = 1000000; + atomic_int count(0); + + double t = time_s(); + double t_new = t; + + double t_reply = t; + double t_reply_new = t; + + if(!strcmp(argv[1], "--get-async")) { + + while(count < iter) { + rdx.command({"GET", "jitter_test:time"}, + [&](Command& c) { + if (!c.ok()) { + cerr << "Bad reply: " << c.status() << endl; + } else { + t_new = time_s(); + t_reply_new = stod(c.reply()); + cout << "dt real: " << (t_new - t) * 1000 + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; + t = t_new; + t_reply = t_reply_new; + } + count++; + if (count == iter) rdx.stop(); + } + ); + + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); + } + + } else if(!strcmp(argv[1], "--get-async-loop")) { + + rdx.commandLoop({"GET", "jitter_test:time"}, + [&](Command& c) { + if (!c.ok()) { + cerr << "Bad reply: " << c.status() << endl; + } else { + t_new = time_s(); + t_reply_new = stod(c.reply()); + cout << "dt real: " << (t_new - t) * 1000 + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; + t = t_new; + t_reply = t_reply_new; + } + count++; + if (count == iter) rdx.stop(); + }, + .001 + ); + + } else if(!strcmp(argv[1], "--set-async")) { + + while (count < iter) { + rdx.command({"SET", "jitter_test:time", to_string(time_s())}, + [&](Command& c) { + if (!c.ok()) { + cerr << "Error setting value: " << c.status() << endl; + } + count++; + if (count == iter) rdx.stop(); + } + ); + this_thread::sleep_for(chrono::microseconds((int) (dt * 1e6))); + } + + } else if(!strcmp(argv[1], "--get-sync")) { + + while(count < iter) { + Command& c = rdx.commandSync({"GET", "jitter_test:time"}); + if(!c.ok()) { + cerr << "Error setting value: " << c.status() << endl; + } else { + t_new = time_s(); + t_reply_new = stod(c.reply()); + cout << "dt real: " << (t_new - t) * 1000 + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; + t = t_new; + t_reply = t_reply_new; + } + count++; + if(count == iter) rdx.stop(); + c.free(); + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); + } + + } else if(!strcmp(argv[1], "--set-sync")) { + + while(count < iter){ + Command& c = rdx.commandSync({"SET", "jitter_test:time", to_string(time_s())}); + if(!c.ok()) { + cerr << "Error setting value: " << c.status() << endl; + } + count++; + if(count == iter) rdx.stop(); + c.free(); + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); + } + } else { + cerr << usage_string << endl; + return 1; + } + + rdx.wait(); + return 0; +};