File: stream_input_benchmark.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (335 lines) | stat: -rw-r--r-- 9,814 bytes parent folder | download
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------

#include <benchmark/benchmark.h>

#include <iostream>
#include <memory>
#include <sstream>

#include <seqan3/io/stream/iterator.hpp>

#ifdef SEQAN3_HAS_ZLIB
    #include <seqan3/contrib/stream/bgzf_istream.hpp>
    #include <seqan3/contrib/stream/bgzf_ostream.hpp>
    #include <seqan3/contrib/stream/gz_istream.hpp>
    #include <seqan3/contrib/stream/gz_ostream.hpp>
#endif

// only benchmark BZIP2 if explicitly requested, because slow setup
#if !defined(SEQAN3_BENCH_BZIP2) && defined(SEQAN3_HAS_BZIP2)
    #undef SEQAN3_HAS_BZIP2
#endif

#ifdef SEQAN3_HAS_BZIP2
    #include <seqan3/contrib/stream/bz2_istream.hpp>
    #include <seqan3/contrib/stream/bz2_ostream.hpp>
#endif

// SEQAN2
#if __has_include(<seqan/stream.h>)
    #define SEQAN3_HAS_SEQAN2 1

    #ifdef SEQAN3_HAS_ZLIB
        #define SEQAN_HAS_ZLIB 1
    #endif

    #ifdef SEQAN3_HAS_BZIP2
        #define SEQAN_HAS_BZIP2 1
    #endif

    #include <seqan/stream.h>
#endif

std::string input
{
    [] ()
    {
        std::string line{"The quick brown fox jumps over the lazy dog"};
        std::string ret;
        for (size_t i = 0; i < 10'000'000; ++i)
            ret += line;
        return ret;
    } ()
};

template <typename t>
std::string const input_comp;

#ifdef SEQAN3_HAS_SEQAN2
template <>
std::string const & input_comp<seqan::Nothing> = input;
#endif

#ifdef SEQAN3_HAS_ZLIB
template <>
std::string const input_comp<seqan3::contrib::gz_istream>
{
    [] ()
    {
        std::ostringstream ret;
        { // In scope to force flush of ostream on destruction.
            seqan3::contrib::gz_ostream os{ret};
            std::copy(input.begin(), input.end(), std::ostreambuf_iterator<char>(os));
        }
        return ret.str();
    } ()
};

template <>
std::string const input_comp<seqan3::contrib::bgzf_istream>
{
    [] ()
    {
        std::ostringstream ret;
        { // In scope to force flush of ostream on destruction.
            seqan3::contrib::bgzf_ostream os{ret};
            std::copy(input.begin(), input.end(), std::ostreambuf_iterator<char>(os));
        }
        return ret.str();
    } ()
};
#ifdef SEQAN3_HAS_SEQAN2
template <>
std::string const & input_comp<seqan::GZFile> = input_comp<seqan3::contrib::gz_istream>;

template <>
std::string const & input_comp<seqan::BgzfFile> = input_comp<seqan3::contrib::bgzf_istream>;
#endif
#endif

#ifdef SEQAN3_HAS_BZIP2
template <>
std::string const input_comp<seqan3::contrib::bz2_istream>
{
    [] ()
    {
        std::ostringstream ret;
        { // In scope to force flush of ostream on destruction.
            seqan3::contrib::bz2_ostream os{ret};
            std::copy(input.begin(), input.end(), std::ostreambuf_iterator<char>(os));
        }
        return ret.str();
    } ()
};
#ifdef SEQAN3_HAS_SEQAN2
template <>
std::string const & input_comp<seqan::BZ2File> = input_comp<seqan3::contrib::bz2_istream>;
#endif
#endif

// ============================================================================
//  plain benchmark of ostringstream
// ============================================================================

void uncompressed(benchmark::State & state)
{
    std::istringstream s{input};
    size_t i = 0;
    for (auto _ : state)
    {
        s.clear();
        s.seekg(0, std::ios::beg);
        seqan3::detail::fast_istreambuf_iterator<char> it{*s.rdbuf()};

        for (; it != std::default_sentinel; ++it)
            i += *it;
    }

    state.counters["iterations_per_run"] = i;
}

BENCHMARK(uncompressed);

// ============================================================================
//  compression applied
// ============================================================================

template <typename compressed_istream_t>
void compressed(benchmark::State & state)
{
    std::istringstream s{input_comp<compressed_istream_t>};

    size_t i = 0;
    for (auto _ : state)
    {
        s.clear();
        s.seekg(0, std::ios::beg);
        compressed_istream_t comp{s};
        seqan3::detail::fast_istreambuf_iterator<char> it{*comp.rdbuf()};

        for (size_t v = 0; v < input_comp<compressed_istream_t>.size(); ++v)
        {
            i += *it;
            ++it;
        }
    }

    state.counters["iterations_per_run"] = i;
}

