blob: b3c90178bd86e843032d29d615ed529cfbb60b5a [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +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
20unit TestClient;
21
22interface
23
24uses
25 SysUtils, Classes, Thrift.Protocol, Thrift.Transport, Thrift.Test,
26 Generics.Collections, Thrift.Collections, Windows, Thrift.Console,
27 DateUtils;
28
29type
30
31 TThreadConsole = class
32 private
33 FThread : TThread;
34 public
35 procedure Write( const S : string);
36 procedure WriteLine( const S : string);
37 constructor Create( AThread: TThread);
38 end;
39
40 TClientThread = class( TThread )
41 private
42 FTransport : ITransport;
43 FNumIteration : Integer;
44 FConsole : TThreadConsole;
45
46 procedure ClientTest;
47 protected
48 procedure Execute; override;
49 public
50 constructor Create(ATransport: ITransport; ANumIteration: Integer);
51 destructor Destroy; override;
52 end;
53
54 TTestClient = class
55 private
56 class var
57 FNumIteration : Integer;
58 FNumThread : Integer;
59 public
60 class procedure Execute( const args: array of string);
61 end;
62
63implementation
64
65{ TTestClient }
66
67class procedure TTestClient.Execute(const args: array of string);
68var
69 i : Integer;
70 host : string;
71 port : Integer;
72 url : string;
73 bBuffered : Boolean;
74 bFramed : Boolean;
75 s : string;
76 n : Integer;
77 threads : array of TThread;
78 dtStart : TDateTime;
79 test : Integer;
80 thread : TThread;
81 trans : ITransport;
82 streamtrans : IStreamTransport;
83 http : IHTTPClient;
84
85begin
86 bBuffered := False;;
87 bFramed := False;
88 try
89 host := 'localhost';
90 port := 9090;
91 url := '';
92 i := 0;
93 try
94 while ( i < Length(args) ) do
95 begin
96 try
97 if ( args[i] = '-h') then
98 begin
99 Inc( i );
100 s := args[i];
101 n := Pos( ':', s);
102 if ( n > 0 ) then
103 begin
104 host := Copy( s, 1, n - 1);
105 port := StrToInt( Copy( s, n + 1, MaxInt));
106 end else
107 begin
108 host := s;
109 end;
110 end else
111 if (args[i] = '-u') then
112 begin
113 Inc( i );
114 url := args[i];
115 end else
116 if (args[i] = '-n') then
117 begin
118 Inc( i );
119 FNumIteration := StrToInt( args[i] );
120 end else
121 if (args[i] = '-b') then
122 begin
123 bBuffered := True;
124 Console.WriteLine('Using buffered transport');
125 end else
126 if (args[i] = '-f' ) or ( args[i] = '-framed') then
127 begin
128 bFramed := True;
129 Console.WriteLine('Using framed transport');
130 end else
131 if (args[i] = '-t') then
132 begin
133 Inc( i );
134 FNumThread := StrToInt( args[i] );
135 end;
136 finally
137 Inc( i );
138 end;
139 end;
140 except
141 on E: Exception do
142 begin
143 Console.WriteLine( E.Message );
144 end;
145 end;
146
147 SetLength( threads, FNumThread);
148 dtStart := Now;
149
150 for test := 0 to FNumThread - 1 do
151 begin
152 if url = '' then
153 begin
154 streamtrans := TSocketImpl.Create( host, port );
155 trans := streamtrans;
156 if bBuffered then
157 begin
158 trans := TBufferedTransportImpl.Create( streamtrans );
159 end;
160
161 if bFramed then
162 begin
163 trans := TFramedTransportImpl.Create( trans );
164 end;
165 end else
166 begin
167 http := THTTPClientImpl.Create( url );
168 trans := http;
169 end;
170 thread := TClientThread.Create( trans, FNumIteration);
171 threads[test] := thread;
172{$WARN SYMBOL_DEPRECATED OFF}
173 thread.Resume;
174{$WARN SYMBOL_DEPRECATED ON}
175 end;
176
177 for test := 0 to FNumThread - 1 do
178 begin
179 threads[test].WaitFor;
180 end;
181
182 for test := 0 to FNumThread - 1 do
183 begin
184 threads[test].Free;
185 end;
186
187 Console.Write('Total time: ' + IntToStr( MilliSecondsBetween(Now, dtStart)));
188
189 except
190 on E: Exception do
191 begin
192 Console.WriteLine( E.Message + ' ST: ' + E.StackTrace );
193 end;
194 end;
195
196 Console.WriteLine('');
197 Console.WriteLine('done!');
198end;
199
200{ TClientThread }
201
202procedure TClientThread.ClientTest;
203var
204 binaryProtocol : TBinaryProtocolImpl;
205 client : TThriftTest.Iface;
206 s : string;
207 i8 : ShortInt;
208 i32 : Integer;
209 i64 : Int64;
210 dub : Double;
211 o : IXtruct;
212 o2 : IXtruct2;
213 i : IXtruct;
214 i2 : IXtruct2;
215 mapout : IThriftDictionary<Integer,Integer>;
216 mapin : IThriftDictionary<Integer,Integer>;
217 j : Integer;
218 first : Boolean;
219 key : Integer;
220 listout : IThriftList<Integer>;
221 listin : IThriftList<Integer>;
222 setout : IHashSet<Integer>;
223 setin : IHashSet<Integer>;
224 ret : TNumberz;
225 uid : Int64;
226 mm : IThriftDictionary<Integer, IThriftDictionary<Integer, Integer>>;
227 m2 : IThriftDictionary<Integer, Integer>;
228 k2 : Integer;
229 insane : IInsanity;
230 truck : IXtruct;
231 whoa : IThriftDictionary<Int64, IThriftDictionary<TNumberz, IInsanity>>;
232 key64 : Int64;
233 val : IThriftDictionary<TNumberz, IInsanity>;
234 k2_2 : TNumberz;
235 k3 : TNumberz;
236 v2 : IInsanity;
237 userMap : IThriftDictionary<TNumberz, Int64>;
238 xtructs : IThriftList<IXtruct>;
239 x : IXtruct;
240 arg0 : ShortInt;
241 arg1 : Integer;
242 arg2 : Int64;
243 multiDict : IThriftDictionary<SmallInt, string>;
244 arg4 : TNumberz;
245 arg5 : Int64;
246 StartTick : Cardinal;
247 k : Integer;
248 proc : TThreadProcedure;
249
250begin
251 binaryProtocol := TBinaryProtocolImpl.Create( FTransport );
252 client := TThriftTest.TClient.Create( binaryProtocol );
253 try
254 if not FTransport.IsOpen then
255 begin
256 FTransport.Open;
257 end;
258 except
259 on E: Exception do
260 begin
261 Console.WriteLine( E.Message );
262 Exit;
263 end;
264 end;
265
266 Console.Write('testException()');
267 try
268 client.testException('Xception');
269 except
270 on E: TXception do
271 begin
272 Console.WriteLine( ' = ' + IntToStr(E.ErrorCode) + ', ' + E.Message_ );
273 end;
274 end;
275
276 Console.Write('testVoid()');
277 client.testVoid();
278 Console.WriteLine(' = void');
279
280 Console.Write('testString(''Test'')');
281 s := client.testString('Test');
282 Console.WriteLine(' := ''' + s + '''');
283
284 Console.Write('testByte(1)');
285 i8 := client.testByte(1);
286 Console.WriteLine(' := ' + IntToStr( i8 ));
287
288 Console.Write('testI32(-1)');
289 i32 := client.testI32(-1);
290 Console.WriteLine(' := ' + IntToStr(i32));
291
292 Console.Write('testI64(-34359738368)');
293 i64 := client.testI64(-34359738368);
294 Console.WriteLine(' := ' + IntToStr( i64));
295
296 Console.Write('testDouble(5.325098235)');
297 dub := client.testDouble(5.325098235);
298 Console.WriteLine(' := ' + FloatToStr( dub));
299
300 Console.Write('testStruct({''Zero'', 1, -3, -5})');
301 o := TXtructImpl.Create;
302 o.String_thing := 'Zero';
303 o.Byte_thing := 1;
304 o.I32_thing := -3;
305 o.I64_thing := -5;
306 i := client.testStruct(o);
307 Console.WriteLine(' := {''' +
308 i.String_thing + ''', ' +
309 IntToStr( i.Byte_thing) + ', ' +
310 IntToStr( i.I32_thing) + ', ' +
311 IntToStr( i.I64_thing) + '}');
312
313 Console.Write('testNest({1, {''Zero'', 1, -3, -5}, 5})');
314 o2 := TXtruct2Impl.Create;
315 o2.Byte_thing := 1;
316 o2.Struct_thing := o;
317 o2.I32_thing := 5;
318 i2 := client.testNest(o2);
319 i := i2.Struct_thing;
320 Console.WriteLine(' := {' + IntToStr( i2.Byte_thing) + ', {''' +
321 i.String_thing + ''', ' +
322 IntToStr( i.Byte_thing) + ', ' +
323 IntToStr( i.I32_thing) + ', ' +
324 IntToStr( i.I64_thing) + '}, ' +
325 IntToStr( i2.I32_thing) + '}');
326
327
328 mapout := TThriftDictionaryImpl<Integer,Integer>.Create;
329
330 for j := 0 to 4 do
331 begin
332 mapout.AddOrSetValue( j, j - 10);
333 end;
334 Console.Write('testMap({');
335 first := True;
336 for key in mapout.Keys do
337 begin
338 if first then
339 begin
340 first := False;
341 end else
342 begin
343 Console.Write( ', ' );
344 end;
345 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapout[key]));
346 end;
347 Console.Write('})');
348
349 mapin := client.testMap( mapout );
350 Console.Write(' = {');
351 first := True;
352 for key in mapin.Keys do
353 begin
354 if first then
355 begin
356 first := False;
357 end else
358 begin
359 Console.Write( ', ' );
360 end;
361 Console.Write( IntToStr( key) + ' => ' + IntToStr( mapin[key]));
362 end;
363 Console.WriteLine('}');
364
365 setout := THashSetImpl<Integer>.Create;
366 for j := -2 to 2 do
367 begin
368 setout.Add( j );
369 end;
370 Console.Write('testSet({');
371 first := True;
372 for j in setout do
373 begin
374 if first then
375 begin
376 first := False;
377 end else
378 begin
379 Console.Write(', ');
380 end;
381 Console.Write(IntToStr( j));
382 end;
383 Console.Write('})');
384
385 Console.Write(' = {');
386
387 first := True;
388 setin := client.testSet(setout);
389 for j in setin do
390 begin
391 if first then
392 begin
393 first := False;
394 end else
395 begin
396 Console.Write(', ');
397 end;
398 Console.Write(IntToStr( j));
399 end;
400 Console.WriteLine('}');
401
402 Console.Write('testEnum(ONE)');
403 ret := client.testEnum(TNumberz.ONE);
404 Console.WriteLine(' = ' + IntToStr( Integer( ret)));
405
406 Console.Write('testEnum(TWO)');
407 ret := client.testEnum(TNumberz.TWO);
408 Console.WriteLine(' = ' + IntToStr( Integer( ret)));
409
410 Console.Write('testEnum(THREE)');
411 ret := client.testEnum(TNumberz.THREE);
412 Console.WriteLine(' = ' + IntToStr( Integer( ret)));
413
414 Console.Write('testEnum(FIVE)');
415 ret := client.testEnum(TNumberz.FIVE);
416 Console.WriteLine(' = ' + IntToStr( Integer( ret)));
417
418 Console.Write('testEnum(EIGHT)');
419 ret := client.testEnum(TNumberz.EIGHT);
420 Console.WriteLine(' = ' + IntToStr( Integer( ret)));
421
422 Console.Write('testTypedef(309858235082523)');
423 uid := client.testTypedef(309858235082523);
424 Console.WriteLine(' = ' + IntToStr( uid));
425
426 Console.Write('testMapMap(1)');
427 mm := client.testMapMap(1);
428 Console.Write(' = {');
429 for key in mm.Keys do
430 begin
431 Console.Write( IntToStr( key) + ' => {');
432 m2 := mm[key];
433 for k2 in m2.Keys do
434 begin
435 Console.Write( IntToStr( k2) + ' => ' + IntToStr( m2[k2]) + ', ');
436 end;
437 Console.Write('}, ');
438 end;
439 Console.WriteLine('}');
440
441 insane := TInsanityImpl.Create;
442 insane.UserMap := TThriftDictionaryImpl<TNumberz, Int64>.Create;
443 insane.UserMap.AddOrSetValue( TNumberz.FIVE, 5000);
444 truck := TXtructImpl.Create;
445 truck.String_thing := 'Truck';
446 truck.Byte_thing := 8;
447 truck.I32_thing := 8;
448 truck.I64_thing := 8;
449 insane.Xtructs := TThriftListImpl<IXtruct>.Create;
450 insane.Xtructs.Add( truck );
451 Console.Write('testInsanity()');
452 whoa := client.testInsanity( insane );
453 Console.Write(' = {');
454 for key64 in whoa.Keys do
455 begin
456 val := whoa[key64];
457 Console.Write( IntToStr( key64) + ' => {');
458 for k2_2 in val.Keys do
459 begin
460 v2 := val[k2_2];
461 Console.Write( IntToStr( Integer( k2_2)) + ' => {');
462 userMap := v2.UserMap;
463 Console.Write('{');
464 if userMap <> nil then
465 begin
466 for k3 in userMap.Keys do
467 begin
468 Console.Write( IntToStr( Integer( k3)) + ' => ' + IntToStr( userMap[k3]) + ', ');
469 end;
470 end else
471 begin
472 Console.Write('null');
473 end;
474 Console.Write('}, ');
475 xtructs := v2.Xtructs;
476 Console.Write('{');
477
478 if xtructs <> nil then
479 begin
480 for x in xtructs do
481 begin
482 Console.Write('{"' + x.String_thing + '", ' +
483 IntToStr( x.Byte_thing) + ', ' +
484 IntToStr( x.I32_thing) + ', ' +
485 IntToStr( x.I32_thing) + '}, ');
486 end;
487 end else
488 begin
489 Console.Write('null');
490 end;
491 Console.Write('}');
492 Console.Write('}, ');
493 end;
494 Console.Write('}, ');
495 end;
496 Console.WriteLine('}');
497
498 arg0 := 1;
499 arg1 := 2;
500 arg2 := High(Int64);
501
502 multiDict := TThriftDictionaryImpl<SmallInt, string>.Create;
503 multiDict.AddOrSetValue( 1, 'one');
504
505 arg4 := TNumberz.FIVE;
506 arg5 := 5000000;
507 Console.WriteLine('Test Multi(' + IntToStr( arg0) + ',' +
508 IntToStr( arg1) + ',' + IntToStr( arg2) + ',' +
509 multiDict.ToString + ',' + IntToStr( Integer( arg4)) + ',' +
510 IntToStr( arg5) + ')');
511
512 Console.WriteLine('Test Oneway(1)');
513 client.testOneway(1);
514
515 Console.Write('Test Calltime()');
516 StartTick := GetTIckCount;
517
518 for k := 0 to 1000 - 1 do
519 begin
520 client.testVoid();
521 end;
522 Console.WriteLine(' = ' + FloatToStr( (GetTickCount - StartTick) / 1000 ) + ' ms a testVoid() call' );
523
524end;
525
526constructor TClientThread.Create(ATransport: ITransport; ANumIteration: Integer);
527begin
528 inherited Create( True );
529 FNumIteration := ANumIteration;
530 FTransport := ATransport;
531 FConsole := TThreadConsole.Create( Self );
532end;
533
534destructor TClientThread.Destroy;
535begin
536 FConsole.Free;
537 inherited;
538end;
539
540procedure TClientThread.Execute;
541var
542 i : Integer;
543 proc : TThreadProcedure;
544begin
545 for i := 0 to FNumIteration - 1 do
546 begin
547 ClientTest;
548 end;
549
550 proc := procedure
551 begin
552 if FTransport <> nil then
553 begin
554 FTransport.Close;
555 FTransport := nil;
556 end;
557 end;
558
559 Synchronize( proc );
560end;
561
562{ TThreadConsole }
563
564constructor TThreadConsole.Create(AThread: TThread);
565begin
566 FThread := AThread;
567end;
568
569procedure TThreadConsole.Write(const S: string);
570var
571 proc : TThreadProcedure;
572begin
573 proc := procedure
574 begin
575 Console.Write( S );
576 end;
577 TThread.Synchronize( FThread, proc);
578end;
579
580procedure TThreadConsole.WriteLine(const S: string);
581var
582 proc : TThreadProcedure;
583begin
584 proc := procedure
585 begin
586 Console.WriteLine( S );
587 end;
588 TThread.Synchronize( FThread, proc);
589end;
590
591initialization
592begin
593 TTestClient.FNumIteration := 1;
594 TTestClient.FNumThread := 1;
595end;
596
597end.