Skip to content

Commit

Permalink
Mutator: print success_rate_variable in columns
Browse files Browse the repository at this point in the history
  • Loading branch information
atrosinenko committed Sep 24, 2023
1 parent a2d23c4 commit 3ec8802
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 35 deletions.
19 changes: 9 additions & 10 deletions runtime/mutators/tests/success-rate.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
- Usage count (queue):
never_0 (0.00), never_2 (2.00), half_2 (2.00), always_3 (3.00), rare_3 (3.00), frequent_4 (4.00)
- Usage count (all):
never_0 (0.00), never_2 (2.00), half_2 (2.00), always_3 (3.00), rare_3 (3.00), frequent_4 (4.00)
- Success (queue):
never_0 (-nan), never_2 (0.00), rare_3 (0.33), half_2 (0.50), frequent_4 (0.75), always_3 (1.00)
- Success (all):
never_0 (-nan), never_2 (0.00), rare_3 (0.33), half_2 (0.50), frequent_4 (0.75), always_3 (1.00)
- Success (queue-to-all):
never_0 (-nan), never_2 (-nan), always_3 (1.00), frequent_4 (1.00), rare_3 (1.00), half_2 (1.00)

Usage count (queue) Usage count (all) Success (queue) Success (all) Success (queue-to-all)

never_0 (0.00) never_0 (0.00) never_0 (-nan) never_0 (-nan) never_0 (-nan)
never_2 (2.00) never_2 (2.00) never_2 (0.00) never_2 (0.00) never_2 (-nan)
half_2 (2.00) half_2 (2.00) rare_3 (0.33) rare_3 (0.33) always_3 (1.00)
always_3 (3.00) always_3 (3.00) half_2 (0.50) half_2 (0.50) frequent_4 (1.00)
rare_3 (3.00) rare_3 (3.00) frequent_4 (0.75) frequent_4 (0.75) rare_3 (1.00)
frequent_4 (4.00) frequent_4 (4.00) always_3 (1.00) always_3 (1.00) half_2 (1.00)
78 changes: 53 additions & 25 deletions runtime/mutators/variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "kbdysch/mutator-defs.h"
#include "helpers.h"

#include <assert.h>

#include <algorithm>
#include <cmath>
#include <functional>
Expand Down Expand Up @@ -90,16 +92,19 @@ class success_rate_variable : public variable {
unsigned index_in_permutation,
std::function<double(unsigned)> rank);

void print_summary(FILE *stream,
const std::string &description,
void print_summary(const std::string &description,
std::function<double(unsigned)> rank);

string_variable *Labels;
counter_variable *Success;
counter_variable *Failure;

unsigned NumColumns;
std::vector<std::string> Rows;
std::vector<unsigned> Permutation; // scratch variable

const unsigned ColumnWidth = 30;
const unsigned ColumnSpacing = 2;
const unsigned NumTopElements = 10;
};

Expand All @@ -118,10 +123,10 @@ void success_rate_variable::print_label_with_rank(
}

void success_rate_variable::print_summary(
FILE *stream,
const std::string &description,
std::function<double(unsigned)> rank) {
unsigned size = Labels->num_elements_real();
NumColumns += 1;

Permutation.resize(size);
for (unsigned i = 0; i < size; ++i)
Expand All @@ -139,37 +144,54 @@ void success_rate_variable::print_summary(
return ra < rb;
});

fprintf(stream, " - %s:\n", description.c_str());
std::ostringstream out;
unsigned row_index = 0;
auto append_to_next_row = [&](const std::string &text) {
assert(row_index < Rows.size());
auto &row = Rows[row_index++];
row.append(text);
unsigned expected_size = NumColumns * ColumnWidth;
if (row.size() < expected_size)
row.append(expected_size - row.size(), ' ');
row.append(ColumnSpacing, ' ');
};

append_to_next_row(description);
append_to_next_row("");

if (size <= 2 * NumTopElements) {
// A (1.00), B (1.23), C (3.45), X (99.85)
out << " ";
for (unsigned i = 0; i < size; ++i) {
std::ostringstream out;
print_label_with_rank(out, i, rank);
if (i + 1 < size)
out << ", ";
append_to_next_row(out.str());
}
out << "\n";
} else {
// A (1.01), B (1.23), ...
// ..., U (95.12), X (99.85)
out << " ";
for (unsigned i = 0; i < NumTopElements; ++i) {
std::ostringstream out;
print_label_with_rank(out, i, rank);
out << ", ";
append_to_next_row(out.str());
}
out << "...\n";
out << " ...";
append_to_next_row("");
append_to_next_row("...");
append_to_next_row("");
for (unsigned i = size - NumTopElements; i < size; ++i) {
out << ", ";
std::ostringstream out;
print_label_with_rank(out, i, rank);
append_to_next_row(out.str());
}
out << "\n";
}
fprintf(stream, "%s", out.str().c_str());

assert(row_index == Rows.size());
}

void success_rate_variable::print(FILE *stream, unsigned var_index) {
unsigned size = Labels->num_elements_real();
bool with_ellipsis = size > 2 * NumTopElements;

NumColumns = 0;
Rows.resize(with_ellipsis ? (5 + 2 * NumTopElements) : (2 + size));
for (auto &row : Rows)
row.clear();

auto total = [this](unsigned i) {
return 0.0 + Success->get(i) + Failure->get(i);
};
Expand All @@ -182,14 +204,20 @@ void success_rate_variable::print(FILE *stream, unsigned var_index) {
auto percent_acc = [this, total_acc](unsigned i) {
return Success->get_accumulated(i) / total_acc(i);
};
fprintf(stderr, "Variable #%u: %s\n", var_index, Name.c_str());
print_summary(stream, "Usage count (queue)", total_acc);
print_summary(stream, "Usage count (all)", total);
print_summary(stream, "Success (queue)", percent_acc);
print_summary(stream, "Success (all)", percent);
print_summary(stream, "Success (queue-to-all)", [percent_acc, percent](unsigned i) {
print_summary("Usage count (queue)", total_acc);
print_summary("Usage count (all)", total);
print_summary("Success (queue)", percent_acc);
print_summary("Success (all)", percent);
print_summary("Success (queue-to-all)", [percent_acc, percent](unsigned i) {
return percent_acc(i) / percent(i);
});

fprintf(stderr, "Variable #%u: %s\n\n", var_index, Name.c_str());
for (auto &row : Rows) {
while (row.back() == ' ')
row.pop_back();
fprintf(stderr, "%s\n", row.c_str());
}
}

success_rate_variable::~success_rate_variable() {
Expand Down

0 comments on commit 3ec8802

Please sign in to comment.