File: stream_output_benchmark.cpp

package info (click to toggle)
seqan3 3.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,580 kB
  • sloc: cpp: 145,192; sh: 307; xml: 264; javascript: 95; makefile: 70; perl: 29; php: 15
file content (163 lines) | stat: -rw-r--r-- 4,892 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
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause

#include <benchmark/benchmark.h>

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

#if SEQAN3_HAS_ZLIB
#    include <seqan3/contrib/stream/bgzf_ostream.hpp>
#    include <seqan3/contrib/stream/gz_ostream.hpp>
#endif // SEQAN3_HAS_ZLIB

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

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

#    if SEQAN3_HAS_ZLIB
#        define SEQAN_HAS_ZLIB 1
#    endif // SEQAN3_HAS_ZLIB

#    if SEQAN3_HAS_BZIP2
#        define SEQAN_HAS_BZIP2 1
#    endif // SEQAN3_HAS_BZIP2

#    include <seqan/stream.h>
#endif

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

void uncompressed(benchmark::State & state)
{
    std::ostringstream os;

    std::ostreambuf_iterator<char> oit{os};

    size_t i = 0;
    for (auto _ : state)
        oit = static_cast<char>(i++ % 128);
}
BENCHMARK(uncompressed);

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

template <typename compressed_ostream_t>
void compressed(benchmark::State & state)
{
    std::ostringstream os;

    compressed_ostream_t ogzf{os};

    std::ostreambuf_iterator<char> oit{ogzf};

    size_t i = 0;
    for (auto _ : state)
        oit = static_cast<char>(i++ % 128);
}

#if SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::gz_ostream);
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::bgzf_ostream);
#endif // SEQAN3_HAS_ZLIB
#if SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed, seqan3::contrib::bz2_ostream);
#endif // SEQAN3_HAS_BZIP2

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

template <typename compressed_ostream_t>
void compressed_type_erased(benchmark::State & state)
{
    std::ostringstream os;

    std::unique_ptr<std::ostream> ogzf{new compressed_ostream_t{os}};

    std::ostreambuf_iterator<char> oit{*ogzf};

    size_t i = 0;
    for (auto _ : state)
        oit = static_cast<char>(i++ % 128);
}

#if SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::gz_ostream);
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::bgzf_ostream);
#endif // SEQAN3_HAS_ZLIB
#if SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed_type_erased, seqan3::contrib::bz2_ostream);
#endif // SEQAN3_HAS_BZIP2

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

template <typename compressed_ostream_t>
void compressed_type_erased2(benchmark::State & state)
{
    std::unique_ptr<std::ostream> os{new std::ostringstream{}};

    std::unique_ptr<std::ostream> ogzf{new compressed_ostream_t{*os}};

    std::ostreambuf_iterator<char> oit{*ogzf};

    size_t i = 0;
    for (auto _ : state)
        oit = static_cast<char>(i++ % 128);
}

#if SEQAN3_HAS_ZLIB
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::gz_ostream);
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::bgzf_ostream);
#endif // SEQAN3_HAS_ZLIB
#if SEQAN3_HAS_BZIP2
BENCHMARK_TEMPLATE(compressed_type_erased2, seqan3::contrib::bz2_ostream);
#endif // SEQAN3_HAS_BZIP2

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

#ifdef SEQAN3_HAS_SEQAN2
template <typename compression_type>
void seqan2_compressed(benchmark::State & state)
{
    std::ostringstream os;

    seqan2::VirtualStream<char, seqan2::Output> ogzf;
    compression_type tag;
    open(ogzf, os, tag);

    size_t i = 0;
    for (auto _ : state)
        write(ogzf, static_cast<char>(i++ % 128));
}
BENCHMARK_TEMPLATE(seqan2_compressed, seqan2::Nothing);

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

#endif // SEQAN3_HAS_SEQAN2

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

BENCHMARK_MAIN();