cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
compressed_msgpack_reporter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
21
22#include <cstdio>
23#include <utility>
24
25#include <fmt/format.h>
26#include <msgpack_light/output_stream.h>
27#include <msgpack_light/serialize.h>
28#include <zlib.h>
29
30#include "stat_bench/reporter/msgpack_data_file_helper.h" // IWYU pragma: keep
33
34namespace stat_bench {
35namespace reporter {
36
40class CompressedMsgpackOutputFileStream : public msgpack_light::output_stream {
41public:
47 explicit CompressedMsgpackOutputFileStream(const std::string& file_path)
48 : file_(gzopen(file_path.c_str(), "wb9")) {
49 if (file_ == nullptr) {
50 throw StatBenchException(
51 fmt::format(FMT_STRING("Failed to open {}."), file_path));
52 }
53 }
54
55 CompressedMsgpackOutputFileStream(
56 const CompressedMsgpackOutputFileStream&) = delete;
57 CompressedMsgpackOutputFileStream(
58 CompressedMsgpackOutputFileStream&&) = delete;
59 auto operator=(const CompressedMsgpackOutputFileStream&)
60 -> CompressedMsgpackOutputFileStream& = delete;
61 auto operator=(CompressedMsgpackOutputFileStream&&)
62 -> CompressedMsgpackOutputFileStream& = delete;
63
67 ~CompressedMsgpackOutputFileStream() { (void)gzclose(file_); }
68
75 void write(const unsigned char* data, std::size_t size) override {
76 if (gzwrite(file_, data, size) != static_cast<int>(size)) {
77 throw StatBenchException("Failed to write data.");
78 }
79 }
80
81private:
83 gzFile file_;
84};
85
87 : DataFileReporterBase(std::move(file_path)) {}
88
90 const std::string& file_path, const data_file_spec::RootData& data) {
92
93 CompressedMsgpackOutputFileStream stream{file_path};
94 msgpack_light::serialize_to(stream, data);
95}
96
97} // namespace reporter
98} // namespace stat_bench
void write_data_file(const std::string &file_path, const data_file_spec::RootData &data) override
Write a data file.
DataFileReporterBase(std::string file_path)
Constructor.
Definition of CompressedMsgPackReporter class.
Helper of MsgPack data files.
Namespace of reporters of results of benchmarks.
void prepare_directory_for(const std::string &path)
Prepare a directory for a file.
Namespace of stat_bench source codes.
STL namespace.
Declaration of prepare_directory function.
Definition of StatBenchException class.
Struct of root objects in data files.