我想使用HPC进行一些模拟,我将使用SOA。我有一些样本材料的代码,我修改了它(我先添加了这个)。目前我偶然发现了优化/性能不佳的问题。这个基本样本没有任何期望查询服务方法,这个方法返回它在参数中得到的值。但是我的例子很慢。我有60台拥有4个核心处理器和1Gb网络的计算机。发送消息的第一阶段需要大约2秒钟,然后我必须等待另外7秒才能返回值。所有值同时都是leas或更多。我遇到的另一个问题是我不能重新使用会话对象,这就是为什么第一个是在外面使用我想把它放在里面使用,但后来我得到了时间,或者BrokerClient结束的信息。
我可以重用BrokerClient或DurableSession对象吗?
如何加快邮件传递的整个过程?
static void Main(string[] args)
{
const string headnode = "Head-Node.hpcCluster.edu.edu";
const string serviceName = "EchoService";
const int numRequests = 1000;
SessionStartInfo info = new SessionStartInfo(headnode, serviceName);
for (int j = 0; j < 100; j++)
{
using (DurableSession session = DurableSession.CreateSession(info))
{
Console.WriteLine("done session id = {0}", session.Id);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
using (BrokerClient<IService1> client = new BrokerClient<IService1>(session, binding))
{
for (int i = 0; i < numRequests; i++)
{
EchoRequest request = new EchoRequest("hello world!");
client.SendRequest<EchoRequest>(request, i);
}
client.EndRequests();
foreach (var response in client.GetResponses<EchoResponse>())
{
try
{
string reply = response.Result.EchoResult;
Console.WriteLine("\tReceived response for request {0}: {1}", response.GetUserData<int>(), reply);
}
catch (Exception ex)
{
}
}
}
session.Close();
}
}
}
使用Session而不是DurableSession的第二个版本,它运行得更好,但我遇到会话重用问题:
using (Session session = Session.CreateSession(info))
{
for (int i = 0; i < 100; i++)
{
count = 0;
Console.WriteLine("done session id = {0}", session.Id);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
using (BrokerClient<IService1> client = new BrokerClient<IService1>( session, binding))
{
//set getresponse handler
client.SetResponseHandler<EchoResponse>((item) =>
{
try
{
Console.WriteLine("\tReceived response for request {0}: {1}",
item.GetUserData<int>(), item.Result.EchoResult);
}
catch (SessionException ex)
{
Console.WriteLine("SessionException while getting responses in callback: {0}", ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Exception while getting responses in callback: {0}", ex.Message);
}
if (Interlocked.Increment(ref count) == numRequests)
done.Set();
});
// start to send requests
Console.Write("Sending {0} requests...", numRequests);
for (int j = 0; j < numRequests; j++)
{
EchoRequest request = new EchoRequest("hello world!");
client.SendRequest<EchoRequest>(request, i);
}
client.EndRequests();
Console.WriteLine("done");
Console.WriteLine("Retrieving responses...");
// Main thread block here waiting for the retrieval process
// to complete. As the thread that receives the "numRequests"-th
// responses does a Set() on the event, "done.WaitOne()" will pop
done.WaitOne();
Console.WriteLine("Done retrieving {0} responses", numRequests);
}
}
// Close connections and delete messages stored in the system
session.Close();
}
我在第二次运行EndRequest时遇到异常:服务器没有提供有意义的回复;这可能是由于合同不匹配,过早的会话关闭或内部服务器错误造成的。
答案 0 :(得分:4)
DurableSession
用于单个请求短于约30秒的计算。 DurableSession
将由代理中的MSMQ队列支持。您的请求和响应可以往返磁盘;如果每个请求的计算量很小,这将导致性能问题。您应该使用Session
代替。 DurableSession
。在这种情况下,由于您在GetResponses
之后立即致电SendRequests
,Session
将适合您。Session
或DurableSession
对象来创建任意数量的BrokerClient
个对象,只要您没有调用Session.Close
。BrokerClient.SetResponseHandler
设置一个回调函数,该函数将异步处理响应(而不是使用同步处理它们的client.GetResponses
)。有关详细信息,请查看HelloWorldR2
示例代码。