Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
Merge pull request #138 from measurement-kit/feature/improve-output-f…
Browse files Browse the repository at this point in the history
…ormat

Output web100/summary as JSON and provide performance info
  • Loading branch information
robertodauria authored Oct 16, 2019
2 parents 5324893 + d7e9916 commit bd30e80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
13 changes: 10 additions & 3 deletions libndt-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ void BatchClient::on_result(std::string, std::string, std::string value) {
std::cout << value << std::endl;
}
// on_performance is overridded to hide the user-friendly output messages.
void BatchClient::on_performance(libndt::NettestFlags, uint8_t, double, double,
double) {
/* NOTHING */
void BatchClient::on_performance(libndt::NettestFlags tid, uint8_t nflows,
double measured_bytes,
double elapsed_time, double) {
nlohmann::json performance;
performance["ElapsedTime"] = elapsed_time;
performance["NumFlows"] = nflows;
performance["TestId"] = (int)tid;
performance["Speed"] = libndt::format_speed_from_kbits(measured_bytes,
elapsed_time);
std::cout << performance.dump() << std::endl;
}


Expand Down
26 changes: 16 additions & 10 deletions libndt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ static std::string trim(std::string s) noexcept {
return s;
}

static bool emit_result(Client *client, std::string scope,
static bool parse_result(Client *client, nlohmann::json &summary,
std::string message) noexcept {
std::stringstream ss_line{message};
std::string line;
Expand All @@ -1199,10 +1199,9 @@ static bool emit_result(Client *client, std::string scope,
}
if (keyval.size() != 2) {
LIBNDT_EMIT_WARNING_EX(client, "incorrectly formatted summary message: " << message);
return false;
continue;
}
client->on_result(scope, trim(std::move(keyval[0])),
trim(std::move(keyval[1])));
summary[trim(keyval[0])] = trim(keyval[1]);
}
return true;
}
Expand Down Expand Up @@ -1573,6 +1572,9 @@ bool Client::run_tests() noexcept {
}

bool Client::recv_results_and_logout() noexcept {
// Read summary from the server and put it into a JSON object.
nlohmann::json summary;

for (auto i = 0; i < max_loops; ++i) { // don't loop forever
std::string message;
MsgType code = MsgType{0};
Expand All @@ -1584,13 +1586,15 @@ bool Client::recv_results_and_logout() noexcept {
return false;
}
if (code == msg_logout) {
this->on_result("summary", "summary", summary.dump());
return true;
}
if (!emit_result(this, "summary", std::move(message))) {
// NOTHING: apparently ndt-cloud returns a free text message in this
// case and the warning has already been printed by emit_result(). We
// used to fail here but probably it's more robust just to warn.
}

if (!parse_result(this, summary, std::move(message))) {
// NOTHING: apparently ndt-cloud returns a free text message in this
// case and the warning has already been printed by emit_result(). We
// used to fail here but probably it's more robust just to warn.
}
}
LIBNDT_EMIT_WARNING("recv_results_and_logout: too many msg_results messages");
return false; // Too many loops
Expand Down Expand Up @@ -1751,6 +1755,7 @@ bool Client::run_download() noexcept {
}

LIBNDT_EMIT_INFO("reading summary web100 variables");
nlohmann::json web100;
for (auto i = 0; i < max_loops; ++i) { // don't loop forever
std::string message;
MsgType code = MsgType{0};
Expand All @@ -1762,9 +1767,10 @@ bool Client::run_download() noexcept {
return false;
}
if (code == msg_test_finalize) {
this->on_result("web100", "web100", web100.dump());
return true;
}
if (!emit_result(this, "web100", std::move(message))) {
if (!parse_result(this, web100, std::move(message))) {
// NOTHING: warning already printed by emit_result() and failing the whole
// test - rather than warning - because of an incorrect data format is
// probably being too strict in this context. So just keep going.
Expand Down

0 comments on commit bd30e80

Please sign in to comment.