Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PH] Added initial version of the TPS tester #27

Merged
merged 2 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/trx_generator/trx_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ namespace eosio::testing {
_peer_connection.disconnect();
}


typedef trx_tps_tester<null_trx_generator, simple_tps_monitor> null_tester;
}
69 changes: 69 additions & 0 deletions tests/trx_generator/trx_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include<boost/asio/ip/tcp.hpp>
#include<fc/network/message_buffer.hpp>
#include<eosio/chain/thread_utils.hpp>
#include<chrono>

using namespace std::chrono_literals;

namespace eosio::testing {
using send_buffer_type = std::shared_ptr<std::vector<char>>;
Expand Down Expand Up @@ -36,4 +39,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<typename G, typename M>
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 * std::chrono::microseconds(1s).count()};

bool keep_running = true;
fc::microseconds trx_interval{std::chrono::microseconds(1s).count() / _target_tps};

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()));
}
}

}
}
};
}