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 | |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 20 | #include <iostream> |
| 21 | #include <cmath> |
David Reiss | 28f298d | 2008-05-01 06:17:36 +0000 | [diff] [blame] | 22 | #include <transport/TBufferTransports.h> |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 23 | #include <protocol/TBinaryProtocol.h> |
| 24 | #include <protocol/TJSONProtocol.h> |
| 25 | #include "gen-cpp/DebugProtoTest_types.h" |
| 26 | #include <time.h> |
| 27 | #include "../lib/cpp/src/protocol/TDebugProtocol.h" |
David Reiss | 81c7fc0 | 2008-04-28 02:51:44 +0000 | [diff] [blame] | 28 | #include <sys/time.h> |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 29 | |
| 30 | class Timer { |
| 31 | public: |
| 32 | timeval vStart; |
| 33 | |
| 34 | Timer() { |
| 35 | gettimeofday(&vStart, 0); |
| 36 | } |
| 37 | void start() { |
| 38 | gettimeofday(&vStart, 0); |
| 39 | } |
| 40 | |
| 41 | double frame() { |
| 42 | timeval vEnd; |
| 43 | gettimeofday(&vEnd, 0); |
| 44 | double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0); |
| 45 | double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0); |
| 46 | return dend - dstart; |
| 47 | } |
| 48 | |
| 49 | }; |
| 50 | |
| 51 | int main() { |
| 52 | using namespace std; |
| 53 | using namespace thrift::test::debug; |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 54 | using namespace apache::thrift::transport; |
| 55 | using namespace apache::thrift::protocol; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 56 | using namespace boost; |
| 57 | |
| 58 | OneOfEach ooe; |
| 59 | ooe.im_true = true; |
| 60 | ooe.im_false = false; |
| 61 | ooe.a_bite = 0xd6; |
| 62 | ooe.integer16 = 27000; |
| 63 | ooe.integer32 = 1<<24; |
| 64 | ooe.integer64 = (uint64_t)6000 * 1000 * 1000; |
| 65 | ooe.double_precision = M_PI; |
| 66 | ooe.some_characters = "JSON THIS! \"\1"; |
| 67 | ooe.zomg_unicode = "\xd7\n\a\t"; |
| 68 | ooe.base64 = "\1\2\3\255"; |
| 69 | |
| 70 | shared_ptr<TMemoryBuffer> buf(new TMemoryBuffer()); |
| 71 | |
| 72 | int num = 1000000; |
| 73 | |
| 74 | { |
| 75 | Timer timer; |
| 76 | |
| 77 | for (int i = 0; i < num; i ++) { |
| 78 | buf->resetBuffer(); |
| 79 | TBinaryProtocol prot(buf); |
| 80 | ooe.write(&prot); |
| 81 | } |
| 82 | cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl; |
| 83 | } |
| 84 | |
| 85 | uint8_t* data; |
| 86 | uint32_t datasize; |
| 87 | |
| 88 | buf->getBuffer(&data, &datasize); |
| 89 | |
| 90 | { |
| 91 | |
| 92 | Timer timer; |
| 93 | |
| 94 | for (int i = 0; i < num; i ++) { |
| 95 | OneOfEach ooe2; |
| 96 | shared_ptr<TMemoryBuffer> buf2(new TMemoryBuffer(data, datasize)); |
| 97 | //buf2->resetBuffer(data, datasize); |
| 98 | TBinaryProtocol prot(buf2); |
| 99 | ooe2.read(&prot); |
| 100 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 101 | //cout << apache::thrift::ThriftDebugString(ooe2) << endl << endl; |
David Reiss | 2a4bfd6 | 2008-04-07 23:45:00 +0000 | [diff] [blame] | 102 | } |
| 103 | cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | return 0; |
| 108 | } |