2

我有一个简单的 WCF 主机和客户端,它们在 Windows 中运行良好,但在 Mono 2.10.8/9 上性能极差。

WCF 主机在自托管控制台应用程序中运行。WCF 客户端也是一个简单的控制台应用程序。他们使用 netTcpBinding 没有任何安全性。

这是主机 app.config:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehavior">
          <serviceMetadata />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MetadataBehavior" name="EmployeeServiceLib.EmployeeService">
        <clear />
        <endpoint address="net.tcp://localhost:8888/EmployeeService/tcp"
          binding="netTcpBinding" bindingConfiguration="ultra" name="EmployeeService"
          contract="EmployeeServiceLib.IEmployeeService" listenUriMode="Explicit" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="ultra" transactionFlow="false" transferMode="Streamed">
          <reliableSession enabled="false" />
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

客户端 app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="EmployeeService" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
                    transferMode="Streamed" transactionProtocol="OleTransactions"
                    hostNameComparisonMode="StrongWildcard" listenBacklog="10"
                    maxBufferPoolSize="65536" maxBufferSize="65536" maxConnections="10"
                    maxReceivedMessageSize="1048576">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:8888/EmployeeService/tcp"
                binding="netTcpBinding" bindingConfiguration="EmployeeService"
                contract="EmployeeService.IEmployeeService" name="EmployeeService" />
        </client>
    </system.serviceModel>
</configuration>

WCF 服务类:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single,
    AddressFilterMode = AddressFilterMode.Any)]
public class EmployeeService : IEmployeeService
{
    private readonly List<Employee> m_Employees = new List<Employee>();

    #region IEmployeeService Members

    public void AddEmployee(Employee employee)
    {
        m_Employees.Add(employee);
    }

    // ... snipped ...
}

客户所做的是调用 OperationContract 100 次并对调用进行基准测试。

var employee = new Employee { ... };
var client = new EmployeeServiceClient("EmployeeService", "net.tcp://localhost:8888/EmployeeService/tcp");
client.Open();
Console.Write("Adding 100 employees...");
var startTime = Environment.TickCount;
for (int i = 0; i < 100; i++)
{
    client.AddEmployee(employee);
}
var timeTaken = Environment.TickCount - startTime;
Console.WriteLine(" Done.");

基准测试结果:

.NET 4.0 = 62 毫秒

单声道 2.11.1 = 1672 毫秒

在 VM 中运行的 Debian 6 中的 Mono 2.10.9 = 9632 毫秒(CPU 使用率徘徊在 1% 左右)

我可以做些什么来加快 WCF 在 Debian 中 Mono esp 上的性能?

4

0 回答 0