wcf - 简单WCF "Hello world"服务在负载下仅占用一个CPU核心

标签 wcf performance

我有一个基于 basicHttpBinding 的简单“Hello world”服务。该服务托管在四核 CPU 上。

当我运行负载测试时,仅占用一个核心 (95%),其他三个核心大约占 4-8%。

为什么其他核心不用于处理?

设置ConcurrencyMode = ConcurrencyMode.Multiple没有帮助。

Enter image description here

最佳答案

为您的服务配置ServiceBehavior

WCF 默认使用 ConcurrencyMode=ConcurrencyMode.Single。该模式在一个线程中运行对服务的所有请求。

With ConcurrencyMode.Single, WCF does not call again into the object so long as the method is running. After the operation returns the object can be called again.

一个 CPU 核心用于运行该线程。

为您的服务添加以下属性以使用所有 CPU:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]

启用该模式时请注意服务状态。如果更改状态,您可能需要实现自己的锁定。

检查 ConcurrencyMode Enumeration 了解更多详细信息。

还要确保您的客户端同时进行四个调用(在客户端中实现多线程)。如果没有它,即使您的服务器支持多线程,您仍然会进行顺序的单线程调用处理。

检查代码后更新:

您的 WCF 方法不执行任何可以加载 CPU 的工作。请将您的方法替换为一些大量使用 CPU 的函数(计算哈希值或阶乘)并重新检查。

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HelloService : IHelloService
{
   public string HelloWorld()
   {
      return "Hello world";
   }
}

关于wcf - 简单WCF "Hello world"服务在负载下仅占用一个CPU核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924507/

相关文章:

json - 使用多个 JSON 文件对 API 进行压力测试

c - Blit 比条件 + 指针增量更快?

performance - 更高效的算法在 Haskell 中表现更差

c# - DataContractJsonSerializerOperationFormatter 不反序列化使用 JSON.NET

c# - WCF 协议(protocol)异常 : Bad Request 400 (related to http message size)

c# - 将新项目添加到解决方案

c++ - 为什么使用更多线程会导致运行时间变慢?

c# - TLS 致命 : Handshake Failure

c# - 如何在 Windows Universal App 中使用双工 wcf 服务

mysql - 重组数据库以获得最佳性能