这是服务端线程的模型,数据包前4位记录包长度。
我怎么来给它作压力测试呢?
unit mySocketRW;
interface
uses
Classes, Sysutils, ScktComp, windows;
type
TDFServerThread = class(TServerClientThread)
private
FSocketStream : TWinSocketStream;
function WriteClient( strValue: String ): Boolean;
function ReadClient( var strValue: String; Count: integer): Boolean;
function DoTestClient: Boolean;
public
constructor Create( Suspend: Boolean; ASocket: TServerClientWinSocket );
procedure ClientExecute; override;
end;
implementation
uses Main;
procedure TDFServerThread.ClientExecute;
begin
FSocketStream := TWinSocketStream.Create( ClientSocket, 60000 );
try
while DoTestClient() do;
finally
FSocketStream.Free;
end;
end;
constructor TDFServerThread.Create(Suspend: Boolean;
ASocket: TServerClientWinSocket);
begin
inherited Create( Suspend, ASocket );
end;
function TDFServerThread.DoTestClient: Boolean;
var
i, iLen : integer;
sIn, sOut: string;
begin
result := false;
if ReadClient(sIn, 4) then
begin
iLen := strtointdef(trim(sIn),0);
if iLen < 0 then
begin
WriteLog('接收字符串长度错');
result:= false;
exit;
end;
if ReadClient(sIn, iLen) then
begin
sOut:= 'Handle:' + inttostr(self.Handle) + ' Now:'+ datetimetostr(now);
i := length(sOut);
sOut := copy(inttostr(10000+i),2,4) + sOut;
WriteClient(sOut);
WriteLog('接收=['+sIn+']'+' | '+'回应=['+sOut+']');
end
else
WriteLog('不能接收到长度为'+inttostr(iLen)+'的字符串');
end;
end;
function TDFServerThread.ReadClient(var strValue: String; Count: integer): Boolean;
var
buffer: array[0..254] of char;
nCount, nCut: integer;
begin
Assert(FSocketStream <> nil);
nCut:= Count;
try
while (not Terminated) and ClientSocket.Connected and (nCut>0) do
if FSocketStream.WaitForData( 2000 ) then
begin
nCount := FSocketStream.Read(buffer, Count);
if nCount > 0 then
begin
buffer[nCount] := #0;
strValue := strValue + pchar(string(buffer));
Dec(nCut, nCount);
end
else
break;
end
else
break;
except
Terminate;
end;
result := nCut = 0;
end;
function TDFServerThread.WriteClient( strValue: String ): Boolean;
var
buffer: array[0..400] of char;
nCount, nLeft : integer;
begin
Assert(FSocketStream <> nil);
nLeft := length(strValue);
try
repeat
if (Terminated or not ClientSocket.Connected ) then
break;
strmove(@buffer, pchar(strValue) , nLeft);
nCount := FSocketStream.Write( Buffer, nLeft );
Dec(nLeft, nCount);
Delete(strValue, 1, nCount );
until( (strValue='') or (nCount = 0) );
except
Terminate;
end;
result := nLeft = 0;
end;
end.