David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 23 | #include <iostream> |
| 24 | #include <cmath> |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 25 | #include "transport/TBufferTransports.h" |
| 26 | #include "protocol/TBinaryProtocol.h" |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 27 | #include "gen-cpp/DebugProtoTest_types.h" |
| 28 | #include <time.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 29 | #ifdef HAVE_SYS_TIME_H |
David Reiss | 81c7fc0 | 2008-04-28 02:51:44 +0000 | [diff] [blame] | 30 | #include <sys/time.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 31 | #endif |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 32 | |
| 33 | class Timer { |
| 34 | public: |
| 35 | timeval vStart; |
| 36 | |
| 37 | Timer() { |
| 38 | gettimeofday(&vStart, 0); |
| 39 | } |
| 40 | void start() { |
| 41 | gettimeofday(&vStart, 0); |
| 42 | } |
| 43 | |
| 44 | double frame() { |
| 45 | timeval vEnd; |
| 46 | gettimeofday(&vEnd, 0); |
| 47 | double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0); |
| 48 | double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0); |
| 49 | return dend - dstart; |
| 50 | } |
| 51 | |
| 52 | }; |
| 53 | |
| 54 | int main() { |
| 55 | using namespace std; |
| 56 | using namespace thrift::test::debug; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 57 | using namespace apache::thrift::transport; |
| 58 | using namespace apache::thrift::protocol; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 59 | using namespace boost; |
| 60 | |
| 61 | OneOfEach ooe; |
| 62 | ooe.im_true = true; |
| 63 | ooe.im_false = false; |
Christian Lavoie | cbf87cb | 2010-11-28 14:34:26 +0000 | [diff] [blame] | 64 | ooe.a_bite = 0x7f; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 65 | ooe.integer16 = 27000; |
| 66 | ooe.integer32 = 1<<24; |
| 67 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 68 | ooe.double_precision = M_PI; |
| 69 | ooe.some_characters = "JSON THIS! \"\1"; |
| 70 | ooe.zomg_unicode = "\xd7\n\a\t"; |
| 71 | ooe.base64 = "\1\2\3\255"; |
| 72 | |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 73 | boost::shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 74 | |
| 75 | int num = 1000000; |
| 76 | |
| 77 | { |
| 78 | Timer timer; |
| 79 | |
| 80 | for (int i = 0; i < num; i ++) { |
| 81 | buf->resetBuffer(); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 82 | TBinaryProtocolT<TBufferBase> prot(buf); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 83 | ooe.write(&prot); |
| 84 | } |
| 85 | cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl; |
| 86 | } |
| 87 | |
| 88 | uint8_t* data; |
| 89 | uint32_t datasize; |
| 90 | |
| 91 | buf->getBuffer(&data, &datasize); |
| 92 | |
| 93 | { |
| 94 | |
| 95 | Timer timer; |
| 96 | |
| 97 | for (int i = 0; i < num; i ++) { |
| 98 | OneOfEach ooe2; |
Roger Meier | 611f90c | 2011-12-11 22:08:51 +0000 | [diff] [blame] | 99 | boost::shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 100 | //buf2->resetBuffer(data, datasize); |
David Reiss | e71115b | 2010-10-06 17:09:56 +0000 | [diff] [blame] | 101 | TBinaryProtocolT<TBufferBase> prot(buf2); |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 102 | ooe2.read(&prot); |
| 103 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 104 | //cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 105 | } |
| 106 | cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | return 0; |
| 111 | } |