blob: fc3e41856247f769090c4617a482f5d1c26cbf6e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Reiss63191332009-01-06 19:49:22 +000020using System;
21using System.Collections.Generic;
David Reissd831a212009-02-13 03:09:52 +000022using System.Threading;
23using Thrift.Collections;
David Reiss63191332009-01-06 19:49:22 +000024using Thrift.Protocol;
25using Thrift.Transport;
26using Thrift.Test;
David Reiss63191332009-01-06 19:49:22 +000027
28namespace Test
29{
Jens Geyerd5436f52014-10-03 19:50:38 +020030 public class TestClient
31 {
32 private static int numIterations = 1;
33 private static string protocol = "";
David Reiss63191332009-01-06 19:49:22 +000034
Jens Geyerd5436f52014-10-03 19:50:38 +020035 public static void Execute(string[] args)
36 {
37 try
38 {
39 string host = "localhost";
40 int port = 9090;
41 string url = null, pipe = null;
42 int numThreads = 1;
43 bool buffered = false, framed = false, encrypted = false;
David Reiss63191332009-01-06 19:49:22 +000044
Jens Geyerd5436f52014-10-03 19:50:38 +020045 try
46 {
47 for (int i = 0; i < args.Length; i++)
48 {
49 if (args[i] == "-u")
50 {
51 url = args[++i];
52 }
53 else if (args[i] == "-n")
54 {
55 numIterations = Convert.ToInt32(args[++i]);
56 }
57 else if (args[i] == "-pipe") // -pipe <name>
58 {
59 pipe = args[++i];
60 Console.WriteLine("Using named pipes transport");
61 }
62 else if (args[i].Contains("--host="))
63 {
64 host = args[i].Substring(args[i].IndexOf("=") + 1);
65 }
66 else if (args[i].Contains("--port="))
67 {
68 port = int.Parse(args[i].Substring(args[i].IndexOf("=")+1));
69 }
70 else if (args[i] == "-b" || args[i] == "--buffered" || args[i] == "--transport=buffered")
71 {
72 buffered = true;
73 Console.WriteLine("Using buffered sockets");
74 }
75 else if (args[i] == "-f" || args[i] == "--framed" || args[i] == "--transport=framed")
76 {
77 framed = true;
78 Console.WriteLine("Using framed transport");
79 }
80 else if (args[i] == "-t")
81 {
82 numThreads = Convert.ToInt32(args[++i]);
83 }
84 else if (args[i] == "--compact" || args[i] == "--protocol=compact")
85 {
86 protocol = "compact";
87 Console.WriteLine("Using compact protocol");
88 }
89 else if (args[i] == "--json" || args[i] == "--protocol=json")
90 {
91 protocol = "json";
92 Console.WriteLine("Using JSON protocol");
93 }
94 else if (args[i] == "--ssl")
95 {
96 encrypted = true;
97 Console.WriteLine("Using encrypted transport");
98 }
99 }
100 }
101 catch (Exception e)
102 {
103 Console.WriteLine(e.StackTrace);
104 }
David Reiss63191332009-01-06 19:49:22 +0000105
Jens Geyerd5436f52014-10-03 19:50:38 +0200106 //issue tests on separate threads simultaneously
107 Thread[] threads = new Thread[numThreads];
108 DateTime start = DateTime.Now;
109 for (int test = 0; test < numThreads; test++)
110 {
111 Thread t = new Thread(new ParameterizedThreadStart(ClientThread));
112 threads[test] = t;
113 if (url == null)
114 {
115 // endpoint transport
116 TTransport trans = null;
117 if (pipe != null)
118 trans = new TNamedPipeClientTransport(pipe);
119 else
120 {
121 if (encrypted)
122 trans = new TTLSSocket(host, port, "../../../../../keys/client.pem");
123 else
124 trans = new TSocket(host, port);
125 }
Jens Geyerc1d79432014-04-22 22:52:43 +0200126
Jens Geyerd5436f52014-10-03 19:50:38 +0200127 // layered transport
128 if (buffered)
129 trans = new TBufferedTransport(trans as TStreamTransport);
130 if (framed)
131 trans = new TFramedTransport(trans);
Jens Geyerc1d79432014-04-22 22:52:43 +0200132
Jens Geyerd5436f52014-10-03 19:50:38 +0200133 //ensure proper open/close of transport
134 trans.Open();
135 trans.Close();
136 t.Start(trans);
137 }
138 else
139 {
140 THttpClient http = new THttpClient(new Uri(url));
141 t.Start(http);
142 }
143 }
David Reiss63191332009-01-06 19:49:22 +0000144
Jens Geyerd5436f52014-10-03 19:50:38 +0200145 for (int test = 0; test < numThreads; test++)
146 {
147 threads[test].Join();
148 }
149 Console.Write("Total time: " + (DateTime.Now - start));
150 }
151 catch (Exception outerEx)
152 {
153 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
154 }
David Reiss63191332009-01-06 19:49:22 +0000155
Jens Geyerd5436f52014-10-03 19:50:38 +0200156 Console.WriteLine();
157 Console.WriteLine();
158 }
David Reiss63191332009-01-06 19:49:22 +0000159
Jens Geyerd5436f52014-10-03 19:50:38 +0200160 public static void ClientThread(object obj)
161 {
162 TTransport transport = (TTransport)obj;
163 for (int i = 0; i < numIterations; i++)
164 {
165 ClientTest(transport);
166 }
167 transport.Close();
168 }
David Reiss63191332009-01-06 19:49:22 +0000169
Jens Geyer71e814a2014-12-13 23:40:35 +0100170 public static string BytesToHex(byte[] data) {
171 return BitConverter.ToString(data).Replace("-", string.Empty);
172 }
173
174 public static byte[] PrepareTestData(bool randomDist)
175 {
176 byte[] retval = new byte[0x100];
177 int initLen = Math.Min(0x100,retval.Length);
178
179 // linear distribution, unless random is requested
180 if (!randomDist) {
181 for (var i = 0; i < initLen; ++i) {
182 retval[i] = (byte)i;
183 }
184 return retval;
185 }
186
187 // random distribution
188 for (var i = 0; i < initLen; ++i) {
189 retval[i] = (byte)0;
190 }
191 var rnd = new Random();
192 for (var i = 1; i < initLen; ++i) {
193 while( true) {
194 int nextPos = rnd.Next() % initLen;
195 if (retval[nextPos] == 0) {
196 retval[nextPos] = (byte)i;
197 break;
198 }
199 }
200 }
201 return retval;
202 }
203
Jens Geyerd5436f52014-10-03 19:50:38 +0200204 public static void ClientTest(TTransport transport)
205 {
206 TProtocol proto;
207 if (protocol == "compact")
208 proto = new TCompactProtocol(transport);
209 else if (protocol == "json")
210 proto = new TJSONProtocol(transport);
211 else
212 proto = new TBinaryProtocol(transport);
David Reiss63191332009-01-06 19:49:22 +0000213
Jens Geyerd5436f52014-10-03 19:50:38 +0200214 ThriftTest.Client client = new ThriftTest.Client(proto);
215 try
216 {
217 if (!transport.IsOpen)
218 {
219 transport.Open();
220 }
221 }
222 catch (TTransportException ttx)
223 {
224 Console.WriteLine("Connect failed: " + ttx.Message);
225 return;
226 }
David Reiss63191332009-01-06 19:49:22 +0000227
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 long start = DateTime.Now.ToFileTime();
David Reiss63191332009-01-06 19:49:22 +0000229
Jens Geyerd5436f52014-10-03 19:50:38 +0200230 Console.Write("testVoid()");
231 client.testVoid();
232 Console.WriteLine(" = void");
David Reiss63191332009-01-06 19:49:22 +0000233
Jens Geyerd5436f52014-10-03 19:50:38 +0200234 Console.Write("testString(\"Test\")");
235 string s = client.testString("Test");
236 Console.WriteLine(" = \"" + s + "\"");
David Reiss63191332009-01-06 19:49:22 +0000237
Jens Geyerd5436f52014-10-03 19:50:38 +0200238 Console.Write("testByte(1)");
239 sbyte i8 = client.testByte((sbyte)1);
240 Console.WriteLine(" = " + i8);
David Reiss63191332009-01-06 19:49:22 +0000241
Jens Geyerd5436f52014-10-03 19:50:38 +0200242 Console.Write("testI32(-1)");
243 int i32 = client.testI32(-1);
244 Console.WriteLine(" = " + i32);
David Reiss63191332009-01-06 19:49:22 +0000245
Jens Geyerd5436f52014-10-03 19:50:38 +0200246 Console.Write("testI64(-34359738368)");
247 long i64 = client.testI64(-34359738368);
248 Console.WriteLine(" = " + i64);
David Reiss63191332009-01-06 19:49:22 +0000249
Jens Geyerd5436f52014-10-03 19:50:38 +0200250 Console.Write("testDouble(5.325098235)");
251 double dub = client.testDouble(5.325098235);
252 Console.WriteLine(" = " + dub);
David Reiss63191332009-01-06 19:49:22 +0000253
Jens Geyer71e814a2014-12-13 23:40:35 +0100254 byte[] binOut = PrepareTestData(true);
255 Console.Write("testBinary(" + BytesToHex(binOut) + ")");
256 try
257 {
258 byte[] binIn = client.testBinary(binOut);
259 Console.WriteLine(" = " + BytesToHex(binIn));
260 if (binIn.Length != binOut.Length)
261 throw new Exception("testBinary: length mismatch");
262 for (int ofs = 0; ofs < Math.Min(binIn.Length, binOut.Length); ++ofs)
263 if (binIn[ofs] != binOut[ofs])
264 throw new Exception("testBinary: content mismatch at offset " + ofs.ToString());
265 }
266 catch (Thrift.TApplicationException e)
267 {
268 Console.Write("testBinary(" + BytesToHex(binOut) + "): "+e.Message);
269 }
270
Jens Geyerd5436f52014-10-03 19:50:38 +0200271 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
272 Xtruct o = new Xtruct();
273 o.String_thing = "Zero";
274 o.Byte_thing = (sbyte)1;
275 o.I32_thing = -3;
276 o.I64_thing = -5;
277 Xtruct i = client.testStruct(o);
278 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000279
Jens Geyerd5436f52014-10-03 19:50:38 +0200280 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
281 Xtruct2 o2 = new Xtruct2();
282 o2.Byte_thing = (sbyte)1;
283 o2.Struct_thing = o;
284 o2.I32_thing = 5;
285 Xtruct2 i2 = client.testNest(o2);
286 i = i2.Struct_thing;
287 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
David Reiss63191332009-01-06 19:49:22 +0000288
Jens Geyerd5436f52014-10-03 19:50:38 +0200289 Dictionary<int, int> mapout = new Dictionary<int, int>();
290 for (int j = 0; j < 5; j++)
291 {
292 mapout[j] = j - 10;
293 }
294 Console.Write("testMap({");
295 bool first = true;
296 foreach (int key in mapout.Keys)
297 {
298 if (first)
299 {
300 first = false;
301 }
302 else
303 {
304 Console.Write(", ");
305 }
306 Console.Write(key + " => " + mapout[key]);
307 }
308 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000309
Jens Geyerd5436f52014-10-03 19:50:38 +0200310 Dictionary<int, int> mapin = client.testMap(mapout);
David Reiss63191332009-01-06 19:49:22 +0000311
Jens Geyerd5436f52014-10-03 19:50:38 +0200312 Console.Write(" = {");
313 first = true;
314 foreach (int key in mapin.Keys)
315 {
316 if (first)
317 {
318 first = false;
319 }
320 else
321 {
322 Console.Write(", ");
323 }
324 Console.Write(key + " => " + mapin[key]);
325 }
326 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000327
Jens Geyerd5436f52014-10-03 19:50:38 +0200328 List<int> listout = new List<int>();
329 for (int j = -2; j < 3; j++)
330 {
331 listout.Add(j);
332 }
333 Console.Write("testList({");
334 first = true;
335 foreach (int j in listout)
336 {
337 if (first)
338 {
339 first = false;
340 }
341 else
342 {
343 Console.Write(", ");
344 }
345 Console.Write(j);
346 }
347 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000348
Jens Geyerd5436f52014-10-03 19:50:38 +0200349 List<int> listin = client.testList(listout);
David Reiss63191332009-01-06 19:49:22 +0000350
Jens Geyerd5436f52014-10-03 19:50:38 +0200351 Console.Write(" = {");
352 first = true;
353 foreach (int j in listin)
354 {
355 if (first)
356 {
357 first = false;
358 }
359 else
360 {
361 Console.Write(", ");
362 }
363 Console.Write(j);
364 }
365 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000366
Jens Geyerd5436f52014-10-03 19:50:38 +0200367 //set
368 THashSet<int> setout = new THashSet<int>();
369 for (int j = -2; j < 3; j++)
370 {
371 setout.Add(j);
372 }
373 Console.Write("testSet({");
374 first = true;
375 foreach (int j in setout)
376 {
377 if (first)
378 {
379 first = false;
380 }
381 else
382 {
383 Console.Write(", ");
384 }
385 Console.Write(j);
386 }
387 Console.Write("})");
David Reiss63191332009-01-06 19:49:22 +0000388
Jens Geyerd5436f52014-10-03 19:50:38 +0200389 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000390
Jens Geyerd5436f52014-10-03 19:50:38 +0200391 Console.Write(" = {");
392 first = true;
393 foreach (int j in setin)
394 {
395 if (first)
396 {
397 first = false;
398 }
399 else
400 {
401 Console.Write(", ");
402 }
403 Console.Write(j);
404 }
405 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000406
407
Jens Geyerd5436f52014-10-03 19:50:38 +0200408 Console.Write("testEnum(ONE)");
409 Numberz ret = client.testEnum(Numberz.ONE);
410 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000411
Jens Geyerd5436f52014-10-03 19:50:38 +0200412 Console.Write("testEnum(TWO)");
413 ret = client.testEnum(Numberz.TWO);
414 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000415
Jens Geyerd5436f52014-10-03 19:50:38 +0200416 Console.Write("testEnum(THREE)");
417 ret = client.testEnum(Numberz.THREE);
418 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000419
Jens Geyerd5436f52014-10-03 19:50:38 +0200420 Console.Write("testEnum(FIVE)");
421 ret = client.testEnum(Numberz.FIVE);
422 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000423
Jens Geyerd5436f52014-10-03 19:50:38 +0200424 Console.Write("testEnum(EIGHT)");
425 ret = client.testEnum(Numberz.EIGHT);
426 Console.WriteLine(" = " + ret);
David Reiss63191332009-01-06 19:49:22 +0000427
Jens Geyerd5436f52014-10-03 19:50:38 +0200428 Console.Write("testTypedef(309858235082523)");
429 long uid = client.testTypedef(309858235082523L);
430 Console.WriteLine(" = " + uid);
David Reiss63191332009-01-06 19:49:22 +0000431
Jens Geyerd5436f52014-10-03 19:50:38 +0200432 Console.Write("testMapMap(1)");
433 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
434 Console.Write(" = {");
435 foreach (int key in mm.Keys)
436 {
437 Console.Write(key + " => {");
438 Dictionary<int, int> m2 = mm[key];
439 foreach (int k2 in m2.Keys)
440 {
441 Console.Write(k2 + " => " + m2[k2] + ", ");
442 }
443 Console.Write("}, ");
444 }
445 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000446
Jens Geyerd5436f52014-10-03 19:50:38 +0200447 Insanity insane = new Insanity();
448 insane.UserMap = new Dictionary<Numberz, long>();
449 insane.UserMap[Numberz.FIVE] = 5000L;
450 Xtruct truck = new Xtruct();
451 truck.String_thing = "Truck";
452 truck.Byte_thing = (sbyte)8;
453 truck.I32_thing = 8;
454 truck.I64_thing = 8;
455 insane.Xtructs = new List<Xtruct>();
456 insane.Xtructs.Add(truck);
457 Console.Write("testInsanity()");
458 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
459 Console.Write(" = {");
460 foreach (long key in whoa.Keys)
461 {
462 Dictionary<Numberz, Insanity> val = whoa[key];
463 Console.Write(key + " => {");
David Reiss63191332009-01-06 19:49:22 +0000464
Jens Geyerd5436f52014-10-03 19:50:38 +0200465 foreach (Numberz k2 in val.Keys)
466 {
467 Insanity v2 = val[k2];
David Reiss63191332009-01-06 19:49:22 +0000468
Jens Geyerd5436f52014-10-03 19:50:38 +0200469 Console.Write(k2 + " => {");
470 Dictionary<Numberz, long> userMap = v2.UserMap;
David Reiss63191332009-01-06 19:49:22 +0000471
Jens Geyerd5436f52014-10-03 19:50:38 +0200472 Console.Write("{");
473 if (userMap != null)
474 {
475 foreach (Numberz k3 in userMap.Keys)
476 {
477 Console.Write(k3 + " => " + userMap[k3] + ", ");
478 }
479 }
480 else
481 {
482 Console.Write("null");
483 }
484 Console.Write("}, ");
David Reiss63191332009-01-06 19:49:22 +0000485
Jens Geyerd5436f52014-10-03 19:50:38 +0200486 List<Xtruct> xtructs = v2.Xtructs;
David Reiss63191332009-01-06 19:49:22 +0000487
Jens Geyerd5436f52014-10-03 19:50:38 +0200488 Console.Write("{");
489 if (xtructs != null)
490 {
491 foreach (Xtruct x in xtructs)
492 {
493 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
494 }
495 }
496 else
497 {
498 Console.Write("null");
499 }
500 Console.Write("}");
David Reiss63191332009-01-06 19:49:22 +0000501
Jens Geyerd5436f52014-10-03 19:50:38 +0200502 Console.Write("}, ");
503 }
504 Console.Write("}, ");
505 }
506 Console.WriteLine("}");
David Reiss63191332009-01-06 19:49:22 +0000507
Jens Geyerd5436f52014-10-03 19:50:38 +0200508 sbyte arg0 = 1;
509 int arg1 = 2;
510 long arg2 = long.MaxValue;
511 Dictionary<short, string> multiDict = new Dictionary<short, string>();
512 multiDict[1] = "one";
513 Numberz arg4 = Numberz.FIVE;
514 long arg5 = 5000000;
515 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
516 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
517 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
518 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
David Reiss63191332009-01-06 19:49:22 +0000519
Jens Geyerd5436f52014-10-03 19:50:38 +0200520 Console.WriteLine("Test Oneway(1)");
521 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000522
Jens Geyerd5436f52014-10-03 19:50:38 +0200523 Console.Write("Test Calltime()");
524 var startt = DateTime.UtcNow;
525 for ( int k=0; k<1000; ++k )
526 client.testVoid();
527 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
528 }
529 }
David Reiss63191332009-01-06 19:49:22 +0000530}