#ifdef SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::gz_istream);
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::bgzf_istream);
#endif

#ifdef SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::bz2_istream);
#endif

// ============================================================================
//  compression applied, but stuffed into plain istream
// ============================================================================

template <typename compressed_istream_t>
void compressed_type_erased(benchmark::State & state)
{
    std::istringstream s{input_comp<compressed_istream_t>};
    size_t i = 0;
    for (auto _ : state)
    {
        s.clear();
        s.seekg(0, std::ios::beg);
        std::unique_ptr<std::istream> comp{new compressed_istream_t{s}};
        seqan3::detail::fast_istreambuf_iterator<char> it{*comp->rdbuf()};

        for (size_t v = 0; v < input_comp<compressed_istream_t>.size(); ++v)
        {
            i += *it;
            ++it;
        }
    }

    state.counters["iterations_per_run"] = i;
}

#ifdef SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::gz_istream);
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::bgzf_istream);
#endif
#ifdef SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::bz2_istream);
#endif

// ============================================================================
//  compression applied, but stuffed into plain istream, also stringstream erased
// ============================================================================

template <typename compressed_istream_t>
void compressed_type_erased2(benchmark::State & state)
{
    std::unique_ptr<std::istream> s{new std::istringstream{input_comp<compressed_istream_t>}};
    size_t i = 0;
    for (auto _ : state)
    {
        s->clear();
        s->seekg(0, std::ios::beg);
        std::unique_ptr<std::istream> comp{new compressed_istream_t{*s}};
        seqan3::detail::fast_istreambuf_iterator<char> it{*comp->rdbuf()};

        for (size_t v = 0; v < input_comp<compressed_istream_t>.size(); ++v)
        {
            i += *it;
            ++it;
        }
    }

    state.counters["iterations_per_run"] = i;
}

#ifdef SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::gz_istream);
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::bgzf_istream);
#endif
#ifdef SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::bz2_istream);
#endif

// ============================================================================
//  seqan2 virtual stream
// ============================================================================

#ifdef SEQAN3_HAS_SEQAN2

void seqan2_uncompressed(benchmark::State & state)
{
    std::istringstream s{input};
    size_t i = 0;
    for (auto _ : state)
    {
        s.clear();
        s.seekg(0, std::ios::beg);
        auto it = seqan::Iter<std::istringstream, seqan::StreamIterator<seqan::Input> >(s);

        for (size_t v = 0; v < input.size(); ++v)
        {
            i += *it;
            ++it;
        }
    }

    state.counters["iterations_per_run"] = i;
}

template <typename compression_t, typename stream_t>
void seqan2_compressed_impl(benchmark::State & state)
{
    std::istringstream s{input_comp<compression_t>};
    size_t i = 0;
    for (auto _ : state)
    {
        s.clear();
        s.seekg(0, std::ios::beg);
        stream_t comp{s};
        auto it = seqan::Iter<std::istringstream, seqan::StreamIterator<seqan::Input> >(comp);

        for (size_t v = 0; v < input_comp<compression_t>.size(); ++v)
        {
            i += *it;
            ++it;
        }
    }

    state.counters["iterations_per_run"] = i;
}

template <typename compression_type>
void seqan2_compressed(benchmark::State & state)
{
    #ifdef SEQAN_HAS_ZLIB
    if constexpr (std::is_same_v<compression_type, seqan::GZFile>)
        seqan2_compressed_impl<seqan::GZFile, zlib_stream::zip_istream>(state);
    else if constexpr (std::is_same_v<compression_type, seqan::BgzfFile>)
        seqan2_compressed_impl<seqan::BgzfFile, seqan::bgzf_istream>(state);
    #endif // SEQAN_HAS_ZLIB

    #ifdef SEQAN_HAS_BZIP2
    if constexpr (std::is_same_v<compression_type, seqan::BZ2File>)
        seqan2_compressed_impl<seqan::BZ2File, bzip2_stream::bzip2_istream>(state);
    #endif // SEQAN_HAS_BZIP2

    if constexpr (std::is_same_v<compression_type, seqan::Nothing>)
        seqan2_uncompressed(state);
}

BENCHMARK_TEMPLATE(seqan2_compressed, seqan::Nothing);

#ifdef SEQAN_HAS_ZLIB
BENCHMARK_TEMPLATE(seqan2_compressed, seqan::GZFile);
BENCHMARK_TEMPLATE(seqan2_compressed, seqan::BgzfFile);
#endif
#ifdef SEQAN_HAS_BZIP2
BENCHMARK_TEMPLATE(seqan2_compressed, seqan::BZ2File);
#endif

#endif // SEQAN3_HAS_SEQAN2

// ============================================================================
//  instantiate tests
// ============================================================================

BENCHMARK_MAIN();