make use of new float parsing function

i've also moved the input validation checks to the config parser, so that
warnings aren't displayed in the terminal output every time a benchmark is
run with an invalid percentile entry.
pull/236/head
Kingsley McDonald 4 years ago
parent 979c4d32ca
commit 0e1fe708c8

@ -817,26 +817,31 @@ void calculate_benchmark_data(void *params_void){
benchmark.total = benchmark.total + fps_;
}
size_t max_label_size = 0;
for (std::string percentile : params->benchmark_percentiles) {
float result;
// special case handling for a mean-based average
if (percentile.find("AVG") == 0) {
if (percentile == "AVG") {
result = benchmark.total / sorted.size();
}
// everything else is used to calculate percentiles
else {
char *endChar;
float fraction = std::strtof(percentile.c_str(), &endChar) / 100;
} else {
// the percentiles are already validated when they're parsed from the config.
float fraction = parse_float(percentile) / 100;
if (*endChar != '%' || fraction <= 0.0 || fraction > 1.0) {
std::cerr << "MANGOHUD: Invalid percentile value for benchmark: '" << percentile << "'\n";
continue;
}
result = sorted[(fraction * sorted.size()) - 1];
percentile += "%";
}
if (percentile.length() > max_label_size)
max_label_size = percentile.length();
benchmark.percentile_data.push_back({percentile, result});
}
for (auto& entry : benchmark.percentile_data) {
entry.first.append(max_label_size - entry.first.length(), ' ');
}
}
void update_hud_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID){

@ -206,27 +206,38 @@ static std::vector<std::string>
parse_benchmark_percentiles(const char *str)
{
std::vector<std::string> percentiles;
std::stringstream percentStrings(str);
std::stringstream percent_strings(str);
std::string value;
size_t maxLength = 0; // for padding later on
while (std::getline(percentStrings, value, '+')) {
while (std::getline(percent_strings, value, '+')) {
trim(value);
// everything except AVG is assumed to be a percentile.
if (value != "AVG")
value += '%';
if (value == "AVG") {
percentiles.push_back(value);
continue;
}
percentiles.push_back(value);
float as_float;
size_t float_len = 0;
if (value.length() > maxLength)
maxLength = value.length();
}
try {
as_float = parse_float(value, &float_len);
} catch (const std::invalid_argument&) {
std::cerr << "MANGOHUD: invalid benchmark percentile: '" << value << "'\n";
continue;
}
// now reiterate over the strings and add equal padding
for (size_t i = 0; i < percentiles.size(); i++) {
percentiles[i].append(maxLength - percentiles[i].length(), ' ');
if (float_len != value.length()) {
std::cerr << "MANGOHUD: invalid benchmark percentile: '" << value << "'\n";
continue;
}
if (as_float > 100 || as_float < 0) {
std::cerr << "MANGOHUD: benchmark percentile is not between 0 and 100 (" << value << ")\n";
continue;
}
percentiles.push_back(value);
}
return percentiles;
@ -423,7 +434,7 @@ parse_overlay_config(struct overlay_params *params,
params->log_interval = 100;
params->media_player_order = { MP_ORDER_TITLE, MP_ORDER_ARTIST, MP_ORDER_ALBUM };
params->permit_upload = 0;
params->benchmark_percentiles = { "97% ", "AVG ", "1% ", "0.1%" };
params->benchmark_percentiles = { "97", "AVG", "1", "0.1" };
#ifdef HAVE_X11
params->toggle_hud = { XK_Shift_R, XK_F12 };

Loading…
Cancel
Save