Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
in-predicate-benchmark.cc
Go to the documentation of this file.
1 // Copyright 2015 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 
16 // Note: The results do not include the pre-processing in the prepare function that is
17 // necessary for SetLookup but not Iterate. None of the values searched for are in the
18 // fabricated IN list (i.e. hit rate is 0).
19 
20 // Machine Info: Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
21 // int n=1: Function Rate (iters/ms) Comparison
22 // ----------------------------------------------------------------------
23 // SetLookup n=1 902.4 1X
24 // Iterate n=1 938.3 1.04X
25 
26 // int n=2: Function Rate (iters/ms) Comparison
27 // ----------------------------------------------------------------------
28 // SetLookup n=2 888.6 1X
29 // Iterate n=2 805.6 0.9066X
30 
31 // int n=3: Function Rate (iters/ms) Comparison
32 // ----------------------------------------------------------------------
33 // SetLookup n=3 806.5 1X
34 // Iterate n=3 744.1 0.9227X
35 
36 // int n=4: Function Rate (iters/ms) Comparison
37 // ----------------------------------------------------------------------
38 // SetLookup n=4 784.1 1X
39 // Iterate n=4 661 0.843X
40 
41 // int n=5: Function Rate (iters/ms) Comparison
42 // ----------------------------------------------------------------------
43 // SetLookup n=5 801.6 1X
44 // Iterate n=5 594.4 0.7415X
45 
46 // int n=6: Function Rate (iters/ms) Comparison
47 // ----------------------------------------------------------------------
48 // SetLookup n=6 746.6 1X
49 // Iterate n=6 539 0.722X
50 
51 // int n=7: Function Rate (iters/ms) Comparison
52 // ----------------------------------------------------------------------
53 // SetLookup n=7 683.4 1X
54 // Iterate n=7 493.9 0.7226X
55 
56 // int n=8: Function Rate (iters/ms) Comparison
57 // ----------------------------------------------------------------------
58 // SetLookup n=8 772 1X
59 // Iterate n=8 455.5 0.59X
60 
61 // int n=9: Function Rate (iters/ms) Comparison
62 // ----------------------------------------------------------------------
63 // SetLookup n=9 702.9 1X
64 // Iterate n=9 420.1 0.5976X
65 
66 // int n=10: Function Rate (iters/ms) Comparison
67 // ----------------------------------------------------------------------
68 // SetLookup n=10 710.7 1X
69 // Iterate n=10 392.4 0.5521X
70 
71 // int n=400: Function Rate (iters/ms) Comparison
72 // ----------------------------------------------------------------------
73 // SetLookup n=400 422.3 1X
74 // Iterate n=400 14.01 0.03318X
75 
76 
77 // string n=1: Function Rate (iters/ms) Comparison
78 // ----------------------------------------------------------------------
79 // SetLookup n=1 250.8 1X
80 // Iterate n=1 540.4 2.154X
81 
82 // string n=2: Function Rate (iters/ms) Comparison
83 // ----------------------------------------------------------------------
84 // SetLookup n=2 205 1X
85 // Iterate n=2 297.7 1.453X
86 
87 // string n=3: Function Rate (iters/ms) Comparison
88 // ----------------------------------------------------------------------
89 // SetLookup n=3 166.8 1X
90 // Iterate n=3 240.3 1.441X
91 
92 // string n=4: Function Rate (iters/ms) Comparison
93 // ----------------------------------------------------------------------
94 // SetLookup n=4 146.2 1X
95 // Iterate n=4 177.8 1.216X
96 
97 // string n=5: Function Rate (iters/ms) Comparison
98 // ----------------------------------------------------------------------
99 // SetLookup n=5 149.9 1X
100 // Iterate n=5 144.8 0.9662X
101 
102 // string n=6: Function Rate (iters/ms) Comparison
103 // ----------------------------------------------------------------------
104 // SetLookup n=6 135.9 1X
105 // Iterate n=6 127.4 0.9372X
106 
107 // string n=7: Function Rate (iters/ms) Comparison
108 // ----------------------------------------------------------------------
109 // SetLookup n=7 143.4 1X
110 // Iterate n=7 112.8 0.7866X
111 
112 // string n=8: Function Rate (iters/ms) Comparison
113 // ----------------------------------------------------------------------
114 // SetLookup n=8 123.7 1X
115 // Iterate n=8 117.1 0.9467X
116 
117 // string n=9: Function Rate (iters/ms) Comparison
118 // ----------------------------------------------------------------------
119 // SetLookup n=9 117 1X
120 // Iterate n=9 89.19 0.762X
121 
122 // string n=10: Function Rate (iters/ms) Comparison
123 // ----------------------------------------------------------------------
124 // SetLookup n=10 125.4 1X
125 // Iterate n=10 81.63 0.6508X
126 
127 // string n=400: Function Rate (iters/ms) Comparison
128 // ----------------------------------------------------------------------
129 // SetLookup n=400 55.77 1X
130 // Iterate n=400 1.936 0.03471X
131 
132 // decimal(4,0) n=1: Function Rate (iters/ms) Comparison
133 // ----------------------------------------------------------------------
134 // SetLookup n=1 587.9 1X
135 // Iterate n=1 658.3 1.12X
136 
137 // decimal(4,0) n=2: Function Rate (iters/ms) Comparison
138 // ----------------------------------------------------------------------
139 // SetLookup n=2 521.5 1X
140 // Iterate n=2 478.5 0.9175X
141 
142 // decimal(4,0) n=3: Function Rate (iters/ms) Comparison
143 // ----------------------------------------------------------------------
144 // SetLookup n=3 524 1X
145 // Iterate n=3 373.7 0.7132X
146 
147 // decimal(4,0) n=4: Function Rate (iters/ms) Comparison
148 // ----------------------------------------------------------------------
149 // SetLookup n=4 486.6 1X
150 // Iterate n=4 308.9 0.6348X
151 
152 // decimal(4,0) n=400: Function Rate (iters/ms) Comparison
153 // ----------------------------------------------------------------------
154 // SetLookup n=400 258.2 1X
155 // Iterate n=400 4.272 0.01655X
156 
157 #include <boost/lexical_cast.hpp>
158 #include <gutil/strings/substitute.h>
159 
160 #include "exprs/in-predicate.h"
161 #include "exprs/in-predicate-ir.cc"
162 
163 #include "udf/udf-test-harness.h"
164 #include "util/benchmark.h"
165 #include "util/cpu-info.h"
166 
167 #include "common/names.h"
168 
169 using namespace impala;
170 using namespace impala_udf;
171 using namespace strings;
172 
173 namespace impala {
174 
175 template<typename T> T MakeAnyVal(int v) {
176  return T(v);
177 }
178 
179 template<> StringVal MakeAnyVal(int v) {
180  // Leak these strings so we don't have to worry about them going out of scope
181  string* s = new string();
182  *s = lexical_cast<string>(v);
183  return StringVal(reinterpret_cast<uint8_t*>(const_cast<char*>(s->c_str())), s->size());
184 }
185 
187  public:
188  template<typename T, typename SetType>
189  struct TestData {
190  vector<T> anyvals;
191  vector<AnyVal*> anyval_ptrs;
193 
194  vector<T> search_vals;
195 
200  };
201 
202  template<typename T, typename SetType>
203  static TestData<T, SetType> CreateTestData(int num_values,
204  const FunctionContext::TypeDesc& type, int num_search_vals = 100) {
205  srand(time(NULL));
207  data.anyvals.resize(num_values);
208  data.anyval_ptrs.resize(num_values);
209  for (int i = 0; i < num_values; ++i) {
210  data.anyvals[i] = MakeAnyVal<T>(rand());
211  data.anyval_ptrs[i] = &data.anyvals[i];
212  }
213 
214  for (int i = 0; i < num_search_vals; ++i) {
215  data.search_vals.push_back(MakeAnyVal<T>(rand()));
216  }
217 
218  FunctionContext* ctx = CreateContext(num_values, type);
219 
220  vector<AnyVal*> constant_args;
221  constant_args.push_back(NULL);
222  BOOST_FOREACH(AnyVal* p, data.anyval_ptrs) constant_args.push_back(p);
223  UdfTestHarness::SetConstantArgs(ctx, constant_args);
224 
225  InPredicate::SetLookupPrepare<T, SetType>(ctx, FunctionContext::FRAGMENT_LOCAL);
226  data.state = *reinterpret_cast<InPredicate::SetLookupState<SetType>*>(
227  ctx->GetFunctionState(FunctionContext::FRAGMENT_LOCAL));
228 
229  data.total_found_set = data.total_set = data.total_found_iter = data.total_iter = 0;
230  return data;
231  }
232 
233  template<typename T, typename SetType>
234  static void TestSetLookup(int batch_size, void* d) {
235  TestData<T, SetType>* data = reinterpret_cast<TestData<T, SetType>*>(d);
236  for (int i = 0; i < batch_size; ++i) {
237  BOOST_FOREACH(const T& search_val, data->search_vals) {
238  BooleanVal found = InPredicate::SetLookup(&data->state, search_val);
239  if (found.val) ++data->total_found_set;
240  ++data->total_set;
241  }
242  }
243  }
244 
245  template<typename T, typename SetType>
246  static void TestIterate(int batch_size, void* d) {
247  TestData<T, SetType>* data = reinterpret_cast<TestData<T, SetType>*>(d);
248  for (int i = 0; i < batch_size; ++i) {
249  BOOST_FOREACH(const T& search_val, data->search_vals) {
251  data->state.type, search_val, data->anyvals.size(), &data->anyvals[0]);
252  if (found.val) ++data->total_found_iter;
253  ++data->total_iter;
254  }
255  }
256  }
257 
258  static void RunIntBenchmark(int n) {
259  Benchmark suite(Substitute("int n=$0", n));
263  InPredicateBenchmark::CreateTestData<IntVal, int32_t>(n, type);
264  suite.AddBenchmark(Substitute("SetLookup n=$0", n),
265  InPredicateBenchmark::TestSetLookup<IntVal, int32_t>, &data);
266  suite.AddBenchmark(Substitute("Iterate n=$0", n),
267  InPredicateBenchmark::TestIterate<IntVal, int32_t>, &data);
268  cout << suite.Measure() << endl;
269  // cout << "Found set: " << (double)data.total_found_set / data.total_set << endl;
270  // cout << "Found iter: " << (double)data.total_found_iter / data.total_iter << endl;
271  }
272 
273  static void RunStringBenchmark(int n) {
274  Benchmark suite(Substitute("string n=$0", n));
278  InPredicateBenchmark::CreateTestData<StringVal, StringValue>(n, type);
279  suite.AddBenchmark(Substitute("SetLookup n=$0", n),
280  InPredicateBenchmark::TestSetLookup<StringVal, StringValue>, &data);
281  suite.AddBenchmark(Substitute("Iterate n=$0", n),
282  InPredicateBenchmark::TestIterate<StringVal, StringValue>, &data);
283  cout << suite.Measure() << endl;
284  // cout << "Found set: " << (double)data.total_found_set / data.total_set << endl;
285  // cout << "Found iter: " << (double)data.total_found_iter / data.total_iter << endl;
286  }
287 
288  static void RunDecimalBenchmark(int n) {
289  Benchmark suite(Substitute("decimal(4,0) n=$0", n));
292  type.precision = 4;
293  type.scale = 0;
295  InPredicateBenchmark::CreateTestData<DecimalVal, Decimal16Value>(n, type);
296  suite.AddBenchmark(Substitute("SetLookup n=$0", n),
297  InPredicateBenchmark::TestSetLookup<DecimalVal, Decimal16Value>, &data);
298  suite.AddBenchmark(Substitute("Iterate n=$0", n),
299  InPredicateBenchmark::TestIterate<DecimalVal, Decimal16Value>, &data);
300  cout << suite.Measure() << endl;
301  }
302 
303  private:
305  int num_args, const FunctionContext::TypeDesc& type) {
306  // Types don't matter (but number of args do)
307  FunctionContext::TypeDesc ret_type;
308  vector<FunctionContext::TypeDesc> arg_types(num_args + 1, type);
309  return UdfTestHarness::CreateTestContext(ret_type, arg_types);
310  }
311 
312 };
313 
314 }
315 
316 int main(int argc, char **argv) {
317  CpuInfo::Init();
318  cout << Benchmark::GetMachineInfo() << endl;
319 
320  for (int i = 1; i <= 10; ++i) InPredicateBenchmark::RunIntBenchmark(i);
322 
323  cout << endl;
324 
325  for (int i = 1; i <= 10; ++i) InPredicateBenchmark::RunStringBenchmark(i);
327 
328  for (int i = 1; i <= 4; ++i) InPredicateBenchmark::RunDecimalBenchmark(i);
330 
331  return 0;
332 }
int precision
Only valid if type == TYPE_DECIMAL.
Definition: udf.h:75
int AddBenchmark(const std::string &name, BenchmarkFunction fn, void *args, int baseline_idx=0)
Definition: benchmark.cc:70
static void TestIterate(int batch_size, void *d)
static std::string GetMachineInfo()
Output machine/build configuration as a string.
Definition: benchmark.cc:124
static BooleanVal Iterate(const FunctionContext::TypeDesc *type, const T &val, int num_args, const T *args)
Iterates through each vararg looking for val. 'type' is the type of 'val' and 'args'.
std::string Measure()
Runs all the benchmarks and returns the result in a formatted string.
Definition: benchmark.cc:83
static FunctionContext * CreateContext(int num_args, const FunctionContext::TypeDesc &type)
int main(int argc, char **argv)
static BooleanVal SetLookup(SetLookupState< SetType > *state, const T &v)
Looks up v in state->val_set.
static void TestSetLookup(int batch_size, void *d)
static TestData< T, SetType > CreateTestData(int num_values, const FunctionContext::TypeDesc &type, int num_search_vals=100)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75
InPredicate::SetLookupState< SetType > state