简体   繁体   English

Owin.Websocket超过消息大小会导致CPU使用率过高

[英]Owin.Websocket exceeding message size causes high CPU usage

I'm using Owin.WebSocket library to receive a stream of images to an Asp.Net WebAPI app. 我正在使用Owin.WebSocket库来接收Asp.Net WebAPI应用程序的图像流。 I have created a websocket using Owin.Websocket.WebSocketConnection to receive messages from my Frontend. 我使用Owin.Websocket.WebSocketConnection创建了一个websocket来接收来自我的前端的消息。

 public class MyWebScoket : WebSocketConnection
 {
    protected MyWebScoket(int maxMessageSize = 65536) : base(maxMessageSize)
    {
       //logic
    }

    public override void OnClose(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
    {
        //logic
    }

    public override Task OnMessageReceived(ArraySegment<byte> message, WebSocketMessageType type)
    {
        //logic
    }

    public override void OnOpen()
    {
        //logic
    }

    public override void OnReceiveError(Exception error)
    {
        //logic
    }
}

When the message size is below the set message size, CPU usage of API is normal and everything is functioning as it should, however when the message size exceeds the limit, CPU usage of API increases dramatically, causing my machine to stop responding even to mouse clicks. 当消息大小低于设置的消息大小时,API的CPU使用率是正常的,并且一切都正常运行,但是当消息大小超过限制时,API的CPU使用率急剧增加,导致我的机器甚至停止响应鼠标点击。

This seems strange, as the message sending rate is always the same. 这看起来很奇怪,因为消息发送速率总是相同的。

What is the reason of such behavior? 这种行为的原因是什么?

I added breakpoints inside all overridden methods, but none of them is reached, I thought if the message limit size is exceeded the execution should go to OnReceiveError. 我在所有重写的方法中添加了断点,但没有达到它们,我想如果超出了消息限制大小,执行应该转到OnReceiveError。

Any workarounds/ideas or alternatives are welcomed. 欢迎任何变通方法/想法或替代方案。

I have struggled with this issue my self when testing, As I do not do it professionally. 在测试时我一直在努力解决这个问题,因为我不专业。 It seems that, if you do not exceed the message size, the owin web socket will not use the CPU usage much but rather depend on the RAM memory, this way, when not exceeding - cpu usage was at 2-4% and at the top, 10% and my ram was between 5-8% and jumps to 13-14% tops. 似乎,如果你没有超过消息大小,owin web套接字将不会大量使用CPU使用率,而是依赖于RAM内存,这样,当不超过 - cpu使用率为2-4%时最高,10%,我的公羊在5-8%之间,并且上升到13-14%。

However, if you exceed the size, owin web sockets develop unexpected behavior, causing the web sockets to use the free CPU instead. 但是,如果超出大小,owin Web套接字会产生意外行为,导致Web套接字使用空闲CPU。

I tried it on my personal PC - i7 -7700k 4 cors 8 threads, each and every one of the threads was 100% used and my memory stayed at about 2-3% with jumps to 10-14% every two minutes or so. 我在我的个人电脑上尝试过它 - i7 -7700k 4个8线程,每个线程100%使用,我的记忆保持在2-3%左右,每两分钟左右跳跃到10-14%。

I do not know the cause for this behavior, only know that for now you should not exceed the message size as it will result in a very unlikely behavior, I am also trying to look if anyone as posted an official issue about it. 我不知道这种行为的原因,只知道你现在不应该超过消息大小,因为它会导致一个非常不可能的行为,我也试图看看是否有人发布了关于它的官方问题。

The one reason I can see (logically) for this behavior is that when the message exceeds the size, owin mechanics 'fear' that the RAM memory will not be sufficient to run the operation and crush and so it's counting on the cpu to handle the preasure but this is only my assumption. 我可以(逻辑上)看到这种行为的一个原因是,当消息超出大小时,owin机制“担心”RAM内存不足以运行操作并且压碎因此它依靠cpu来处理虽然这只是我的假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM