forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter_benchmark.cc
More file actions
153 lines (125 loc) · 5.42 KB
/
Copy pathconverter_benchmark.cc
File metadata and controls
153 lines (125 loc) · 5.42 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
// 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 "benchmark/benchmark.h"
#include <sstream>
#include <string>
#include "arrow/array.h"
#include "arrow/buffer.h"
#include "arrow/csv/converter.h"
#include "arrow/csv/options.h"
#include "arrow/csv/parser.h"
#include "arrow/csv/reader.h"
#include "arrow/csv/test_common.h"
#include "arrow/io/memory.h"
#include "arrow/testing/gtest_util.h"
#include "arrow/util/value_parsing.h"
namespace arrow {
namespace csv {
static std::shared_ptr<BlockParser> BuildFromExamples(
const std::vector<std::string>& base_rows, int32_t num_rows) {
std::vector<std::string> rows;
for (int32_t i = 0; i < num_rows; ++i) {
rows.push_back(base_rows[i % base_rows.size()]);
}
std::shared_ptr<BlockParser> result;
MakeCSVParser(rows, &result);
return result;
}
static std::shared_ptr<BlockParser> BuildInt64Data(int32_t num_rows) {
const std::vector<std::string> base_rows = {"123\n", "4\n", "-317005557\n",
"\n", "N/A\n", "0\n"};
return BuildFromExamples(base_rows, num_rows);
}
static std::shared_ptr<BlockParser> BuildFloatData(int32_t num_rows) {
const std::vector<std::string> base_rows = {"0\n", "123.456\n", "-3170.55766\n", "\n",
"N/A\n"};
return BuildFromExamples(base_rows, num_rows);
}
static std::shared_ptr<BlockParser> BuildDecimal128Data(int32_t num_rows) {
const std::vector<std::string> base_rows = {"0\n", "123.456\n", "-3170.55766\n",
"\n", "N/A\n", "1233456789.123456789"};
return BuildFromExamples(base_rows, num_rows);
}
static std::shared_ptr<BlockParser> BuildStringData(int32_t num_rows) {
return BuildDecimal128Data(num_rows);
}
static std::shared_ptr<BlockParser> BuildISO8601Data(int32_t num_rows) {
const std::vector<std::string> base_rows = {
"1917-10-17\n", "2018-09-13\n", "1941-06-22 04:00\n", "1945-05-09 09:45:38\n"};
return BuildFromExamples(base_rows, num_rows);
}
static std::shared_ptr<BlockParser> BuildStrptimeData(int32_t num_rows) {
const std::vector<std::string> base_rows = {"10/17/1917\n", "9/13/2018\n",
"9/5/1945\n"};
return BuildFromExamples(base_rows, num_rows);
}
static void BenchmarkConversion(benchmark::State& state, // NOLINT non-const reference
BlockParser& parser,
const std::shared_ptr<DataType>& type,
ConvertOptions options) {
std::shared_ptr<Converter> converter = *Converter::Make(type, options);
while (state.KeepRunning()) {
auto converted = *converter->Convert(parser, 0 /* col_index */);
if (converted->length() != parser.num_rows()) {
std::cerr << "Conversion incomplete\n";
std::abort();
}
}
state.SetItemsProcessed(state.iterations() * parser.num_rows());
}
constexpr size_t num_rows = 10000;
static void Int64Conversion(benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildInt64Data(num_rows);
auto options = ConvertOptions::Defaults();
BenchmarkConversion(state, *parser, int64(), options);
}
static void FloatConversion(benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildFloatData(num_rows);
auto options = ConvertOptions::Defaults();
BenchmarkConversion(state, *parser, float64(), options);
}
static void Decimal128Conversion(benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildDecimal128Data(num_rows);
auto options = ConvertOptions::Defaults();
BenchmarkConversion(state, *parser, decimal(24, 9), options);
}
static void StringConversion(benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildStringData(num_rows);
auto options = ConvertOptions::Defaults();
BenchmarkConversion(state, *parser, utf8(), options);
}
static void TimestampConversionDefault(
benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildISO8601Data(num_rows);
auto options = ConvertOptions::Defaults();
BenchmarkConversion(state, *parser, timestamp(TimeUnit::MILLI), options);
}
static void TimestampConversionStrptime(
benchmark::State& state) { // NOLINT non-const reference
auto parser = BuildStrptimeData(num_rows);
auto options = ConvertOptions::Defaults();
options.timestamp_parsers.push_back(TimestampParser::MakeStrptime("%m/%d/%Y"));
BenchmarkConversion(state, *parser, timestamp(TimeUnit::MILLI), options);
}
BENCHMARK(Int64Conversion);
BENCHMARK(FloatConversion);
BENCHMARK(Decimal128Conversion);
BENCHMARK(StringConversion);
BENCHMARK(TimestampConversionDefault);
BENCHMARK(TimestampConversionStrptime);
} // namespace csv
} // namespace arrow