0

我想用HPC做一些模拟,我打算用SOA。我有一些示例材料中的以下代码,我对其进行了修改(我首先添加了这个)。目前我偶然发现了优化/性能不佳的问题。这个基本示例什么都不做,除了查询服务方法,这个方法返回它在参数中获取的值。但是我的例子很慢。我有 60 台具有 4 个核心处理器和 1Gb 网络的计算机。发送消息的第一阶段大约需要 2 秒,然后我必须再等待 7 秒才能返回值。所有的价值都在同一时间或更多。我遇到的另一个问题是我不能重用会话对象,这就是为什么这首先是在外部使用我想把它放在里面使用,但是我得到了超时,或者 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 的第二个版本效果更好,但我对 Session 重用有疑问:

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 的第二次运行期间出现异常:服务器没有提供有意义的回复;这可能是由于合同不匹配、会话过早关闭或内部服务器错误造成的。

4

1 回答 1

4
  1. 不要DurableSession用于单个请求短于大约 30 秒的计算。ADurableSession将由代理中的 MSMQ 队列支持。您的请求和响应可能会往返于磁盘;如果每个请求的计算量很小,这将导致性能问题。你应该Session改用。
  2. 一般来说,出于性能原因,DurableSession除非您绝对需要代理中的持久行为,否则不要使用。在这种情况下,由于您是GetResponses在 之后立即调用的SendRequestsSession因此对您来说效果很好。
  3. 你可以重用一个SessionorDurableSession对象来创建任意数量的BrokerClient对象,只要你没有调用Session.Close.
  4. 如果在客户端并行处理响应很重要,请使用BrokerClient.SetResponseHandler设置一个回调函数来异步处理响应(而不是使用client.GetResponses同步处理它们)。查看HelloWorldR2示例代码了解详细信息。
于 2011-08-01T20:08:08.993 回答