Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
threading-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 <thrift/protocol/TBinaryProtocol.h>
28 #include <sstream>
29 #include <thrift/TApplicationException.h>
30 #include <thrift/transport/TBufferTransports.h>
31 
32 using namespace impala;
33 using namespace std;
34 using namespace apache::thrift::protocol;
35 using namespace apache::thrift::transport;
36 
37 struct TestData {
38  uint8_t* msg;
39  uint32_t length;
40  boost::shared_ptr<TTransport> tmem_transport;
41  TBinaryProtocolFactoryT<TMemoryBuffer> tproto_factory;
42  boost::shared_ptr<apache::thrift::protocol::TProtocol> tproto;
43 };
44 
45 
46 //boost::shared_ptr<apache::thrift::protocol::TProtocol>
47 
48 void TestDeserializeSharedState(int size, void* d) {
49  THostPort deserialized_msg;
50  TestData* data = reinterpret_cast<TestData*>(d);
51  try {
52  deserialized_msg.read(data->tproto.get());
53  } catch (apache::thrift::protocol::TProtocolException& e) {
54  std::stringstream msg;
55  msg << "couldn't deserialize thrift msg:\n" << e.what();
56  LOG(INFO) << msg;
57  } catch (...) {
58  // TODO: Find the right exception for 0 bytes
59  // LOG(INFO) << "Crap!";
60  }
61 }
62 
63 void TestDeserialize(int size, void* d) {
64  THostPort hostport;
65  TestData* data = reinterpret_cast<TestData*>(d);
66  if (!DeserializeThriftMsg(data->msg, data->length, &hostport).ok()) { LOG(INFO) << "FAILED"; }
67 }
68 
69 int main(int argc, char **argv) {
70  CpuInfo::Init();
71 
72  TestData data;
73  string s;
74  THostPort hostport;
75  hostport.ipaddress = "0.2.3.4";
76  hostport.hostname = "localhost";
77  hostport.port = 12345;
78  SerializeThriftMsgToString(&hostport, &s);
79  data.msg = (uint8_t*)const_cast<char *>(s.c_str());
80  data.length = s.size();
81 
82  data.tmem_transport.reset(new apache::thrift::transport::TMemoryBuffer(
83  const_cast<uint8_t*>(data.msg), data.length));
84 
85  data.tproto = data.tproto_factory.getProtocol(data.tmem_transport);
86 
87  Benchmark suite("deserialize");
88  suite.AddBenchmark("deserialize", TestDeserialize, &data);
89  suite.AddBenchmark("deserialize-shared-state", TestDeserializeSharedState, &data);
90  cout << suite.Measure();
91 
92  return 0;
93 }
int AddBenchmark(const std::string &name, BenchmarkFunction fn, void *args, int baseline_idx=0)
Definition: benchmark.cc:70
const StringSearch UrlParser::protocol_search & protocol
Definition: url-parser.cc:36
std::string Measure()
Runs all the benchmarks and returns the result in a formatted string.
Definition: benchmark.cc:83
int main(int argc, char **argv)
TBinaryProtocolFactoryT< TMemoryBuffer > tproto_factory
void TestDeserialize(int size, void *d)
boost::shared_ptr< TTransport > tmem_transport
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75
Status DeserializeThriftMsg(JNIEnv *env, jbyteArray serialized_msg, T *deserialized_msg)
boost::shared_ptr< apache::thrift::protocol::TProtocol > tproto
bool ok() const
Definition: status.h:172
void TestDeserializeSharedState(int size, void *d)