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