1414
1515#include " benchmark_runner.hpp"
1616
17+ #include < chrono>
18+ #include < cstdint>
19+ #include < fstream>
20+ #include < iomanip>
21+ #include < iostream>
22+ #include < memory>
23+ #include < sstream>
24+
1725#include " bson/bson_encoding.hpp"
1826#include " multi_doc/bulk_insert.hpp"
1927#include " multi_doc/find_many.hpp"
2634#include " single_doc/find_one_by_id.hpp"
2735#include " single_doc/insert_one.hpp"
2836#include " single_doc/run_command.hpp"
37+ #include < bsoncxx/builder/basic/array.hpp>
38+ #include < bsoncxx/builder/basic/document.hpp>
39+ #include < bsoncxx/builder/basic/kvp.hpp>
40+ #include < bsoncxx/builder/basic/sub_document.hpp>
41+ #include < bsoncxx/json.hpp>
2942#include < bsoncxx/stdx/make_unique.hpp>
43+ #include < bsoncxx/types.hpp>
3044
3145namespace benchmark {
3246
@@ -47,26 +61,27 @@ benchmark_runner::benchmark_runner(std::set<benchmark_type> types) : _types{type
4761 _microbenches.push_back (make_unique<run_command>());
4862 _microbenches.push_back (make_unique<find_one_by_id>(" single_and_multi_document/tweet.json" ));
4963 _microbenches.push_back (make_unique<insert_one>(
50- " TestSmallDocInsertOne" , 2.75 , 10000 , " single_and_multi_document/small_doc.json" ));
64+ " TestSmallDocInsertOne" , 2.75 , iterations , " single_and_multi_document/small_doc.json" ));
5165 _microbenches.push_back (make_unique<insert_one>(
5266 " TestLargeDocInsertOne" , 27.31 , 10 , " single_and_multi_document/large_doc.json" ));
5367
5468 // Multi doc microbenchmarks
5569 _microbenches.push_back (make_unique<find_many>(" single_and_multi_document/tweet.json" ));
5670 _microbenches.push_back (make_unique<bulk_insert>(
57- " TestSmallDocBulkInsert" , 2.75 , 10000 , " single_and_multi_document/small_doc.json" ));
71+ " TestSmallDocBulkInsert" , 2.75 , iterations , " single_and_multi_document/small_doc.json" ));
5872 _microbenches.push_back (make_unique<bulk_insert>(
5973 " TestLargeDocBulkInsert" , 27.31 , 10 , " single_and_multi_document/large_doc.json" ));
60- _microbenches.push_back (
61- make_unique<gridfs_upload>(" single_and_multi_document/gridfs_large.bin" ));
62- _microbenches.push_back (
63- make_unique<gridfs_download>(" single_and_multi_document/gridfs_large.bin" ));
74+ // CXX-2794: Disable GridFS benchmarks due to long runtime
75+ // _microbenches.push_back(
76+ // make_unique<gridfs_upload>("single_and_multi_document/gridfs_large.bin"));
77+ // _microbenches.push_back(
78+ // make_unique<gridfs_download>("single_and_multi_document/gridfs_large.bin"));
6479
6580 // Parallel microbenchmarks
6681 _microbenches.push_back (make_unique<json_multi_import>(" parallel/ldjson_multi" ));
6782 _microbenches.push_back (make_unique<json_multi_export>(" parallel/ldjson_multi" ));
68- _microbenches.push_back (make_unique<gridfs_multi_import>(" parallel/gridfs_multi" ));
69- _microbenches.push_back (make_unique<gridfs_multi_export>(" parallel/gridfs_multi" ));
83+ // _microbenches.push_back(make_unique<gridfs_multi_import>("parallel/gridfs_multi"));
84+ // _microbenches.push_back(make_unique<gridfs_multi_export>("parallel/gridfs_multi"));
7085
7186 // Need to remove some
7287 if (!_types.empty ()) {
@@ -89,8 +104,6 @@ benchmark_runner::benchmark_runner(std::set<benchmark_type> types) : _types{type
89104}
90105
91106void benchmark_runner::run_microbenches () {
92- mongocxx::instance instance{};
93-
94107 for (std::unique_ptr<microbench>& bench : _microbenches) {
95108 std::cout << " Starting " << bench->get_name () << " ..." << std::endl;
96109
@@ -145,20 +158,50 @@ double benchmark_runner::calculate_driver_bench_score() {
145158 return (calculate_read_bench_score () + calculate_write_bench_score ()) / 2.0 ;
146159}
147160
148- void benchmark_runner::print_scores () {
161+ void benchmark_runner::write_scores (
162+ const std::chrono::time_point<std::chrono::system_clock> start_time) {
149163 double read = -1 ;
150164 double write = -1 ;
165+ const auto end_time = std::chrono::system_clock::now ();
166+
167+ using namespace bsoncxx ;
168+ using builder::basic::sub_document;
169+
170+ auto doc = builder::basic::document{};
171+ doc.append (kvp (" info" , [](sub_document subdoc) {
172+ subdoc.append (kvp (" test_name" , " C++ microbenchmarks" ));
173+ }));
174+
175+ auto write_time =
176+ [](const std::chrono::time_point<std::chrono::system_clock> t) -> std::string {
177+ std::time_t t1 = std::chrono::system_clock::to_time_t (t);
178+ std::ostringstream oss;
179+ oss << std::put_time (std::gmtime (&t1), " %Y-%m-%dT%H:%M:%S" ) << " +00:00" ;
180+ return oss.str ();
181+ };
182+ doc.append (kvp (" created_at" , write_time (start_time)));
183+ doc.append (kvp (" completed_at" , write_time (end_time)));
184+ doc.append (kvp (" artifacts" , builder::basic::make_array ()));
185+
186+ auto metrics_array = builder::basic::array{};
187+ std::cout << std::endl << " Composite benchmarks:" << std::endl << " ===========" << std::endl;
151188
152189 std::cout << " Individual microbenchmark scores:" << std::endl << " ===========" << std::endl;
153190 for (auto && bench : _microbenches) {
154191 auto & score = bench->get_results ();
192+ const auto bench_time = static_cast <double >(score.get_percentile (50 ).count ()) / 1000.0 ;
155193
156- std::cout << bench->get_name () << " : "
157- << static_cast <double >(score.get_percentile (50 ).count ()) / 1000.0
158- << " second(s) | " << score.get_score () << " MB/s" << std::endl;
159- }
194+ std::cout << bench->get_name () << " : " << bench_time << " seconds | " << score.get_score ()
195+ << " MB/s" << std::endl;
160196
161- std::cout << std::endl << " Composite benchmarks:" << std::endl << " ===========" << std::endl;
197+ auto metric_doc = builder::basic::document{};
198+ metric_doc.append (kvp (" name" , bench->get_name ()));
199+ metric_doc.append (kvp (" type" , " THROUGHPUT" ));
200+ metric_doc.append (kvp (" value" , score.get_score ()));
201+ metrics_array.append (metric_doc);
202+ }
203+ doc.append (kvp (" metrics" , metrics_array));
204+ doc.append (kvp (" sub_tests" , builder::basic::make_array ()));
162205
163206 auto print_comp = [this , &read, &write](benchmark_type type) {
164207 double avg = calculate_average (type);
@@ -169,7 +212,7 @@ void benchmark_runner::print_scores() {
169212 write = avg;
170213 }
171214
172- std::cout << type_names[ type] << " " << avg << " MB/s" << std::endl;
215+ std::cout << type_names. at ( type) << " " << avg << " MB/s" << std::endl;
173216 };
174217
175218 if (!_types.empty ()) {
@@ -185,5 +228,8 @@ void benchmark_runner::print_scores() {
185228 if (read > 0 && write > 0 ) {
186229 std::cout << " DriverBench: " << (read + write) / 2.0 << " MB/s" << std::endl;
187230 }
231+
232+ std::ofstream os{" results.json" };
233+ os << ' [' << bsoncxx::to_json (doc.view ()) << ' ]' ;
188234}
189235} // namespace benchmark
0 commit comments