Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 3 | #include <sys/time.h> |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 4 | #include "protocol/TBinaryProtocol.h" |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 5 | #include "transport/TBufferedTransport.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 6 | #include "transport/TSocket.h" |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 7 | #include "ThriftTest.h" |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 8 | using namespace std; |
| 9 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 10 | extern uint32_t g_socket_syscalls; |
| 11 | |
| 12 | // Current time, microseconds since the epoch |
| 13 | uint64_t now() |
| 14 | { |
| 15 | long long ret; |
| 16 | struct timeval tv; |
| 17 | |
| 18 | gettimeofday(&tv, NULL); |
| 19 | ret = tv.tv_sec; |
| 20 | ret = ret*1000*1000 + tv.tv_usec; |
| 21 | return ret; |
| 22 | } |
| 23 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 24 | int main(int argc, char** argv) { |
| 25 | string host = "localhost"; |
| 26 | int port = 9090; |
| 27 | int numTests = 1; |
| 28 | |
| 29 | if (argc > 1) { |
| 30 | host = argv[1]; |
| 31 | } |
| 32 | if (argc > 2) { |
| 33 | port = atoi(argv[2]); |
| 34 | } |
| 35 | if (argc > 3) { |
| 36 | numTests = atoi(argv[3]); |
| 37 | } |
| 38 | |
| 39 | TSocket socket(host, port); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 40 | TBufferedTransport bufferedSocket(&socket, 2048); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 41 | TBinaryProtocol binaryProtocol; |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 42 | ThriftTestClient testClient(&bufferedSocket, &binaryProtocol); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 43 | |
| 44 | int test = 0; |
| 45 | for (test = 0; test < numTests; ++test) { |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * CONNECT TEST |
| 49 | */ |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 50 | printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port); |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 51 | try { |
| 52 | bufferedSocket.open(); |
| 53 | } catch (TTransportException& ttx) { |
| 54 | printf("Connect failed: %s\n", ttx.getMessage().c_str()); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 55 | continue; |
| 56 | } |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 57 | |
| 58 | uint64_t start = now(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * VOID TEST |
| 62 | */ |
| 63 | printf("testVoid()"); |
| 64 | testClient.testVoid(); |
| 65 | printf(" = void\n"); |
| 66 | |
| 67 | /** |
| 68 | * STRING TEST |
| 69 | */ |
| 70 | printf("testString(\"Test\")"); |
| 71 | string s = testClient.testString("Test"); |
| 72 | printf(" = \"%s\"\n", s.c_str()); |
| 73 | |
| 74 | /** |
| 75 | * BYTE TEST |
| 76 | */ |
| 77 | printf("testByte(1)"); |
| 78 | uint8_t u8 = testClient.testByte(1); |
| 79 | printf(" = %d\n", (int)u8); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 80 | |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 81 | /** |
| 82 | * I32 TEST |
| 83 | */ |
| 84 | printf("testI32(-1)"); |
| 85 | int32_t i32 = testClient.testI32(-1); |
| 86 | printf(" = %d\n", i32); |
| 87 | |
| 88 | /** |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 89 | * I64 TEST |
| 90 | */ |
| 91 | printf("testI64(-34359738368)"); |
| 92 | int64_t i64 = testClient.testI64(-34359738368); |
| 93 | printf(" = %ld\n", i64); |
| 94 | |
| 95 | /** |
| 96 | * STRUCT TEST |
| 97 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 98 | printf("testStruct({\"Zero\", 1, -3, -5})"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 99 | Xtruct out; |
| 100 | out.string_thing = "Zero"; |
| 101 | out.byte_thing = 1; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 102 | out.i32_thing = -3; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 103 | out.i64_thing = -5; |
| 104 | Xtruct in = testClient.testStruct(out); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 105 | printf(" = {\"%s\", %d, %d, %ld}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 106 | in.string_thing.c_str(), |
| 107 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 108 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 109 | in.i64_thing); |
| 110 | |
| 111 | /** |
| 112 | * NESTED STRUCT TEST |
| 113 | */ |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 114 | printf("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 115 | Xtruct2 out2; |
| 116 | out2.byte_thing = 1; |
| 117 | out2.struct_thing = out; |
| 118 | out2.i32_thing = 5; |
| 119 | Xtruct2 in2 = testClient.testNest(out2); |
| 120 | in = in2.struct_thing; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 121 | printf(" = {%d, {\"%s\", %d, %d, %ld}, %d}\n", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 122 | in2.byte_thing, |
| 123 | in.string_thing.c_str(), |
| 124 | (int)in.byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 125 | in.i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 126 | in.i64_thing, |
| 127 | in2.i32_thing); |
| 128 | |
| 129 | /** |
| 130 | * MAP TEST |
| 131 | */ |
| 132 | map<int32_t,int32_t> mapout; |
| 133 | for (int32_t i = 0; i < 5; ++i) { |
| 134 | mapout.insert(make_pair(i, i-10)); |
| 135 | } |
| 136 | printf("testMap({"); |
| 137 | map<int32_t, int32_t>::const_iterator m_iter; |
| 138 | bool first = true; |
| 139 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 140 | if (first) { |
| 141 | first = false; |
| 142 | } else { |
| 143 | printf(", "); |
| 144 | } |
| 145 | printf("%d => %d", m_iter->first, m_iter->second); |
| 146 | } |
| 147 | printf("})"); |
| 148 | map<int32_t,int32_t> mapin = testClient.testMap(mapout); |
| 149 | printf(" = {"); |
| 150 | first = true; |
| 151 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 152 | if (first) { |
| 153 | first = false; |
| 154 | } else { |
| 155 | printf(", "); |
| 156 | } |
| 157 | printf("%d => %d", m_iter->first, m_iter->second); |
| 158 | } |
| 159 | printf("}\n"); |
| 160 | |
| 161 | /** |
| 162 | * SET TEST |
| 163 | */ |
| 164 | set<int32_t> setout; |
| 165 | for (int32_t i = -2; i < 3; ++i) { |
| 166 | setout.insert(i); |
| 167 | } |
| 168 | printf("testSet({"); |
| 169 | set<int32_t>::const_iterator s_iter; |
| 170 | first = true; |
| 171 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 172 | if (first) { |
| 173 | first = false; |
| 174 | } else { |
| 175 | printf(", "); |
| 176 | } |
| 177 | printf("%d", *s_iter); |
| 178 | } |
| 179 | printf("})"); |
| 180 | set<int32_t> setin = testClient.testSet(setout); |
| 181 | printf(" = {"); |
| 182 | first = true; |
| 183 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 184 | if (first) { |
| 185 | first = false; |
| 186 | } else { |
| 187 | printf(", "); |
| 188 | } |
| 189 | printf("%d", *s_iter); |
| 190 | } |
| 191 | printf("}\n"); |
| 192 | |
| 193 | /** |
| 194 | * LIST TEST |
| 195 | */ |
| 196 | list<int32_t> listout; |
| 197 | for (int32_t i = -2; i < 3; ++i) { |
| 198 | listout.push_back(i); |
| 199 | } |
| 200 | printf("testList({"); |
| 201 | list<int32_t>::const_iterator l_iter; |
| 202 | first = true; |
| 203 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 204 | if (first) { |
| 205 | first = false; |
| 206 | } else { |
| 207 | printf(", "); |
| 208 | } |
| 209 | printf("%d", *l_iter); |
| 210 | } |
| 211 | printf("})"); |
| 212 | list<int32_t> listin = testClient.testList(listout); |
| 213 | printf(" = {"); |
| 214 | first = true; |
| 215 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 216 | if (first) { |
| 217 | first = false; |
| 218 | } else { |
| 219 | printf(", "); |
| 220 | } |
| 221 | printf("%d", *l_iter); |
| 222 | } |
| 223 | printf("}\n"); |
| 224 | |
| 225 | /** |
| 226 | * ENUM TEST |
| 227 | */ |
| 228 | printf("testEnum(ONE)"); |
| 229 | Numberz ret = testClient.testEnum(ONE); |
| 230 | printf(" = %d\n", ret); |
| 231 | |
| 232 | printf("testEnum(TWO)"); |
| 233 | ret = testClient.testEnum(TWO); |
| 234 | printf(" = %d\n", ret); |
| 235 | |
| 236 | printf("testEnum(THREE)"); |
| 237 | ret = testClient.testEnum(THREE); |
| 238 | printf(" = %d\n", ret); |
| 239 | |
| 240 | printf("testEnum(FIVE)"); |
| 241 | ret = testClient.testEnum(FIVE); |
| 242 | printf(" = %d\n", ret); |
| 243 | |
| 244 | printf("testEnum(EIGHT)"); |
| 245 | ret = testClient.testEnum(EIGHT); |
| 246 | printf(" = %d\n", ret); |
| 247 | |
| 248 | /** |
| 249 | * TYPEDEF TEST |
| 250 | */ |
| 251 | printf("testTypedef(309858235082523)"); |
| 252 | UserId uid = testClient.testTypedef(309858235082523); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 253 | printf(" = %ld\n", uid); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * NESTED MAP TEST |
| 257 | */ |
| 258 | printf("testMapMap(1)"); |
| 259 | map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1); |
| 260 | printf(" = {"); |
| 261 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 262 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 263 | printf("%d => {", mi->first); |
| 264 | map<int32_t, int32_t>::const_iterator mi2; |
| 265 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 266 | printf("%d => %d, ", mi2->first, mi2->second); |
| 267 | } |
| 268 | printf("}, "); |
| 269 | } |
| 270 | printf("}\n"); |
| 271 | |
| 272 | /** |
| 273 | * INSANITY TEST |
| 274 | */ |
| 275 | Insanity insane; |
| 276 | insane.userMap.insert(make_pair(FIVE, 5000)); |
| 277 | Xtruct truck; |
| 278 | truck.string_thing = "Truck"; |
| 279 | truck.byte_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 280 | truck.i32_thing = 8; |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 281 | truck.i64_thing = 8; |
| 282 | insane.xtructs.push_back(truck); |
| 283 | printf("testInsanity()"); |
| 284 | map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane); |
| 285 | printf(" = {"); |
| 286 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 287 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 288 | printf("%ld => {", i_iter->first); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 289 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 290 | for (i2_iter = i_iter->second.begin(); |
| 291 | i2_iter != i_iter->second.end(); |
| 292 | ++i2_iter) { |
| 293 | printf("%d => {", i2_iter->first); |
| 294 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 295 | map<Numberz, UserId>::const_iterator um; |
| 296 | printf("{"); |
| 297 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 298 | printf("%d => %ld, ", um->first, um->second); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 299 | } |
| 300 | printf("}, "); |
| 301 | |
| 302 | list<Xtruct> xtructs = i2_iter->second.xtructs; |
| 303 | list<Xtruct>::const_iterator x; |
| 304 | printf("{"); |
| 305 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame^] | 306 | printf("{\"%s\", %d, %d, %ld}, ", |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 307 | x->string_thing.c_str(), |
| 308 | (int)x->byte_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 309 | x->i32_thing, |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 310 | x->i64_thing); |
| 311 | } |
| 312 | printf("}"); |
| 313 | |
| 314 | printf("}, "); |
| 315 | } |
| 316 | printf("}, "); |
| 317 | } |
| 318 | printf("}\n"); |
| 319 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 320 | uint64_t stop = now(); |
| 321 | printf("Total time: %lu us\n", stop-start); |
| 322 | |
| 323 | bufferedSocket.close(); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mark Slee | 9577100 | 2006-06-07 06:53:25 +0000 | [diff] [blame] | 326 | printf("\nSocket syscalls: %u", g_socket_syscalls); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 327 | printf("\nAll tests done.\n"); |
| 328 | return 0; |
| 329 | } |