delphimvcframework/test/MainU.pas

168 lines
4.2 KiB
ObjectPascal
Raw Normal View History

2009-11-10 11:32:54 +01:00
unit MainU;
interface
procedure Main(serveraddress: string = 'localhost');
procedure MainWithTransaction(serveraddress: string = 'localhost');
procedure Test_Unicode_Chars(serveraddress: string = 'localhost');
implementation
uses
SysUtils,
dateutils,
StompClient,
StompTypes,
2010-04-02 12:44:54 +02:00
Diagnostics;
2009-11-10 11:32:54 +01:00
function NewStomp(Host: string = '127.0.0.1'; Port: Integer = DEFAULT_STOMP_PORT;
ClientID: string = ''): IStompClient;
2009-11-10 11:32:54 +01:00
begin
Result := TStompClient.Create;
2010-04-02 12:44:54 +02:00
Result.SetUserName('guest');
Result.SetPassword('guest');
Result.Connect(Host, Port, ClientID);
2009-11-10 11:32:54 +01:00
end;
procedure Test_Unicode_Chars(serveraddress: string);
var
stomp: IStompClient;
2010-04-02 12:44:54 +02:00
s: IStompFrame;
2009-11-10 11:32:54 +01:00
const
SERBO = '<27>to je Unicode';
SVEDESE = 'Vad <20>r Unicode';
ITALIANO = 'Cos''<27> Unicode';
begin
stomp := NewStomp(serveraddress);
stomp.Subscribe('/topic/unicode');
stomp.Send('/topic/unicode', ITALIANO);
stomp.Send('/topic/unicode', SERBO);
stomp.Send('/topic/unicode', SVEDESE);
s := stomp.Receive;
assert(s <> nil);
assert(s.GetBody = ITALIANO);
s := stomp.Receive;
assert(s <> nil);
assert(s.GetBody = SERBO);
s := stomp.Receive;
assert(s <> nil);
assert(s.GetBody = SVEDESE);
2009-11-10 11:32:54 +01:00
end;
procedure MainWithTransaction(serveraddress: string);
var
stomp, recv: IStompClient;
2010-04-02 12:44:54 +02:00
frame: IStompFrame;
2009-11-10 11:32:54 +01:00
const
TR = 'TRDANIELE';
TOPIC = '/topic/mytopic'; // TOPIC = PUB/SUB, QUEUE = LOAD BALANCER
BODY1 = 'Hello World 1';
BODY2 = 'Hello World 2';
BODY3 = 'Hello World 3';
BODY4 = 'Hello World 4';
begin
stomp := NewStomp;
recv := NewStomp;
stomp.Subscribe(TOPIC);
recv.Subscribe(TOPIC);
stomp.BeginTransaction(TR);
stomp.Send(TOPIC, BODY1, TR);
stomp.Send(TOPIC, BODY2, TR);
stomp.Send(TOPIC, BODY3, TR);
stomp.Send(TOPIC, BODY4, TR);
// NON DEVCE TROVARE NULLA
frame := recv.Receive;
assert(frame = nil);
stomp.CommitTransaction(TR);
frame := recv.Receive;
assert(frame <> nil);
assert(frame.GetBody = BODY1);
frame := recv.Receive;
assert(frame <> nil);
assert(frame.GetBody = BODY2);
frame := recv.Receive;
assert(frame <> nil);
assert(frame.GetBody = BODY3);
frame := recv.Receive;
assert(frame <> nil);
assert(frame.GetBody = BODY4);
frame := recv.Receive;
assert(frame = nil);
2009-11-10 11:32:54 +01:00
end;
procedure Main(serveraddress: string = 'localhost');
var
stomp: TStompClient;
2010-04-02 12:44:54 +02:00
frame: IStompFrame;
2009-11-10 11:32:54 +01:00
i, c: Integer;
msgcount: Cardinal;
sw: TStopWatch;
message_data: string;
const
MSG = 1000;
MSG_SIZE = 1000;
begin
message_data := StringOfChar('X', MSG_SIZE);
WriteLn('TEST MESSAGE (', length(message_data) * sizeof(char), ' bytes):', #13#10, '"',
message_data, '"'#13#10#13#10);
2010-04-02 12:44:54 +02:00
stomp := TStompClient.Create;
2009-11-10 11:32:54 +01:00
try
2010-04-02 12:44:54 +02:00
stomp.SetUserName('Daniele');
stomp.SetPassword('Paperino');
stomp.Connect(serveraddress);
stomp.Subscribe('/topic/foo.bar');
for c := 1 to 10 do
begin
WriteLn;
WriteLn('= STATS LOOP ', c, '=======================================');
for i := 1 to MSG do
begin
stomp.Send('/topic/foo.bar', message_data,
StompUtils.NewHeaders.Add(TStompHeaders.NewPersistentHeader(true)));
2010-04-02 12:44:54 +02:00
// '01234567890123456789012345678901234567890123456789'
if i mod 1000 = 0 then
WriteLn('Queued ', i, ' messages');
end;
2009-11-10 11:32:54 +01:00
2010-04-02 12:44:54 +02:00
msgcount := 0;
sw.start;
while msgcount < MSG do
2009-11-10 11:32:54 +01:00
begin
2010-04-02 12:44:54 +02:00
frame := stomp.Receive;
if assigned(frame) then
2009-11-10 11:32:54 +01:00
begin
2010-04-02 12:44:54 +02:00
inc(msgcount);
frame := nil;
2010-04-02 12:44:54 +02:00
end
2009-11-10 11:32:54 +01:00
end;
2010-04-02 12:44:54 +02:00
sw.Stop;
WriteLn(msgcount, ' in ', sw.ElapsedMilliseconds, ' milliseconds and ', sw.ElapsedTicks,
' ticks');
2010-04-02 12:44:54 +02:00
WriteLn('Throughput: ');
WriteLn(FormatFloat('###,##0.000', sw.ElapsedMilliseconds / msgcount), ' ms/msg');
WriteLn(FormatFloat('###,##0.000', msgcount / sw.ElapsedMilliseconds), ' msg/ms');
WriteLn('= END LOOP ', c, '========================================='#13#10);
2009-11-10 11:32:54 +01:00
end;
2010-04-02 12:44:54 +02:00
stomp.Unsubscribe('/topic/foo.bar');
stomp.Disconnect;
write('test finished...');
2009-11-10 11:32:54 +01:00
finally
2010-04-02 12:44:54 +02:00
stomp.Free;
2009-11-10 11:32:54 +01:00
end;
end;
end.