10 logger = makeAuto<StdOutLogger>();
21 instance = makeAuto<Session>();
28 if (b->getName() == benchmark->getName()) {
29 status =
makeFailed(
"Benchmark " + b->getName() +
" defined more than once");
33 benchmarks.push(benchmark);
34 Group&
group = this->getGroupByName(groupName);
35 group.addBenchmark(benchmark);
39 Outcome result = this->parseArgs(argc, argv);
41 this->logError(result.
error());
45 this->log(
"Warning: running benchmark in debugging build");
48 this->logError(status.
error());
53 if (params.flags.has(Flag::MAKE_BASELINE)) {
55 params.baseline.path = this->getBaselinePath();
57 }
else if (params.flags.has(Flag::RUN_AGAINST_BASELINE)) {
59 if (!
baseline.parse(this->getBaselinePath())) {
60 this->logError(
"Invalid baseline format");
66 if (!params.benchmarksToRun.empty()) {
69 if (std::find(names.
begin(), names.
end(), b->getName()) == names.
end()) {
75 if (params.flags.has(Flag::RUN_AGAINST_BASELINE)) {
76 if (!
baseline.isRecorded(b->getName())) {
77 params.target.iterateCnt = 1;
79 params.target.iterateCnt =
baseline[b->getName()].iterateCnt;
84 this->logError(
"Fail");
86 if (act->duration > params.maxAllowedDuration) {
88 "Warning: benchmark ", b->getName(),
" is takes too much time, t = ", act->duration);
90 if (params.flags.has(Flag::RUN_AGAINST_BASELINE)) {
91 if (
baseline.isRecorded(b->getName())) {
94 this->log(b->getName() +
" ran " + std::to_string(act->iterateCnt) +
" iterations");
95 this->compareResults(act.
value(), result);
97 this->log(b->getName() +
" not recorded in the baseline");
100 this->log(b->getName(),
115 if (params.flags.has(Flag::MAKE_BASELINE)) {
116 this->writeBaseline(b->getName(), act.
value());
120 }
catch (
const std::exception& e) {
121 this->logError(
"Exception caught in benchmark " + b->getName() +
":\n" + e.what());
127 Path Session::getBaselinePath() {
131 this->logError(
"Cannot determine git commit SHA");
134 this->log(
"Baseline for commit " + sha.
value());
138 void Session::writeBaseline(
const std::string& name,
const Result& measured) {
146 void Session::compareResults(
const Result& measured,
const Result& baseline) {
152 }
else if (diff > sigma) {
161 Group& Session::getGroupByName(
const std::string& groupName) {
163 if (
group.getName() == groupName) {
168 groups.emplaceBack(groupName);
169 return groups[groups.size() - 1];
172 Outcome Session::parseArgs(
int argc,
char* argv[]) {
173 for (
int i = 1; i < argc; ++i) {
174 std::string arg = argv[i];
177 params.flags.set(Flag::MAKE_BASELINE);
178 }
else if (arg ==
"-r") {
179 params.flags.set(Flag::RUN_AGAINST_BASELINE);
181 params.baseline.commit = std::stoi(argv[i + 1]);
184 }
else if (arg ==
"--help") {
187 }
else if (arg[0] !=
'-') {
189 if (arg[0] ==
'[' && arg[arg.size() - 1] ==
']') {
192 for (
auto& unit :
group) {
193 params.benchmarksToRun.push(unit->getName());
197 if (arg[0] ==
'\"' && arg[arg.size() - 1] ==
'\"') {
199 arg = arg.substr(1, arg.size() - 2);
201 params.benchmarksToRun.push(arg);
208 void Session::printHelp() {
209 logger->
write(
"Benchmark. Options:\n -b Create baseline");
212 template <
typename... TArgs>
213 void Session::log(TArgs&&... args) {
214 if (!params.flags.has(Flag::SILENT)) {
215 logger->
write(std::forward<TArgs>(args)...);
219 template <
typename... TArgs>
220 void Session::logError(TArgs&&... args) {
223 logger->
write(std::forward<TArgs>(args)...);
#define SPH_ASSERT(x,...)
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Logging routines of the run.
INLINE T sqrt(const T f)
Return a squared root of a value.
BasicOutcome< std::string > Outcome
Alias for string error message.
const SuccessTag SUCCESS
Global constant for successful outcome.
INLINE Outcome makeFailed(TArgs &&... args)
Constructs failed object with error message.
@ RUN_AGAINST_BASELINE
Compare iteration numbers with recorded baseline.
@ MAKE_BASELINE
Store iteration numbers to baseline file.
#define NAMESPACE_BENCHMARK_BEGIN
#define NAMESPACE_BENCHMARK_END
INLINE Iterator< StorageType > end() noexcept
INLINE Iterator< StorageType > begin() noexcept
INLINE const TError & error() const
Returns the error message.
Wrapper of type that either contains a value of given type, or an error message.
Type & value()
Returns the reference to expected value.
void setPrecision(const Size newPrecision)
Changes the precision of printed numbers.
void write(TArgs &&... args)
Creates and logs a message by concatenating arguments.
void setScientific(const bool newScientific)
Sets/unsets scientific notation.
Object representing a path on a filesystem.
Register(const SharedPtr< Unit > &benchmark, const std::string &groupName)
void run(int argc, char *argv[])
Runs all benchmarks.
struct Session::@0::@1 baseline
std::string group
Run only selected group of benchmarks.
void registerBenchmark(const SharedPtr< Unit > &benchmark, const std::string &groupName)
Adds a new benchmark into the session.
static Session & getInstance()
Outcome removePath(const Path &path, const Flags< RemovePathFlag > flags=EMPTY_FLAGS)