Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uuid-benchmark.cc
Go to the documentation of this file.
1 // Copyright 2012 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 #include <stdlib.h>
16 #include <stdio.h>
17 #include <iostream>
18 #include <vector>
19 #include <sstream>
20 #include "runtime/string-value.h"
21 #include "util/benchmark.h"
22 #include "util/cpu-info.h"
23 #include "util/thrift-util.h"
24 #include "gen-cpp/Types_types.h"
25 
26 #include <boost/shared_ptr.hpp>
27 #include <boost/thread/mutex.hpp>
28 #include <boost/thread.hpp>
29 #include <thrift/protocol/TBinaryProtocol.h>
30 #include <sstream>
31 #include <thrift/TApplicationException.h>
32 #include <thrift/transport/TBufferTransports.h>
33 #include <boost/uuid/uuid.hpp>
34 #include <boost/uuid/uuid_generators.hpp>
35 
36 using namespace impala;
37 using namespace std;
38 using namespace boost;
39 using namespace boost::uuids;
40 
42 static boost::uuids::random_generator uuid_generator_;
43 
44 static int NUM_REPS = 100;
45 
46 void TestSharedGenerator(int n) {
47  for (int i = 0; i < n * 10; ++i) {
48  lock_guard<mutex> l(global_mutex);
49  uuid query_uuid = uuid_generator_();
50  }
51 }
52 
53 void TestOwnGenerator(int n) {
54  for (int i = 0; i < n * 10; ++i) {
55  boost::uuids::random_generator uuid_generator;
56  uuid query_uuid = uuid_generator();
57  }
58 }
59 
60 void TestOwnThreaded(int size, void* d) {
61 cout << "Size: " << size;
62  vector<boost::thread*> threads;
63  for (int i = 0; i < 2; ++i) {
64 threads.push_back(new thread(TestOwnGenerator, size));
65  }
66 
67 for (int i = 0; i< threads.size(); ++i) {
68  threads[i]->join();
69  delete threads[i];
70  }
71 }
72 
73 void TestSharedThreaded(int size, void* d) {
74 vector<boost::thread*> threads;
75 for (int i = 0; i < 2; ++i) {
76 threads.push_back(new thread(TestOwnGenerator, size));
77 }
78 
79 for (int i = 0; i< threads.size(); ++i) {
80 threads[i]->join();
81 delete threads[i];
82 }
83 }
84 
85 
86 int main(int argc, char **argv) {
87  CpuInfo::Init();
88 
89  Benchmark suite("UUID generator");
90  suite.AddBenchmark("Own generator", TestOwnThreaded, NULL);
91  suite.AddBenchmark("Shared generator", TestSharedThreaded, NULL);
92  cout << suite.Measure();
93 
94  return 0;
95 }
int AddBenchmark(const std::string &name, BenchmarkFunction fn, void *args, int baseline_idx=0)
Definition: benchmark.cc:70
static int NUM_REPS
void TestSharedThreaded(int size, void *d)
void TestOwnThreaded(int size, void *d)
std::string Measure()
Runs all the benchmarks and returns the result in a formatted string.
Definition: benchmark.cc:83
static boost::uuids::random_generator uuid_generator_
mutex global_mutex
void TestOwnGenerator(int n)
int main(int argc, char **argv)
void TestSharedGenerator(int n)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75