From 6a1f3ed1b969ef816c471599addb475ae1d5d0c5 Mon Sep 17 00:00:00 2001 From: Chris Gundlach Date: Fri, 19 Aug 2022 09:34:30 -0500 Subject: [PATCH 1/2] initial trx_tps_tester --- tests/trx_generator/trx_provider.cpp | 2 + tests/trx_generator/trx_provider.hpp | 66 ++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/tests/trx_generator/trx_provider.cpp b/tests/trx_generator/trx_provider.cpp index 4e0996745e..bc6d17d8ac 100644 --- a/tests/trx_generator/trx_provider.cpp +++ b/tests/trx_generator/trx_provider.cpp @@ -79,4 +79,6 @@ namespace eosio::testing { _peer_connection.disconnect(); } + + typedef trx_tps_tester null_tester; } diff --git a/tests/trx_generator/trx_provider.hpp b/tests/trx_generator/trx_provider.hpp index 1bb2221ee6..62b47826a9 100644 --- a/tests/trx_generator/trx_provider.hpp +++ b/tests/trx_generator/trx_provider.hpp @@ -36,4 +36,70 @@ namespace eosio::testing { p2p_connection _peer_connection; }; + using fc::time_point; + + struct tps_test_stats { + uint32_t total_trxs; + uint32_t trxs_left; + + time_point start_time; + time_point expected_end_time; + }; + + struct simple_tps_monitor { + bool monitor_test(const tps_test_stats& stats) {return true;} + }; + + struct null_trx_generator { + void generate_and_send() {} + }; + + constexpr int64_t min_sleep_us = 100; + + template + struct trx_tps_tester { + G _generator; + M _monitor; + + uint32_t _gen_duration_seconds; + uint32_t _target_tps; + + trx_tps_tester(G generator, M monitor, uint32_t gen_duration_seconds, uint32_t target_tps) : + _generator(), _monitor(), _gen_duration_seconds(gen_duration_seconds), _target_tps(target_tps) { + + } + + void run() { + tps_test_stats stats; + + stats.total_trxs = _gen_duration_seconds * _target_tps; + stats.trxs_left = stats.total_trxs; + stats.start_time = fc::time_point::now(); + stats.expected_end_time = stats.start_time + fc::microseconds{_gen_duration_seconds * 1000000}; + + bool keep_running = true; + fc::microseconds trx_interval{_target_tps / 1000000}; + + fc::time_point last_run; + fc::time_point next_run; + + while (keep_running) { + last_run = fc::time_point::now(); + next_run = last_run + trx_interval; + + _generator.generate_and_send(); + stats.trxs_left--; + + keep_running = (_monitor.monitor_test(stats) && stats.trxs_left); + + if (keep_running) { + fc::microseconds time_to_sleep{next_run - fc::time_point::now()}; + if (time_to_sleep.count() > min_sleep_us) { + std::this_thread::sleep_for(std::chrono::microseconds(time_to_sleep.count())); + } + } + + } + } + }; } \ No newline at end of file From b393de56284af8bef138c01092bc6c5fbdd75b65 Mon Sep 17 00:00:00 2001 From: Chris Gundlach Date: Fri, 19 Aug 2022 10:16:37 -0500 Subject: [PATCH 2/2] fixed arithmetic error; used std conversion --- tests/trx_generator/trx_provider.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/trx_generator/trx_provider.hpp b/tests/trx_generator/trx_provider.hpp index 62b47826a9..0cdb926400 100644 --- a/tests/trx_generator/trx_provider.hpp +++ b/tests/trx_generator/trx_provider.hpp @@ -6,6 +6,9 @@ #include #include #include +#include + +using namespace std::chrono_literals; namespace eosio::testing { using send_buffer_type = std::shared_ptr>; @@ -75,10 +78,10 @@ namespace eosio::testing { stats.total_trxs = _gen_duration_seconds * _target_tps; stats.trxs_left = stats.total_trxs; stats.start_time = fc::time_point::now(); - stats.expected_end_time = stats.start_time + fc::microseconds{_gen_duration_seconds * 1000000}; + stats.expected_end_time = stats.start_time + fc::microseconds{_gen_duration_seconds * std::chrono::microseconds(1s).count()}; bool keep_running = true; - fc::microseconds trx_interval{_target_tps / 1000000}; + fc::microseconds trx_interval{std::chrono::microseconds(1s).count() / _target_tps}; fc::time_point last_run; fc::time_point next_run;