forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_benchmark.cc
More file actions
164 lines (140 loc) · 5.76 KB
/
Copy pathwriter_benchmark.cc
File metadata and controls
164 lines (140 loc) · 5.76 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/type.h"
#include "benchmark/benchmark.h"
#include <memory>
#include <string>
#include "arrow/array.h"
#include "arrow/array/builder_binary.h"
#include "arrow/csv/options.h"
#include "arrow/csv/writer.h"
#include "arrow/io/memory.h"
#include "arrow/record_batch.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/testing/random.h"
namespace arrow {
namespace csv {
namespace {
constexpr int kSeed = 12345;
// length range of csv test strings
constexpr int kStrLenMin = 5;
constexpr int kStrLenMax = 50;
// rows/cols of csv test dataset
constexpr int kCsvRows = 2000;
constexpr int kCsvCols = 10;
std::shared_ptr<RecordBatch> MakeIntTestBatch(int rows, int cols, int64_t null_percent) {
random::RandomArrayGenerator rg(kSeed);
FieldVector fields(cols);
ArrayVector arrays(cols);
for (int i = 0; i < cols; ++i) {
fields[i] = field('i' + std::to_string(i), int32());
arrays[i] = rg.Int32(rows, -87654321, 123456789, null_percent / 100.);
}
return RecordBatch::Make(schema(fields), rows, arrays);
}
std::shared_ptr<RecordBatch> MakeStrTestBatch(int rows, int cols, bool quote,
int64_t null_percent) {
random::RandomArrayGenerator rg(kSeed + 1);
// append quote to array elements (return a new array)
auto append_quote = [](const Array& array) -> std::shared_ptr<Array> {
typename TypeTraits<StringType>::BuilderType builder;
const auto& string_array = dynamic_cast<const StringArray&>(array);
for (int64_t i = 0; i < string_array.length(); ++i) {
if (string_array.IsValid(i)) {
ABORT_NOT_OK(builder.Append(string_array.GetString(i) + '"'));
} else {
ABORT_NOT_OK(builder.AppendNull());
}
}
std::shared_ptr<Array> result;
ABORT_NOT_OK(builder.Finish(&result));
return result;
};
// string length varies from kStrLenMin to kStrLenMax for different columns
auto lengths =
std::dynamic_pointer_cast<Int32Array>(rg.Int32(cols, kStrLenMin, kStrLenMax));
FieldVector fields(cols);
ArrayVector arrays(cols);
for (int i = 0; i < cols; ++i) {
fields[i] = field('s' + std::to_string(i), utf8());
// string length varies by 20% for different rows in same column
arrays[i] = rg.String(rows, lengths->Value(i), lengths->Value(i) * 6 / 5,
null_percent / 100.);
if (quote) {
arrays[i] = append_quote(*arrays[i]);
}
}
return RecordBatch::Make(schema(fields), rows, arrays);
}
void BenchmarkWriteCsv(benchmark::State& state, const WriteOptions& options,
const RecordBatch& batch) {
int64_t total_size = 0;
while (state.KeepRunning()) {
auto out = io::BufferOutputStream::Create().ValueOrDie();
ABORT_NOT_OK(WriteCSV(batch, options, out.get()));
auto buffer = out->Finish().ValueOrDie();
total_size += buffer->size();
}
// byte size of the generated csv dataset
state.SetBytesProcessed(total_size);
state.counters["null_percent"] = static_cast<double>(state.range(0));
}
// Exercies UnQuotedColumnPopulator with integer
void WriteCsvNumeric(benchmark::State& state) {
auto batch = MakeIntTestBatch(kCsvRows, kCsvCols, state.range(0));
BenchmarkWriteCsv(state, WriteOptions::Defaults(), *batch);
}
// Exercise QuotedColumnPopulator with string (without quote)
void WriteCsvStringNoQuote(benchmark::State& state) {
auto batch = MakeStrTestBatch(kCsvRows, kCsvCols, /*quote=*/false, state.range(0));
BenchmarkWriteCsv(state, WriteOptions::Defaults(), *batch);
}
// Exercise QuotedColumnPopulator with string (with quote)
void WriteCsvStringWithQuote(benchmark::State& state) {
auto batch = MakeStrTestBatch(kCsvRows, kCsvCols, /*quote=*/true, state.range(0));
BenchmarkWriteCsv(state, WriteOptions::Defaults(), *batch);
}
// Exercise UnQuotedColumnPopulator with string
// - caller guarantees there will be no quote in the inputs
void WriteCsvStringRejectQuote(benchmark::State& state) {
auto batch = MakeStrTestBatch(kCsvRows, kCsvCols, /*quote=*/false, state.range(0));
auto options = WriteOptions::Defaults();
options.quoting_style = QuotingStyle::None;
BenchmarkWriteCsv(state, options, *batch);
}
// Exercise QuotedColumnPopulator with integer
// - check quote even for numeric type (is it useful?)
void WriteCsvNumericCheckQuote(benchmark::State& state) {
auto batch = MakeIntTestBatch(kCsvRows, kCsvCols, state.range(0));
auto options = WriteOptions::Defaults();
options.quoting_style = QuotingStyle::AllValid;
BenchmarkWriteCsv(state, options, *batch);
}
void NullPercents(benchmark::internal::Benchmark* bench) {
std::vector<int> null_percents = {0, 1, 10, 50};
for (int null_percent : null_percents) {
bench->Args({null_percent});
}
}
} // namespace
BENCHMARK(WriteCsvNumeric)->Apply(NullPercents);
BENCHMARK(WriteCsvStringNoQuote)->Apply(NullPercents);
BENCHMARK(WriteCsvStringWithQuote)->Apply(NullPercents);
BENCHMARK(WriteCsvStringRejectQuote)->Apply(NullPercents);
BENCHMARK(WriteCsvNumericCheckQuote)->Apply(NullPercents);
} // namespace csv
} // namespace arrow