使用WCF流的高CPU负载 [英] High CPU load using WCF streaming

查看:212
本文介绍了使用WCF流的高CPU负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上进行了大量研究,但是很遗憾,我找不到解决方案。
我的问题是,使用WCF(NetTcpBinding,流式)时,即使在功能强大的计算机上也遇到相当高的CPU负载-更具体地说,在处理20个客户端线程时,我的i5 860的负载为20-40% 。在部署真实服务(而不是测试项目)时,大约有50个真实客户端每秒发送小数据包(每次传输大约20kb),CPU负载已经达到80-90%。最终应该有200多个客户端,但是我无法想象这应该如何在这样的CPU负载下工作...

I've been doing a lot of research on this issue but unfortunately I wasn't able to find a solution. My problem is that I am experiencing a quite high CPU load even on powerful machines when using WCF (NetTcpBinding, Streamed) - to be more specific, my i5 860 has a load of 20-40 percent when handling 20 client threads. When it comes to deploying a real service (and not a testing project) and there's around 50 real clients sending small data packages every second (around 20kb per transfer) the CPU load is already at 80-90 percent. In the end there should be 200+ clients but I can't imagine how this should work with such CPU loads...

出于测试目的,我设置了一个小的使用NetTcpBinding,仅基于WCF流传输的简单客户端和服务器项目。其中已经有很多绝望代码,因为我试图使其工作……为了进行测试,我使用了一个200MB的文件,该文件被发送到WCF服务20次。

For testing purposes I have set up a small project with just a simple client and server based on WCF streamed transfer using NetTcpBinding. There's already a lot of 'desperation code' in it, because I have tried to make it work... for my testing I used a 200MB file that's being sent to the WCF service 20 times.

这里是合同:

[ServiceContract(Namespace = "WCFStreamTest.WCFService")]
public interface IStreamContract
{
    [OperationContract(Name = "ReceiveStream")]
    StreamMessage ReceiveStream(StreamMessage msg);

    [OperationContract(Name = "SendStream")]
    StreamMessage SendStream(StreamMessage msg);
}

此处使用的StreamMessage类只是一个包含字符串标头和流对象。

The StreamMessage class used in here is just a MessageContract containing a string header and a Stream object.

服务器代码如下:

[ServiceBehavior(IncludeExceptionDetailInFaults = false, InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true, MaxItemsInObjectGraph = int.MaxValue)]
public class StreamService : IStreamContract
{
    public StreamMessage ReceiveStream(StreamMessage msg)
    {
        if (File.Exists(msg.Parameters))
            return new StreamMessage() { Parameters = msg.Parameters, DataStream = new System.IO.FileStream(msg.Parameters, System.IO.FileMode.Open, System.IO.FileAccess.Read) };

        return new StreamMessage();
    }

    public StreamMessage SendStream(StreamMessage msg)
    {
        if (msg.Parameters.Trim().Length > 0)
        {
            int bufferSize = 8096 * 4;
            byte[] buffer = new byte[bufferSize];
            int bytes = 0;

            while ((bytes = msg.DataStream.Read(buffer, 0, bufferSize)) > 0)
            {
                byte b = buffer[0];
                b = (byte)(b + 1);
            }
        }

        return new StreamMessage();
    }
}

测试项目仅使用SendStream方法进行测试-

The test project just uses the SendStream method for testing - and that method just reads the data stream and does nothing else.

在这一点上,我想我会节省您的时间,并且不会在其中发布完整的代码这里。也许下载演示项目的链接就足够了? (为使其正常工作,客户端的Program.cs中有一行需要更改的对象:FileInfo fi = new FileInfo(@<<< >>>);)

At this point I think I'll just save your time reading and don't post the full code in here. Maybe a download link to the demo project will be sufficient? (To make it work there is one line in the client's Program.cs that's object to be changed: FileInfo fi = new FileInfo(@"<<<>>>");)

WCFStreamTest项目

对于如何降低CPU使用率的任何想法我都会非常高兴...在此先感谢您的帮助和提示...

I'd be really happy about any idea on how to lower CPU usage... thanks in advance for any help and tips...

推荐答案

您可以尝试让线程在while循环中进入睡眠状态,看看它如何运行。

Can you try to make the thread sleep in while loop, and see how it goes.

while ((bytes = msg.DataStream.Read(buffer, 0, bufferSize)) > 0)
{
    byte b = buffer[0];
    b = (byte)(b + 1);
    Thread.Sleep(100);
}

如果它是视频流服务,则可能需要调整睡眠间隔。

If it is a video streaming service you might have to tweak the sleep interval.

这篇关于使用WCF流的高CPU负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