blob: c7b81b4c047dfe50c479dce146ee1f28b7964386 [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{
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 Luciani7070aaa2011-01-27 02:51:51 +000042 bool buffered = false, framed = false;
David Reiss63191332009-01-06 19:49:22 +000043
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 Luciani7070aaa2011-01-27 02:51:51 +000070 else if (args[i] == "-f" || args[i] == "-framed")
71 {
72 framed = true;
73 Console.WriteLine("Using framed transport");
74 }
David Reiss63191332009-01-06 19:49:22 +000075 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 Duxbury62359472010-06-24 20:34:34 +000095 if (url == null)
David Reiss63191332009-01-06 19:49:22 +000096 {
T Jake Luciani7070aaa2011-01-27 02:51:51 +000097 TTransport trans = new TSocket(host, port);
Bryan Duxbury62359472010-06-24 20:34:34 +000098 if (buffered)
T Jake Luciani7070aaa2011-01-27 02:51:51 +000099 trans = new TBufferedTransport(trans as TStreamTransport);
100 if (framed)
101 trans = new TFramedTransport(trans);
Jens Geyerf6acf442014-01-02 22:58:43 +0100102
103 //ensure proper open/close of transport
104 trans.Open();
105 trans.Close();
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000106 t.Start(trans);
David Reiss63191332009-01-06 19:49:22 +0000107 }
108 else
109 {
Bryan Duxbury62359472010-06-24 20:34:34 +0000110 THttpClient http = new THttpClient(new Uri(url));
111 t.Start(http);
David Reiss63191332009-01-06 19:49:22 +0000112 }
113 }
114
115 for (int test = 0; test < numThreads; test++)
116 {
117 threads[test].Join();
118 }
119 Console.Write("Total time: " + (DateTime.Now - start));
120 }
121 catch (Exception outerEx)
122 {
123 Console.WriteLine(outerEx.Message + " ST: " + outerEx.StackTrace);
124 }
125
126 Console.WriteLine();
127 Console.WriteLine();
128 }
129
130 public static void ClientThread(object obj)
131 {
132 TTransport transport = (TTransport)obj;
133 for (int i = 0; i < numIterations; i++)
134 {
135 ClientTest(transport);
136 }
137 transport.Close();
138 }
139
140 public static void ClientTest(TTransport transport)
141 {
142 TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport);
143
144 ThriftTest.Client client = new ThriftTest.Client(binaryProtocol);
145 try
146 {
147 if (!transport.IsOpen)
148 {
149 transport.Open();
150 }
151 }
152 catch (TTransportException ttx)
153 {
154 Console.WriteLine("Connect failed: " + ttx.Message);
155 return;
156 }
157
158 long start = DateTime.Now.ToFileTime();
159
160 Console.Write("testVoid()");
161 client.testVoid();
162 Console.WriteLine(" = void");
163
164 Console.Write("testString(\"Test\")");
165 string s = client.testString("Test");
166 Console.WriteLine(" = \"" + s + "\"");
167
168 Console.Write("testByte(1)");
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200169 sbyte i8 = client.testByte((sbyte)1);
David Reiss63191332009-01-06 19:49:22 +0000170 Console.WriteLine(" = " + i8);
171
172 Console.Write("testI32(-1)");
173 int i32 = client.testI32(-1);
174 Console.WriteLine(" = " + i32);
175
176 Console.Write("testI64(-34359738368)");
177 long i64 = client.testI64(-34359738368);
178 Console.WriteLine(" = " + i64);
179
180 Console.Write("testDouble(5.325098235)");
181 double dub = client.testDouble(5.325098235);
182 Console.WriteLine(" = " + dub);
183
184 Console.Write("testStruct({\"Zero\", 1, -3, -5})");
185 Xtruct o = new Xtruct();
186 o.String_thing = "Zero";
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200187 o.Byte_thing = (sbyte)1;
David Reiss63191332009-01-06 19:49:22 +0000188 o.I32_thing = -3;
189 o.I64_thing = -5;
190 Xtruct i = client.testStruct(o);
191 Console.WriteLine(" = {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}");
192
193 Console.Write("testNest({1, {\"Zero\", 1, -3, -5}, 5})");
194 Xtruct2 o2 = new Xtruct2();
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200195 o2.Byte_thing = (sbyte)1;
David Reiss63191332009-01-06 19:49:22 +0000196 o2.Struct_thing = o;
197 o2.I32_thing = 5;
198 Xtruct2 i2 = client.testNest(o2);
199 i = i2.Struct_thing;
200 Console.WriteLine(" = {" + i2.Byte_thing + ", {\"" + i.String_thing + "\", " + i.Byte_thing + ", " + i.I32_thing + ", " + i.I64_thing + "}, " + i2.I32_thing + "}");
201
202 Dictionary<int, int> mapout = new Dictionary<int, int>();
203 for (int j = 0; j < 5; j++)
204 {
205 mapout[j] = j - 10;
206 }
207 Console.Write("testMap({");
208 bool first = true;
209 foreach (int key in mapout.Keys)
210 {
211 if (first)
212 {
213 first = false;
214 }
215 else
216 {
217 Console.Write(", ");
218 }
219 Console.Write(key + " => " + mapout[key]);
220 }
221 Console.Write("})");
222
223 Dictionary<int, int> mapin = client.testMap(mapout);
224
225 Console.Write(" = {");
226 first = true;
227 foreach (int key in mapin.Keys)
228 {
229 if (first)
230 {
231 first = false;
232 }
233 else
234 {
235 Console.Write(", ");
236 }
237 Console.Write(key + " => " + mapin[key]);
238 }
239 Console.WriteLine("}");
240
241 List<int> listout = new List<int>();
242 for (int j = -2; j < 3; j++)
243 {
244 listout.Add(j);
245 }
246 Console.Write("testList({");
247 first = true;
248 foreach (int j in listout)
249 {
250 if (first)
251 {
252 first = false;
253 }
254 else
255 {
256 Console.Write(", ");
257 }
258 Console.Write(j);
259 }
260 Console.Write("})");
261
262 List<int> listin = client.testList(listout);
263
264 Console.Write(" = {");
265 first = true;
266 foreach (int j in listin)
267 {
268 if (first)
269 {
270 first = false;
271 }
272 else
273 {
274 Console.Write(", ");
275 }
276 Console.Write(j);
277 }
278 Console.WriteLine("}");
279
280 //set
David Reissd831a212009-02-13 03:09:52 +0000281 THashSet<int> setout = new THashSet<int>();
David Reiss63191332009-01-06 19:49:22 +0000282 for (int j = -2; j < 3; j++)
283 {
284 setout.Add(j);
285 }
286 Console.Write("testSet({");
287 first = true;
288 foreach (int j in setout)
289 {
290 if (first)
291 {
292 first = false;
293 }
294 else
295 {
296 Console.Write(", ");
297 }
298 Console.Write(j);
299 }
300 Console.Write("})");
301
David Reissd831a212009-02-13 03:09:52 +0000302 THashSet<int> setin = client.testSet(setout);
David Reiss63191332009-01-06 19:49:22 +0000303
304 Console.Write(" = {");
305 first = true;
306 foreach (int j in setin)
307 {
308 if (first)
309 {
310 first = false;
311 }
312 else
313 {
314 Console.Write(", ");
315 }
316 Console.Write(j);
317 }
318 Console.WriteLine("}");
319
320
321 Console.Write("testEnum(ONE)");
322 Numberz ret = client.testEnum(Numberz.ONE);
323 Console.WriteLine(" = " + ret);
324
325 Console.Write("testEnum(TWO)");
326 ret = client.testEnum(Numberz.TWO);
327 Console.WriteLine(" = " + ret);
328
329 Console.Write("testEnum(THREE)");
330 ret = client.testEnum(Numberz.THREE);
331 Console.WriteLine(" = " + ret);
332
333 Console.Write("testEnum(FIVE)");
334 ret = client.testEnum(Numberz.FIVE);
335 Console.WriteLine(" = " + ret);
336
337 Console.Write("testEnum(EIGHT)");
338 ret = client.testEnum(Numberz.EIGHT);
339 Console.WriteLine(" = " + ret);
340
341 Console.Write("testTypedef(309858235082523)");
342 long uid = client.testTypedef(309858235082523L);
343 Console.WriteLine(" = " + uid);
344
345 Console.Write("testMapMap(1)");
346 Dictionary<int, Dictionary<int, int>> mm = client.testMapMap(1);
347 Console.Write(" = {");
348 foreach (int key in mm.Keys)
349 {
350 Console.Write(key + " => {");
351 Dictionary<int, int> m2 = mm[key];
352 foreach (int k2 in m2.Keys)
353 {
354 Console.Write(k2 + " => " + m2[k2] + ", ");
355 }
356 Console.Write("}, ");
357 }
358 Console.WriteLine("}");
359
360 Insanity insane = new Insanity();
361 insane.UserMap = new Dictionary<Numberz, long>();
362 insane.UserMap[Numberz.FIVE] = 5000L;
363 Xtruct truck = new Xtruct();
364 truck.String_thing = "Truck";
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200365 truck.Byte_thing = (sbyte)8;
David Reiss63191332009-01-06 19:49:22 +0000366 truck.I32_thing = 8;
367 truck.I64_thing = 8;
368 insane.Xtructs = new List<Xtruct>();
369 insane.Xtructs.Add(truck);
370 Console.Write("testInsanity()");
371 Dictionary<long, Dictionary<Numberz, Insanity>> whoa = client.testInsanity(insane);
372 Console.Write(" = {");
373 foreach (long key in whoa.Keys)
374 {
375 Dictionary<Numberz, Insanity> val = whoa[key];
376 Console.Write(key + " => {");
377
378 foreach (Numberz k2 in val.Keys)
379 {
380 Insanity v2 = val[k2];
381
382 Console.Write(k2 + " => {");
383 Dictionary<Numberz, long> userMap = v2.UserMap;
384
385 Console.Write("{");
386 if (userMap != null)
387 {
388 foreach (Numberz k3 in userMap.Keys)
389 {
390 Console.Write(k3 + " => " + userMap[k3] + ", ");
391 }
392 }
393 else
394 {
395 Console.Write("null");
396 }
397 Console.Write("}, ");
398
399 List<Xtruct> xtructs = v2.Xtructs;
400
401 Console.Write("{");
402 if (xtructs != null)
403 {
404 foreach (Xtruct x in xtructs)
405 {
406 Console.Write("{\"" + x.String_thing + "\", " + x.Byte_thing + ", " + x.I32_thing + ", " + x.I32_thing + "}, ");
407 }
408 }
409 else
410 {
411 Console.Write("null");
412 }
413 Console.Write("}");
414
415 Console.Write("}, ");
416 }
417 Console.Write("}, ");
418 }
419 Console.WriteLine("}");
420
421
Jens Geyer1b4c9b92013-04-26 23:38:58 +0200422 sbyte arg0 = 1;
David Reiss63191332009-01-06 19:49:22 +0000423 int arg1 = 2;
424 long arg2 = long.MaxValue;
425 Dictionary<short, string> multiDict = new Dictionary<short, string>();
426 multiDict[1] = "one";
427 Numberz arg4 = Numberz.FIVE;
428 long arg5 = 5000000;
429 Console.Write("Test Multi(" + arg0 + "," + arg1 + "," + arg2 + "," + multiDict + "," + arg4 + "," + arg5 + ")");
430 Xtruct multiResponse = client.testMulti(arg0, arg1, arg2, multiDict, arg4, arg5);
431 Console.Write(" = Xtruct(byte_thing:" + multiResponse.Byte_thing + ",String_thing:" + multiResponse.String_thing
432 + ",i32_thing:" + multiResponse.I32_thing + ",i64_thing:" + multiResponse.I64_thing + ")\n");
433
David Reiss6ce401d2009-03-24 20:01:58 +0000434 Console.WriteLine("Test Oneway(1)");
435 client.testOneway(1);
T Jake Luciani7070aaa2011-01-27 02:51:51 +0000436
437 Console.Write("Test Calltime()");
438 var startt = DateTime.UtcNow;
439 for ( int k=0; k<1000; ++k )
440 client.testVoid();
441 Console.WriteLine(" = " + (DateTime.UtcNow - startt).TotalSeconds.ToString() + " ms a testVoid() call" );
David Reiss63191332009-01-06 19:49:22 +0000442 }
443 }
444}