Commit 48255731b3ae5c48f2af84bddc76b0a7279d11a1
1 parent
ce650509
Add jitter test example
Useful for analyzing the delay between signals.
Showing
2 changed files
with
142 additions
and
2 deletions
CMakeLists.txt
| @@ -133,14 +133,17 @@ if (examples) | @@ -133,14 +133,17 @@ if (examples) | ||
| 133 | add_executable(pub_sub examples/pub_sub.cpp) | 133 | add_executable(pub_sub examples/pub_sub.cpp) |
| 134 | target_link_libraries(pub_sub redox) | 134 | target_link_libraries(pub_sub redox) |
| 135 | 135 | ||
| 136 | - add_executable(speed_test_pubsub examples/speed_test_pubsub ${SRC_ALL}) | 136 | + add_executable(speed_test_pubsub examples/speed_test_pubsub.cpp) |
| 137 | target_link_libraries(speed_test_pubsub redox) | 137 | target_link_libraries(speed_test_pubsub redox) |
| 138 | 138 | ||
| 139 | + add_executable(jitter_test examples/jitter_test.cpp) | ||
| 140 | + target_link_libraries(jitter_test redox) | ||
| 141 | + | ||
| 139 | add_custom_target(examples) | 142 | add_custom_target(examples) |
| 140 | add_dependencies(examples | 143 | add_dependencies(examples |
| 141 | basic basic_threaded lpush_benchmark speed_test_async speed_test_sync | 144 | basic basic_threaded lpush_benchmark speed_test_async speed_test_sync |
| 142 | speed_test_async_multi data_types multi_client binary_data pub_sub | 145 | speed_test_async_multi data_types multi_client binary_data pub_sub |
| 143 | - speed_test_pubsub | 146 | + speed_test_pubsub jitter_test |
| 144 | ) | 147 | ) |
| 145 | 148 | ||
| 146 | endif() | 149 | endif() |
examples/jitter_test.cpp
0 → 100644
| 1 | +/* | ||
| 2 | +* Test for analyzing the jitter of commands. | ||
| 3 | +*/ | ||
| 4 | + | ||
| 5 | +#include <iostream> | ||
| 6 | +#include <string.h> | ||
| 7 | +#include "redox.hpp" | ||
| 8 | + | ||
| 9 | +using namespace std; | ||
| 10 | +using redox::Redox; | ||
| 11 | +using redox::Command; | ||
| 12 | + | ||
| 13 | +double time_s() { | ||
| 14 | + unsigned long ms = chrono::system_clock::now().time_since_epoch() / chrono::microseconds(1); | ||
| 15 | + return (double)ms / 1e6; | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +int main(int argc, char* argv[]) { | ||
| 19 | + | ||
| 20 | + string usage_string = "Usage: " + string(argv[0]) + " --(set-async|get-async|set-sync|get-sync)"; | ||
| 21 | + if(argc != 2) { | ||
| 22 | + cerr << usage_string<< endl; | ||
| 23 | + return 1; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + Redox rdx; | ||
| 27 | + if(!rdx.connect("localhost", 6379)) return 1; | ||
| 28 | + | ||
| 29 | + double freq = 1000; // Hz | ||
| 30 | + double dt = 1 / freq; // s | ||
| 31 | + int iter = 1000000; | ||
| 32 | + atomic_int count(0); | ||
| 33 | + | ||
| 34 | + double t = time_s(); | ||
| 35 | + double t_new = t; | ||
| 36 | + | ||
| 37 | + double t_reply = t; | ||
| 38 | + double t_reply_new = t; | ||
| 39 | + | ||
| 40 | + if(!strcmp(argv[1], "--get-async")) { | ||
| 41 | + | ||
| 42 | + while(count < iter) { | ||
| 43 | + rdx.command<string>({"GET", "jitter_test:time"}, | ||
| 44 | + [&](Command<string>& c) { | ||
| 45 | + if (!c.ok()) { | ||
| 46 | + cerr << "Bad reply: " << c.status() << endl; | ||
| 47 | + } else { | ||
| 48 | + t_new = time_s(); | ||
| 49 | + t_reply_new = stod(c.reply()); | ||
| 50 | + cout << "dt real: " << (t_new - t) * 1000 | ||
| 51 | + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; | ||
| 52 | + t = t_new; | ||
| 53 | + t_reply = t_reply_new; | ||
| 54 | + } | ||
| 55 | + count++; | ||
| 56 | + if (count == iter) rdx.stop(); | ||
| 57 | + } | ||
| 58 | + ); | ||
| 59 | + | ||
| 60 | + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + } else if(!strcmp(argv[1], "--get-async-loop")) { | ||
| 64 | + | ||
| 65 | + rdx.commandLoop<string>({"GET", "jitter_test:time"}, | ||
| 66 | + [&](Command<string>& c) { | ||
| 67 | + if (!c.ok()) { | ||
| 68 | + cerr << "Bad reply: " << c.status() << endl; | ||
| 69 | + } else { | ||
| 70 | + t_new = time_s(); | ||
| 71 | + t_reply_new = stod(c.reply()); | ||
| 72 | + cout << "dt real: " << (t_new - t) * 1000 | ||
| 73 | + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; | ||
| 74 | + t = t_new; | ||
| 75 | + t_reply = t_reply_new; | ||
| 76 | + } | ||
| 77 | + count++; | ||
| 78 | + if (count == iter) rdx.stop(); | ||
| 79 | + }, | ||
| 80 | + .001 | ||
| 81 | + ); | ||
| 82 | + | ||
| 83 | + } else if(!strcmp(argv[1], "--set-async")) { | ||
| 84 | + | ||
| 85 | + while (count < iter) { | ||
| 86 | + rdx.command<string>({"SET", "jitter_test:time", to_string(time_s())}, | ||
| 87 | + [&](Command<string>& c) { | ||
| 88 | + if (!c.ok()) { | ||
| 89 | + cerr << "Error setting value: " << c.status() << endl; | ||
| 90 | + } | ||
| 91 | + count++; | ||
| 92 | + if (count == iter) rdx.stop(); | ||
| 93 | + } | ||
| 94 | + ); | ||
| 95 | + this_thread::sleep_for(chrono::microseconds((int) (dt * 1e6))); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + } else if(!strcmp(argv[1], "--get-sync")) { | ||
| 99 | + | ||
| 100 | + while(count < iter) { | ||
| 101 | + Command<string>& c = rdx.commandSync<string>({"GET", "jitter_test:time"}); | ||
| 102 | + if(!c.ok()) { | ||
| 103 | + cerr << "Error setting value: " << c.status() << endl; | ||
| 104 | + } else { | ||
| 105 | + t_new = time_s(); | ||
| 106 | + t_reply_new = stod(c.reply()); | ||
| 107 | + cout << "dt real: " << (t_new - t) * 1000 | ||
| 108 | + << ", dt msg: " << (t_reply_new - t_reply) * 1000 << endl; | ||
| 109 | + t = t_new; | ||
| 110 | + t_reply = t_reply_new; | ||
| 111 | + } | ||
| 112 | + count++; | ||
| 113 | + if(count == iter) rdx.stop(); | ||
| 114 | + c.free(); | ||
| 115 | + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + } else if(!strcmp(argv[1], "--set-sync")) { | ||
| 119 | + | ||
| 120 | + while(count < iter){ | ||
| 121 | + Command<string>& c = rdx.commandSync<string>({"SET", "jitter_test:time", to_string(time_s())}); | ||
| 122 | + if(!c.ok()) { | ||
| 123 | + cerr << "Error setting value: " << c.status() << endl; | ||
| 124 | + } | ||
| 125 | + count++; | ||
| 126 | + if(count == iter) rdx.stop(); | ||
| 127 | + c.free(); | ||
| 128 | + this_thread::sleep_for(chrono::microseconds((int)((dt) * 1e6))); | ||
| 129 | + } | ||
| 130 | + } else { | ||
| 131 | + cerr << usage_string << endl; | ||
| 132 | + return 1; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + rdx.wait(); | ||
| 136 | + return 0; | ||
| 137 | +}; |