-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathoutput_formatter_test.cc
More file actions
312 lines (265 loc) · 13 KB
/
output_formatter_test.cc
File metadata and controls
312 lines (265 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <chrono>
#include "nighthawk/common/exception.h"
#include "external/envoy/source/common/protobuf/message_validator_impl.h"
#include "external/envoy/source/common/protobuf/utility.h"
#include "external/envoy/test/test_common/file_system_for_test.h"
#include "external/envoy/test/test_common/simulated_time_system.h"
#include "api/client/options.pb.h"
#include "api/client/output.pb.h"
#include "source/client/output_collector_impl.h"
#include "source/client/output_formatter_impl.h"
#include "source/common/statistic_impl.h"
#include "source/common/version_info.h"
#include "test_common/environment.h"
#include "test_common/proto_matchers.h"
#include "test/mocks/client/mock_options.h"
#include "absl/strings/str_replace.h"
#include "gtest/gtest.h"
using namespace std::chrono_literals;
using namespace testing;
namespace Nighthawk {
namespace Client {
using ::Envoy::Protobuf::TextFormat;
using ::nighthawk::client::Protocol;
class OutputCollectorTest : public Test {
public:
OutputCollectorTest() {
StatisticPtr used_statistic = std::make_unique<StreamingStatistic>();
StatisticPtr empty_statistic = std::make_unique<StreamingStatistic>();
StatisticPtr size_statistic = std::make_unique<HdrStatistic>();
StatisticPtr latency_statistic = std::make_unique<HdrStatistic>();
used_statistic->setId("stat_id");
used_statistic->addValue(1000000);
used_statistic->addValue(2000000);
used_statistic->addValue(3000000);
size_statistic->addValue(14);
size_statistic->addValue(15);
size_statistic->addValue(16);
size_statistic->addValue(17);
size_statistic->setId("foo_size");
latency_statistic->addValue(180000);
latency_statistic->addValue(190000);
latency_statistic->addValue(200000);
latency_statistic->addValue(210000);
latency_statistic->setId("foo_latency");
statistics_.push_back(std::move(used_statistic));
statistics_.push_back(std::move(empty_statistic));
statistics_.push_back(std::move(size_statistic));
statistics_.push_back(std::move(latency_statistic));
counters_["foo"] = 1;
counters_["bar"] = 2;
time_system_.setSystemTime(std::chrono::milliseconds(1234567891567));
command_line_options_.mutable_duration()->set_seconds(1);
command_line_options_.mutable_connections()->set_value(0);
EXPECT_CALL(options_, toCommandLineOptions())
.WillOnce(Return(ByMove(
std::make_unique<nighthawk::client::CommandLineOptions>(command_line_options_))));
setupCollector();
}
void expectEqualToGoldFile(absl::string_view output, absl::string_view path) {
std::string s = readGoldFile(path);
EXPECT_EQ(s, output);
}
std::string readGoldFile(absl::string_view path) {
std::string s = Envoy::Filesystem::fileSystemForTest()
.fileReadToEnd(TestEnvironment::runfilesPath(std::string(path)))
.value();
const auto version = VersionInfo::buildVersion().version();
const std::string major = fmt::format("{}", version.major_number());
const std::string minor = fmt::format("{}", version.minor_number());
const std::string patch = fmt::format("{}", version.patch());
s = absl::StrReplaceAll(s, {{"@version_major@", major}});
s = absl::StrReplaceAll(s, {{"@version_minor@", minor}});
s = absl::StrReplaceAll(s, {{"@version_patch@", patch}});
return s;
}
void setupCollector() {
collector_ = std::make_unique<OutputCollectorImpl>(time_system_, options_);
collector_->addResult("worker_0", statistics_, counters_, 1s, time_system_.systemTime(), {});
collector_->addResult("worker_1", statistics_, counters_, 1s, absl::nullopt, {});
collector_->addResult("global", statistics_, counters_, 1s, time_system_.systemTime(), {});
}
nighthawk::client::CommandLineOptions command_line_options_;
Envoy::Event::SimulatedTimeSystem time_system_;
MockOptions options_;
std::vector<StatisticPtr> statistics_;
std::map<std::string, uint64_t> counters_;
OutputCollectorPtr collector_;
};
TEST_F(OutputCollectorTest, CliFormatter) {
ConsoleOutputFormatterImpl formatter;
expectEqualToGoldFile(*(formatter.formatProto(collector_->toProto())),
"test/test_data/output_formatter.txt.gold");
}
TEST_F(OutputCollectorTest, JsonFormatter) {
JsonOutputFormatterImpl formatter;
EXPECT_EQ((formatter.formatProto(collector_->toProto())).ok(), true);
std::string expected_str = readGoldFile("test/test_data/output_formatter.json.gold");
nighthawk::client::Output expected_output_proto, output_proto;
TextFormat::ParseFromString(expected_str, &expected_output_proto);
TextFormat::ParseFromString((formatter.formatProto(collector_->toProto())).value(),
&output_proto);
EXPECT_THAT(output_proto, EqualsProto(expected_output_proto));
}
TEST_F(OutputCollectorTest, YamlFormatter) {
YamlOutputFormatterImpl formatter;
EXPECT_EQ((formatter.formatProto(collector_->toProto())).ok(), true);
std::string expected_str = readGoldFile("test/test_data/output_formatter.yaml.gold");
nighthawk::client::Output expected_output_proto, output_proto;
TextFormat::ParseFromString(expected_str, &expected_output_proto);
TextFormat::ParseFromString((formatter.formatProto(collector_->toProto())).value(),
&output_proto);
EXPECT_THAT(output_proto, EqualsProto(expected_output_proto));
}
TEST_F(OutputCollectorTest, DottedFormatter) {
DottedStringOutputFormatterImpl formatter;
expectEqualToGoldFile((formatter.formatProto(collector_->toProto())).value(),
"test/test_data/output_formatter.dotted.gold");
}
TEST_F(OutputCollectorTest, CsvFormatter) {
CsvOutputFormatterImpl formatter;
expectEqualToGoldFile((formatter.formatProto(collector_->toProto())).value(),
"test/test_data/output_formatter.csv.gold");
}
TEST_F(OutputCollectorTest, PrometheusFormatter) {
PrometheusOutputFormatterImpl formatter;
expectEqualToGoldFile((formatter.formatProto(collector_->toProto())).value(),
"test/test_data/output_formatter.prometheus.gold");
}
TEST_F(OutputCollectorTest, GetLowerCaseOutputFormats) {
auto output_formats = OutputFormatterImpl::getLowerCaseOutputFormats();
// When you're looking at this code you probably just added an output format.
// This is to point out that you might want to update the list below and add a test above.
ASSERT_THAT(output_formats, ElementsAre("json", "human", "yaml", "dotted", "fortio",
"experimental_fortio_pedantic", "csv", "prometheus"));
}
class FortioOutputCollectorTest : public OutputCollectorTest {
public:
FortioOutputCollectorTest() {
counters_["upstream_rq_total"] = 3;
counters_["benchmark.http_2xx"] = 4;
StatisticPtr used_statistic = std::make_unique<StreamingStatistic>();
used_statistic->setId("benchmark_http_client.request_to_response");
used_statistic->addValue(4000000);
statistics_.push_back(std::move(used_statistic));
EXPECT_CALL(options_, toCommandLineOptions())
.WillOnce(Return(ByMove(
std::make_unique<nighthawk::client::CommandLineOptions>(command_line_options_))));
setupCollector();
}
};
TEST_F(FortioOutputCollectorTest, MissingGlobalResult) {
nighthawk::client::Output output_proto = collector_->toProto();
output_proto.clear_results();
FortioOutputFormatterImpl formatter;
EXPECT_FALSE((formatter.formatProto(output_proto)).ok());
}
TEST_F(FortioOutputCollectorTest, MissingGlobalResultGetGlobalResult) {
nighthawk::client::Output output_proto = collector_->toProto();
output_proto.clear_results();
FortioOutputFormatterImpl formatter;
EXPECT_FALSE((formatter.getGlobalResult(output_proto)).has_value());
}
TEST_F(FortioOutputCollectorTest, MissingCounter) {
nighthawk::client::Output output_proto = collector_->toProto();
output_proto.mutable_results(2)->clear_counters();
FortioOutputFormatterImpl formatter;
EXPECT_TRUE((formatter.formatProto(output_proto)).ok());
ASSERT_NO_THROW((formatter.formatProto(output_proto)).value());
}
TEST_F(FortioOutputCollectorTest, MissingStatistic) {
nighthawk::client::Output output_proto = collector_->toProto();
output_proto.mutable_results(2)->clear_statistics();
FortioOutputFormatterImpl formatter;
EXPECT_TRUE((formatter.formatProto(output_proto)).ok());
ASSERT_NO_THROW((formatter.formatProto(output_proto)).value());
}
TEST_F(FortioOutputCollectorTest, NoExceptions) {
nighthawk::client::Output output_proto = collector_->toProto();
FortioOutputFormatterImpl formatter;
EXPECT_TRUE((formatter.formatProto(output_proto)).ok());
ASSERT_NO_THROW((formatter.formatProto(output_proto)).value());
}
class MediumOutputCollectorTest : public OutputCollectorTest {
public:
nighthawk::client::Output loadProtoFromFile(absl::string_view path) {
nighthawk::client::Output proto;
const auto contents = Envoy::Filesystem::fileSystemForTest()
.fileReadToEnd(TestEnvironment::runfilesPath(std::string(path)))
.value();
Envoy::MessageUtil::loadFromJson(contents, proto,
Envoy::ProtobufMessage::getStrictValidationVisitor());
return proto;
}
};
TEST_F(MediumOutputCollectorTest, FortioFormatter) {
const nighthawk::client::Output input_proto =
loadProtoFromFile("test/test_data/output_formatter.medium.proto.gold");
std::string expected_str = readGoldFile("test/test_data/output_formatter.medium.fortio.gold");
nighthawk::client::Output expected_output_proto, output_proto;
TextFormat::ParseFromString(expected_str, &expected_output_proto);
FortioOutputFormatterImpl formatter;
TextFormat::ParseFromString((formatter.formatProto(input_proto)).value(), &output_proto);
EXPECT_THAT(output_proto, EqualsProto(expected_output_proto));
}
TEST_F(MediumOutputCollectorTest, FortioFormatter0sJitterUniformGetsReflected) {
nighthawk::client::Output input_proto =
loadProtoFromFile("test/test_data/output_formatter.medium.proto.gold");
FortioOutputFormatterImpl formatter;
input_proto.mutable_options()->mutable_jitter_uniform()->set_nanos(0);
input_proto.mutable_options()->mutable_jitter_uniform()->set_seconds(0);
EXPECT_NE((formatter.formatProto(input_proto)).value().find(" \"Jitter\": false,"),
std::string::npos);
}
TEST_F(MediumOutputCollectorTest, CalculatesNumThreads) {
nighthawk::client::Output input_proto =
loadProtoFromFile("test/test_data/output_formatter.medium.proto.gold");
FortioOutputFormatterImpl formatter;
absl::StatusOr<std::string> result_json = formatter.formatProto(input_proto);
ASSERT_TRUE(result_json.status().ok());
nighthawk::client::FortioResult result;
Envoy::MessageUtil::loadFromJson(*result_json, result,
Envoy::ProtobufMessage::getStrictValidationVisitor());
// Expect 300 threads (3 workers * 100 connections).
EXPECT_EQ(300, result.numthreads());
}
TEST_F(MediumOutputCollectorTest, ConsoleOutputFormatter) {
const nighthawk::client::Output input_proto =
loadProtoFromFile("test/test_data/percentile-column-overflow.json");
ConsoleOutputFormatterImpl formatter;
expectEqualToGoldFile((formatter.formatProto(input_proto)).value(),
"test/test_data/percentile-column-overflow.txt.gold");
}
class StatidToNameTest : public Test {};
TEST_F(StatidToNameTest, TestTranslations) {
// Well known id's shouldn't be returned as-is, but unknown ones should.
EXPECT_EQ(ConsoleOutputFormatterImpl::statIdtoFriendlyStatName("foo"), "foo");
const std::vector<std::string> ids = {"benchmark_http_client.queue_to_connect",
"benchmark_http_client.request_to_response",
"benchmark_http_client.response_body_size",
"benchmark_http_client.response_header_size",
"sequencer.callback",
"sequencer.blocking"};
for (const std::string& id : ids) {
EXPECT_NE(ConsoleOutputFormatterImpl::statIdtoFriendlyStatName(id), id);
}
}
TEST_F(MediumOutputCollectorTest, FortioPedanticFormatter) {
const nighthawk::client::Output input_proto =
loadProtoFromFile("test/test_data/output_formatter.medium.proto.gold");
std::string expected_str =
readGoldFile("test/test_data/output_formatter.medium.fortio-noquirks.gold");
nighthawk::client::Output expected_output_proto, output_proto;
TextFormat::ParseFromString(expected_str, &expected_output_proto);
FortioPedanticOutputFormatterImpl formatter;
TextFormat::ParseFromString((formatter.formatProto(input_proto)).value(), &output_proto);
EXPECT_THAT(output_proto, EqualsProto(expected_output_proto));
}
TEST_F(MediumOutputCollectorTest, FortioPedanticFormatterMissingGlobalResult) {
nighthawk::client::Output output_proto = collector_->toProto();
output_proto.clear_results();
FortioPedanticOutputFormatterImpl formatter;
EXPECT_FALSE((formatter.formatProto(output_proto)).ok());
}
} // namespace Client
} // namespace Nighthawk