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 | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 20 | using System; |
| 21 | using System.Collections.Generic; |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 22 | using System.Threading; |
| 23 | using Thrift.Collections; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 24 | using Thrift.Protocol; |
| 25 | using Thrift.Transport; |
| 26 | using Thrift.Test; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 27 | |
| 28 | namespace Test |
| 29 | { |
| 30 | public class TestClient |
| 31 | { |
| 32 | private static int numIterations = 1; |
| 33 | |
| 34 | public static void Execute(string[] args) |
| 35 | { |
| 36 | try |
| 37 | { |
| 38 | string host = "localhost"; |
| 39 | int port = 9090; |
| 40 | string url = null; |
| 41 | int numThreads = 1; |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame^] | 42 | bool buffered = false, framed = false; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 43 | |
| 44 | try |
| 45 | { |
| 46 | for (int i = 0; i < args.Length; i++) |
| 47 | { |
| 48 | if (args[i] == "-h") |
| 49 | { |
| 50 | string[] hostport = args[++i].Split(':'); |
| 51 | host = hostport[0]; |
| 52 | if (hostport.Length > 1) |
| 53 | { |
| 54 | port = Convert.ToInt32(hostport[1]); |
| 55 | } |
| 56 | } |
| 57 | else if (args[i] == "-u") |
| 58 | { |
| 59 | url = args[++i]; |
| 60 | } |
| 61 | else if (args[i] == "-n") |
| 62 | { |
| 63 | numIterations = Convert.ToInt32(args[++i]); |
| 64 | } |
| 65 | else if (args[i] == "-b" || args[i] == "-buffered") |
| 66 | { |
| 67 | buffered = true; |
| 68 | Console.WriteLine("Using buffered sockets"); |
| 69 | } |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame^] | 70 | else if (args[i] == "-f" || args[i] == "-framed") |
| 71 | { |
| 72 | framed = true; |
| 73 | Console.WriteLine("Using framed transport"); |
| 74 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 75 | else if (args[i] == "-t") |
| 76 | { |
| 77 | numThreads = Convert.ToInt32(args[++i]); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | catch (Exception e) |
| 82 | { |
| 83 | Console.WriteLine(e.StackTrace); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | //issue tests on separate threads simultaneously |
| 89 | Thread[] threads = new Thread[numThreads]; |
| 90 | DateTime start = DateTime.Now; |
| 91 | for (int test = 0; test < numThreads; test++) |
| 92 | { |
| 93 | Thread t = new Thread(new ParameterizedThreadStart(ClientThread)); |
| 94 | threads[test] = t; |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 95 | if (url == null) |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 96 | { |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame^] | 97 | TTransport trans = new TSocket(host, port); |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 98 | if (buffered) |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame^] | 99 | trans = new TBufferedTransport(trans as TStreamTransport); |
| 100 | if (framed) |
| 101 | trans = new TFramedTransport(trans); |
| 102 | |
| 103 | t.Start(trans); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
Bryan Duxbury | 6235947 | 2010-06-24 20:34:34 +0000 | [diff] [blame] | 107 | THttpClient http = new THttpClient(new Uri(url)); |
| 108 | t.Start(http); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | for (int test = 0; test < numThreads; test++) |
| 113 | { |
| 114 | threads[test].Join(); |
| 115 | } |
| 116 | Console.Write("Total time: " + (DateTime.Now - start)); |
| 117 | } |
| 118 | catch (Exception outerEx) |
| 119 | { |
| 120 | Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace); |
| 121 | } |
| 122 | |
| 123 | Console.WriteLine(); |
| 124 | Console.WriteLine(); |
| 125 | } |
| 126 | |
| 127 | public static void ClientThread(object obj) |
| 128 | { |
| 129 | TTransport transport = (TTransport)obj; |
| 130 | for (int i = 0; i < numIterations; i++) |
| 131 | { |
| 132 | ClientTest(transport); |
| 133 | } |
| 134 | transport.Close(); |
| 135 | } |
| 136 | |
| 137 | public static void ClientTest(TTransport transport) |
| 138 | { |
| 139 | TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport); |
| 140 | |
| 141 | ThriftTest.Client client = new ThriftTest.Client(binaryProtocol); |
| 142 | try |
| 143 | { |
| 144 | if (!transport.IsOpen) |
| 145 | { |
| 146 | transport.Open(); |
| 147 | } |
| 148 | } |
| 149 | catch (TTransportException ttx) |
| 150 | { |
| 151 | Console.WriteLine("Connect failed: " + ttx.Message); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | long start = DateTime.Now.ToFileTime(); |
| 156 | |
| 157 | Console.Write("testVoid()"); |
| 158 | client.testVoid(); |
| 159 | Console.WriteLine(" = void"); |
| 160 | |
| 161 | Console.Write("testString(\"Test\")"); |
| 162 | string s = client.testString("Test"); |
| 163 | Console.WriteLine(" = \"" + s + "\""); |
| 164 | |
| 165 | Console.Write("testByte(1)"); |
| 166 | byte i8 = client.testByte((byte)1); |
| 167 | Console.WriteLine(" = " + i8); |
| 168 | |
| 169 | Console.Write("testI32(-1)"); |
| 170 | int i32 = client.testI32(-1); |
| 171 | Console.WriteLine(" = " + i32); |
| 172 | |
| 173 | Console.Write("testI64(-34359738368)"); |
| 174 | long i64 = client.testI64(-34359738368); |
| 175 | Console.WriteLine(" = " + i64); |
| 176 | |
| 177 | Console.Write("testDouble(5.325098235)"); |
| 178 | double dub = client.testDouble(5.325098235); |
| 179 | Console.WriteLine(" = " + dub); |
| 180 | |
| 181 | Console.Write("testStruct({\"Zero\", 1, -3, -5})"); |
| 182 | Xtruct o = new Xtruct(); |
| 183 | o.String_thing = "Zero"; |
| 184 | o.Byte_thing = (byte)1; |
| 185 | o.I32_thing = -3; |
| 186 | o.I64_thing = -5; |
| 187 | Xtruct i = client.testStruct(o); |
| 188 | Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}"); |
| 189 | |
| 190 | Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})"); |
| 191 | Xtruct2 o2 = new Xtruct2(); |
| 192 | o2.Byte_thing = (byte)1; |
| 193 | o2.Struct_thing = o; |
| 194 | o2.I32_thing = 5; |
| 195 | Xtruct2 i2 = client.testNest(o2); |
| 196 | i = i2.Struct_thing; |
| 197 | Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}"); |
| 198 | |
| 199 | Dictionary<int, int> mapout = new Dictionary<int, int>(); |
| 200 | for (int j = 0; j < 5; j++) |
| 201 | { |
| 202 | mapout[j] = j - 10; |
| 203 | } |
| 204 | Console.Write("testMap({"); |
| 205 | bool first = true; |
| 206 | foreach (int key in mapout.Keys) |
| 207 | { |
| 208 | if (first) |
| 209 | { |
| 210 | first = false; |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | Console.Write(", "); |
| 215 | } |
| 216 | Console.Write(key + " => " + mapout[key]); |
| 217 | } |
| 218 | Console.Write("})"); |
| 219 | |
| 220 | Dictionary<int, int> mapin = client.testMap(mapout); |
| 221 | |
| 222 | Console.Write(" = {"); |
| 223 | first = true; |
| 224 | foreach (int key in mapin.Keys) |
| 225 | { |
| 226 | if (first) |
| 227 | { |
| 228 | first = false; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | Console.Write(", "); |
| 233 | } |
| 234 | Console.Write(key + " => " + mapin[key]); |
| 235 | } |
| 236 | Console.WriteLine("}"); |
| 237 | |
| 238 | List<int> listout = new List<int>(); |
| 239 | for (int j = -2; j < 3; j++) |
| 240 | { |
| 241 | listout.Add(j); |
| 242 | } |
| 243 | Console.Write("testList({"); |
| 244 | first = true; |
| 245 | foreach (int j in listout) |
| 246 | { |
| 247 | if (first) |
| 248 | { |
| 249 | first = false; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | Console.Write(", "); |
| 254 | } |
| 255 | Console.Write(j); |
| 256 | } |
| 257 | Console.Write("})"); |
| 258 | |
| 259 | List<int> listin = client.testList(listout); |
| 260 | |
| 261 | Console.Write(" = {"); |
| 262 | first = true; |
| 263 | foreach (int j in listin) |
| 264 | { |
| 265 | if (first) |
| 266 | { |
| 267 | first = false; |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | Console.Write(", "); |
| 272 | } |
| 273 | Console.Write(j); |
| 274 | } |
| 275 | Console.WriteLine("}"); |
| 276 | |
| 277 | //set |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 278 | THashSet<int> setout = new THashSet<int>(); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 279 | for (int j = -2; j < 3; j++) |
| 280 | { |
| 281 | setout.Add(j); |
| 282 | } |
| 283 | Console.Write("testSet({"); |
| 284 | first = true; |
| 285 | foreach (int j in setout) |
| 286 | { |
| 287 | if (first) |
| 288 | { |
| 289 | first = false; |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | Console.Write(", "); |
| 294 | } |
| 295 | Console.Write(j); |
| 296 | } |
| 297 | Console.Write("})"); |
| 298 | |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 299 | THashSet<int> setin = client.testSet(setout); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 300 | |
| 301 | Console.Write(" = {"); |
| 302 | first = true; |
| 303 | foreach (int j in setin) |
| 304 | { |
| 305 | if (first) |
| 306 | { |
| 307 | first = false; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | Console.Write(", "); |
| 312 | } |
| 313 | Console.Write(j); |
| 314 | } |
| 315 | Console.WriteLine("}"); |
| 316 | |
| 317 | |
| 318 | Console.Write("testEnum(ONE)"); |
| 319 | Numberz ret = client.testEnum(Numberz.ONE); |
| 320 | Console.WriteLine(" = " + ret); |
| 321 | |
| 322 | Console.Write("testEnum(TWO)"); |
| 323 | ret = client.testEnum(Numberz.TWO); |
| 324 | Console.WriteLine(" = " + ret); |
| 325 | |
| 326 | Console.Write("testEnum(THREE)"); |
| 327 | ret = client.testEnum(Numberz.THREE); |
| 328 | Console.WriteLine(" = " + ret); |
| 329 | |
| 330 | Console.Write("testEnum(FIVE)"); |
| 331 | ret = client.testEnum(Numberz.FIVE); |
| 332 | Console.WriteLine(" = " + ret); |
| 333 | |
| 334 | Console.Write("testEnum(EIGHT)"); |
| 335 | ret = client.testEnum(Numberz.EIGHT); |
| 336 | Console.WriteLine(" = " + ret); |
| 337 | |
| 338 | Console.Write("testTypedef(309858235082523)"); |
| 339 | long uid = client.testTypedef(309858235082523L); |
| 340 | Console.WriteLine(" = " + uid); |
| 341 | |
| 342 | Console.Write("testMapMap(1)"); |
| 343 | Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1); |
| 344 | Console.Write(" = {"); |
| 345 | foreach (int key in mm.Keys) |
| 346 | { |
| 347 | Console.Write(key + " => {"); |
| 348 | Dictionary<int, int> m2 = mm[key]; |
| 349 | foreach (int k2 in m2.Keys) |
| 350 | { |
| 351 | Console.Write(k2 + " => " + m2[k2] + ", "); |
| 352 | } |
| 353 | Console.Write("}, "); |
| 354 | } |
| 355 | Console.WriteLine("}"); |
| 356 | |
| 357 | Insanity insane = new Insanity(); |
| 358 | insane.UserMap = new Dictionary<Numberz, long>(); |
| 359 | insane.UserMap[Numberz.FIVE] = 5000L; |
| 360 | Xtruct truck = new Xtruct(); |
| 361 | truck.String_thing = "Truck"; |
| 362 | truck.Byte_thing = (byte)8; |
| 363 | truck.I32_thing = 8; |
| 364 | truck.I64_thing = 8; |
| 365 | insane.Xtructs = new List<Xtruct>(); |
| 366 | insane.Xtructs.Add(truck); |
| 367 | Console.Write("testInsanity()"); |
| 368 | Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane); |
| 369 | Console.Write(" = {"); |
| 370 | foreach (long key in whoa.Keys) |
| 371 | { |
| 372 | Dictionary<Numberz, Insanity> val = whoa[key]; |
| 373 | Console.Write(key + " => {"); |
| 374 | |
| 375 | foreach (Numberz k2 in val.Keys) |
| 376 | { |
| 377 | Insanity v2 = val[k2]; |
| 378 | |
| 379 | Console.Write(k2 + " => {"); |
| 380 | Dictionary<Numberz, long> userMap = v2.UserMap; |
| 381 | |
| 382 | Console.Write("{"); |
| 383 | if (userMap != null) |
| 384 | { |
| 385 | foreach (Numberz k3 in userMap.Keys) |
| 386 | { |
| 387 | Console.Write(k3 + " => " + userMap[k3] + ", "); |
| 388 | } |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | Console.Write("null"); |
| 393 | } |
| 394 | Console.Write("}, "); |
| 395 | |
| 396 | List<Xtruct> xtructs = v2.Xtructs; |
| 397 | |
| 398 | Console.Write("{"); |
| 399 | if (xtructs != null) |
| 400 | { |
| 401 | foreach (Xtruct x in xtructs) |
| 402 | { |
| 403 | Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, "); |
| 404 | } |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | Console.Write("null"); |
| 409 | } |
| 410 | Console.Write("}"); |
| 411 | |
| 412 | Console.Write("}, "); |
| 413 | } |
| 414 | Console.Write("}, "); |
| 415 | } |
| 416 | Console.WriteLine("}"); |
| 417 | |
| 418 | |
| 419 | byte arg0 = 1; |
| 420 | int arg1 = 2; |
| 421 | long arg2 = long.MaxValue; |
| 422 | Dictionary<short, string> multiDict = new Dictionary<short, string>(); |
| 423 | multiDict[1] = "one"; |
| 424 | Numberz arg4 = Numberz.FIVE; |
| 425 | long arg5 = 5000000; |
| 426 | Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")"); |
| 427 | Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5); |
| 428 | Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing |
| 429 | + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n"); |
| 430 | |
David Reiss | 6ce401d | 2009-03-24 20:01:58 +0000 | [diff] [blame] | 431 | Console.WriteLine("Test Oneway(1)"); |
| 432 | client.testOneway(1); |
T Jake Luciani | 7070aaa | 2011-01-27 02:51:51 +0000 | [diff] [blame^] | 433 | |
| 434 | Console.Write("Test Calltime()"); |
| 435 | var startt = DateTime.UtcNow; |
| 436 | for ( int k=0; k<1000; ++k ) |
| 437 | client.testVoid(); |
| 438 | Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" ); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | } |