Add --markdown / --csv options to option_parser.

This commit is contained in:
Allison Vacanti
2021-03-01 17:13:11 -05:00
parent 630aefda93
commit 52fbbbcc7a
3 changed files with 128 additions and 4 deletions

View File

@@ -3,8 +3,8 @@
#include <nvbench/benchmark_base.cuh>
#include <nvbench/benchmark_manager.cuh>
#include <nvbench/cuda_call.cuh>
#include <nvbench/markdown_format.cuh>
#include <nvbench/option_parser.cuh>
#include <nvbench/output_format.cuh>
#include <iostream>
@@ -32,8 +32,8 @@
{ \
nvbench::option_parser parser; \
parser.parse(argc, argv); \
auto &printer = parser.get_printer(); \
\
nvbench::markdown_format printer(std::cout); \
printer.print_device_info(); \
printer.print_log_preamble(); \
auto &benchmarks = parser.get_benchmarks(); \

View File

@@ -1,17 +1,22 @@
#include <nvbench/option_parser.cuh>
#include <nvbench/benchmark_base.cuh>
#include <nvbench/benchmark_manager.cuh>
#include <nvbench/csv_format.cuh>
#include <nvbench/markdown_format.cuh>
#include <nvbench/output_format.cuh>
#include <nvbench/range.cuh>
#include <nvbench/detail/throw.cuh>
#include <nvbench/markdown_format.cuh>
#include <fmt/format.h>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <memory>
#include <regex>
#include <stdexcept>
#include <string>
@@ -265,6 +270,10 @@ auto parse_axis_key_flag_value_spec(const std::string &spec)
namespace nvbench
{
// Defined here to avoid including <fstream> in the header.
option_parser::option_parser() = default;
option_parser::~option_parser() = default;
void option_parser::parse(int argc, char const *const *argv)
{
m_args.clear();
@@ -331,6 +340,18 @@ void option_parser::parse_range(option_parser::arg_iterator_t first,
this->print_list();
std::exit(0);
}
else if (arg == "--markdown" || arg == "--md")
{
check_params(1);
this->add_markdown_format(first[1]);
first += 2;
}
else if (arg == "--csv")
{
check_params(1);
this->add_csv_format(first[1]);
first += 2;
}
else if (arg == "--benchmark" || arg == "-b")
{
check_params(1);
@@ -371,6 +392,67 @@ void option_parser::parse_range(option_parser::arg_iterator_t first,
}
}
void option_parser::add_markdown_format(const std::string &spec)
try
{
std::ostream &stream = this->output_format_spec_to_ostream(spec);
m_printer.emplace<nvbench::markdown_format>(stream);
}
catch (std::exception &e)
{
NVBENCH_THROW(std::runtime_error,
"Error while adding markdown output for `{}`:\n{}",
spec,
e.what());
}
void option_parser::add_csv_format(const std::string &spec)
try
{
std::ostream &stream = this->output_format_spec_to_ostream(spec);
m_printer.emplace<nvbench::csv_format>(stream);
}
catch (std::exception &e)
{
NVBENCH_THROW(std::runtime_error,
"Error while adding csv output for `{}`:\n{}",
spec,
e.what());
}
std::ostream &
option_parser::output_format_spec_to_ostream(const std::string &spec)
{
if (spec == "stdout")
{
return std::cout;
}
else if (spec == "stderr")
{
return std::cerr;
}
else
{
m_ofstream_storage.push_back(std::make_unique<std::ofstream>());
auto &file_stream = *m_ofstream_storage.back();
// Throw if file can't open
file_stream.exceptions(file_stream.exceptions() | std::ios::failbit);
try
{
file_stream.open(spec);
}
catch (...)
{
m_ofstream_storage.pop_back();
throw;
}
return file_stream;
}
}
void option_parser::print_list() const
{
const auto &bench_mgr = nvbench::benchmark_manager::get();
@@ -669,4 +751,13 @@ catch (std::exception &e)
e.what());
}
nvbench::output_format &option_parser::get_printer()
{
if (m_printer.get_output_count() == 0)
{
this->add_markdown_format("stdout");
}
return m_printer;
}
} // namespace nvbench

View File

@@ -1,7 +1,8 @@
#pragma once
#include <nvbench/benchmark_base.cuh>
#include <nvbench/output_multiplex.cuh>
#include <iosfwd>
#include <memory>
#include <string>
#include <vector>
@@ -9,6 +10,13 @@
namespace nvbench
{
struct benchmark_base;
struct float64_axis;
struct int64_axis;
struct output_format;
struct string_axis;
struct type_axis;
/**
* Parses command-line args into a set of benchmarks.
*/
@@ -17,6 +25,9 @@ struct option_parser
using benchmark_vector =
std::vector<std::unique_ptr<nvbench::benchmark_base>>;
option_parser();
~option_parser();
void parse(int argc, char const *const argv[]);
void parse(std::vector<std::string> args);
@@ -31,12 +42,29 @@ struct option_parser
return m_args;
}
/*!
* Returns the output format requested by the parse options.
*
* If no output format requested, markdown + stdout are used.
*
* If multiple formats requested, an output_multiple is used.
*
* The returned object is only valid for the lifetime of this option_parser.
*/
// output_format has no useful const API, so no const overload.
[[nodiscard]] nvbench::output_format &get_printer();
private:
void parse_impl();
using arg_iterator_t = std::vector<std::string>::const_iterator;
void parse_range(arg_iterator_t first, arg_iterator_t last);
void add_markdown_format(const std::string &spec);
void add_csv_format(const std::string &spec);
std::ostream &output_format_spec_to_ostream(const std::string &spec);
void print_list() const;
void add_benchmark(const std::string &name);
@@ -70,6 +98,11 @@ private:
// "global args". Replay them after every benchmark.
std::vector<std::string> m_global_args;
benchmark_vector m_benchmarks;
// Manages lifetimes of any ofstreams opened for m_printer.
std::vector<std::unique_ptr<std::ofstream>> m_ofstream_storage;
nvbench::output_multiplex m_printer;
};
} // namespace nvbench