Skip to content

Commit

Permalink
Merge pull request #27 from AntelopeIO/ph_add_tps_tester
Browse files Browse the repository at this point in the history
Added initial version of the TPS tester
  • Loading branch information
ndcgundlach authored Aug 22, 2022
2 parents 0478825 + b393de5 commit 03af897
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
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()));
}
}

}
}
};
}

0 comments on commit 03af897

Please sign in to comment.