xrpld
Loading...
Searching...
No Matches
reporter.h
1// Distributed under the Boost Software License, Version 1.0. (See accompanying
2// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
3//
4
5#pragma once
6
7#include <xrpl/beast/unit_test/amount.h>
8#include <xrpl/beast/unit_test/runner.h>
9#include <xrpl/beast/unit_test/suite_info.h>
10
11#include <boost/optional.hpp>
12
13#include <algorithm>
14#include <chrono>
15#include <cstddef>
16#include <iomanip>
17#include <iostream>
18#include <sstream>
19#include <string>
20#include <utility>
21#include <vector>
22
23namespace beast::unit_test {
24
25namespace detail {
26
31template <class = void>
32class Reporter : public Runner
33{
34private:
36
38 {
42
43 explicit CaseResults(std::string name = "") : name(std::move(name))
44 {
45 }
46 };
47
49 {
54 ClockType::time_point start = ClockType::now();
55
56 explicit SuiteResults(std::string name = "") : name(std::move(name))
57 {
58 }
59
60 void
61 add(CaseResults const& r);
62 };
63
64 struct Results
65 {
67
68 static constexpr auto kMaxTop = 10;
69
75 ClockType::time_point start = ClockType::now();
76
77 void
78 add(SuiteResults const& r);
79 };
80
85
86public:
87 Reporter(Reporter const&) = delete;
89 operator=(Reporter const&) = delete;
90
91 ~Reporter() override;
92
93 explicit Reporter(std::ostream& os = std::cout);
94
95private:
96 static std::string
97 fmtdur(ClockType::duration const& d);
98
99 void
100 onSuiteBegin(SuiteInfo const& info) override;
101
102 void
103 onSuiteEnd() override;
104
105 void
106 onCaseBegin(std::string const& name) override;
107
108 void
109 onCaseEnd() override;
110
111 void
112 onPass() override;
113
114 void
115 onFail(std::string const& reason) override;
116
117 void
118 onLog(std::string const& s) override;
119};
120
121//------------------------------------------------------------------------------
122
123template <class Unused>
124void
126{
127 ++cases;
128 total += r.total;
129 failed += r.failed;
130}
131
132template <class Unused>
133void
135{
136 ++suites;
137 total += r.total;
138 cases += r.cases;
139 failed += r.failed;
140 auto const elapsed = ClockType::now() - r.start;
141 if (elapsed >= std::chrono::seconds{1})
142 {
143 auto const iter = std::lower_bound(
144 top.begin(), top.end(), elapsed, [](RunTime const& t1, ClockType::duration const& t2) {
145 return t1.second > t2;
146 });
147 if (iter != top.end())
148 {
149 if (top.size() == kMaxTop)
150 top.resize(top.size() - 1);
151 top.emplace(iter, r.name, elapsed);
152 }
153 else if (top.size() < kMaxTop)
154 {
155 top.emplace_back(r.name, elapsed);
156 }
157 }
158}
159
160//------------------------------------------------------------------------------
161
162template <class Unused>
166
167template <class Unused>
169{
170 if (results_.top.size() > 0)
171 {
172 os_ << "Longest suite times:\n";
173 for (auto const& i : results_.top)
174 os_ << std::setw(8) << fmtdur(i.second) << " " << i.first << '\n';
175 }
176 auto const elapsed = ClockType::now() - results_.start;
177 os_ << fmtdur(elapsed) << ", " << Amount{results_.suites, "suite"} << ", "
178 << Amount{results_.cases, "case"} << ", " << Amount{results_.total, "test"} << " total, "
179 << Amount{results_.failed, "failure"} << std::endl;
180}
181
182template <class Unused>
184Reporter<Unused>::fmtdur(ClockType::duration const& d)
185{
186 using namespace std::chrono;
187 auto const ms = duration_cast<milliseconds>(d);
188 if (ms < seconds{1})
189 return std::to_string(ms.count()) + "ms";
191 ss << std::fixed << std::setprecision(1) << (ms.count() / 1000.) << "s";
192 return ss.str();
193}
194
195template <class Unused>
196void
201
202template <class Unused>
203void
208
209template <class Unused>
210void
212{
214 os_ << suiteResults_.name << (caseResults_.name.empty() ? "" : (" " + caseResults_.name))
215 << std::endl;
216}
217
218template <class Unused>
219void
224
225template <class Unused>
226void
228{
229 ++caseResults_.total;
230}
231
232template <class Unused>
233void
235{
236 ++caseResults_.failed;
237 ++caseResults_.total;
238 os_ << "#" << caseResults_.total << " failed" << (reason.empty() ? "" : ": ") << reason
239 << std::endl;
240}
241
242template <class Unused>
243void
245{
246 os_ << s;
247}
248
249} // namespace detail
250
252
253// TODO [#8340]: Legacy name still used by external consumers of libxrpl (e.g. validator-keys-tool).
254// Do not use in new code; remove once those consumers have migrated.
255using reporter = Reporter; // NOLINT(readability-identifier-naming)
256
257} // namespace beast::unit_test
Utility for producing nicely composed output of amounts with units.
Associates a unit test type with metadata.
Definition suite_info.h:20
std::string fullName() const
Return the canonical suite name as a string.
Definition suite_info.h:78
A simple test runner that writes everything to a stream in real time.
Definition reporter.h:33
static std::string fmtdur(ClockType::duration const &d)
Definition reporter.h:184
void onSuiteBegin(SuiteInfo const &info) override
Called when a new suite starts.
Definition reporter.h:197
void onFail(std::string const &reason) override
Called for each failing condition.
Definition reporter.h:234
Reporter(Reporter const &)=delete
std::chrono::steady_clock ClockType
Definition reporter.h:35
void onCaseEnd() override
Called when a new case ends.
Definition reporter.h:220
void onSuiteEnd() override
Called when a suite ends.
Definition reporter.h:204
void onPass() override
Called for each passing condition.
Definition reporter.h:227
Reporter & operator=(Reporter const &)=delete
void onCaseBegin(std::string const &name) override
Called when a new case starts.
Definition reporter.h:211
void onLog(std::string const &s) override
Called when a test logs output.
Definition reporter.h:244
T duration_cast(T... args)
T empty(T... args)
T endl(T... args)
T fixed(T... args)
T lower_bound(T... args)
Reporter reporter
Definition reporter.h:255
detail::Reporter<> Reporter
Definition reporter.h:251
STL namespace.
T setprecision(T... args)
T setw(T... args)
T str(T... args)
std::pair< std::string, ClockType::duration > RunTime
Definition reporter.h:66
void add(SuiteResults const &r)
Definition reporter.h:134
T to_string(T... args)