您好,登錄后才能下訂單哦!
場景
目前通過Thrift框架生成了C#代碼,由于沒有怎么接觸過這門語言,因此直接啟動程序模擬多個用戶進行測試。在無法修改代碼進行負載性測試的情況下,可以使用system函數啟動程序,模擬多個用戶的實際操作。
#include <windows.h>
int main()
{
while (1)
{
::system("E:/thriftserver/thrift-csharp/ThriftTest/bin/Debug/ThriftTest.exe");
::Sleep(1000);
}
return 0;
}
C#的實際代碼如下
static void ProcessThread(TProtocol protocol)
{
TProcessor processor = new PushMessageService.Processor(new CRecvMessageSErviceImpl());
while (true)
{
try
{
while (processor.Process(protocol, protocol)) { };
return;
}
catch (System.NullReferenceException tt)
{
return;
}
catch (Thrift.Transport.TTransportException tt)
{
//服務器沒有啟動,會拋出此異常
return;
}
}
}
static void MonitorThread(TTransport trans, Action<string> callback)
{
while (true)
{
try
{
if (!trans.Peek())
{
callback("connect close\n");
}
Thread.Sleep(200);
}
catch (Thrift.TApplicationException ex)
{
callback(ex.Message);
return;
}
}
}
static void Main(string[] args)
{
TTransport transport = new TBufferedTransport(new TSocket("192.168.1.110", 7001));
TProtocol protocol = new TBinaryProtocol(transport);
UploadMessageService.Client client = new UploadMessageService.Client(protocol);
Action<TProtocol> processAction = new Action<TProtocol>(ProcessThread);
Action<TTransport, Action<string>> monitorAction = new Action<TTransport, Action<string>>(MonitorThread);
try
{
transport.Open();
processAction.BeginInvoke(protocol, (result) =>
{
processAction.EndInvoke(result);
}, null);
monitorAction.BeginInvoke(transport, (msg) =>
{
Console.WriteLine("connect stop", msg);
}, (result) =>
{
}, null);
info.StrUserId = "admin";
info.StrPassword = "admin";
client.Login(info);
}
}
catch (System.NullReferenceException ttx)
{
//服務器停止運行,會拋出此異常
return;
}
catch (System.IO.IOException ttx)
{
return;
}
catch (Thrift.Transport.TTransportException tt)
{
//服務器沒有啟動,會拋出此異常
return;
}
//transport.Close();
//在這里模擬異常情況,沒有正常關閉
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。