HttpListenerResponse OutputStream.Write 在我的服务器上非常慢

Tra*_*chs 3 c# linux mono webserver http

我想在我的服务器上用 C# 设置一个非常基本的网络服务器(Linux 版本 2.6.32-5-vserver-amd64 (Debian 2.6.32-48))。
该服务器位于我所在的城市,并且功能开销接近零,例如显示 7通过 Apache2 或 Tomcat 8 的 html 行需要约 15 毫秒(DNS 1 毫秒,连接 4 毫秒,发送 1 毫秒,等待 6 毫秒,接收 4 毫秒)。在
该服务器上运行我的最小 C# Web 服务器时,接收阶段持续 60 毫秒!
我在本地尝试过.net框架和mono中的计算机也是如此,最坏的
情况下总是只需要一毫秒。通过测试一下,我发现如果我不通过 context.Response.OutputStream.Write(...) 发送任何数据,那就是甚至比 Apache2 或 Tomcat 还要快,接收速度下降到 0ms!
发送标头不会使其变慢并且工作正常。Mono
3.0.6 正在服务器上运行,我在 Windows PC 上使用 3.0.6 进行测试。

它沸腾了回到问题:
为什么我的服务器上 OutputStream.Write(...) 如此慢以及如何修复它?

以下是 C# 代码:

internal class Program
{
    private static void Main(string[] args)
    {
        System.Net.HttpListener listener = new System.Net.HttpListener();
        listener.Prefixes.Add("http://domain.tld:7777/");
        //listener.Prefixes.Add("http://localhost:7777/");
        //listener.Prefixes.Add("http://127.0.0.1:7777/");
        listener.Start();

        while (true)
        {
            System.Net.HttpListenerContext context = listener.GetContext();
            System.Threading.Tasks.Task.Factory.StartNew(() => { ProcessRequest(context); });
        }
    }

    private static void ProcessRequest(System.Net.HttpListenerContext context)
    {
        System.Net.HttpListenerResponse response = context.Response;
        response.ContentType = "text/html";
        byte[] bytes = GetBytes("Testtext");
        response.OutputStream.Write(bytes, 0, bytes.Length); // this line makes the request 60ms slower!
        context.Response.Headers.Add("headerName", "This is the headervalue");
        response.Close();
    }

    private static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
}
Run Code Online (Sandbox Code Playgroud)

Tra*_*chs 5

我找到了一个解决方案,或者更确切地说是解决方法:
如果我使用

response.Close(bytes, bool); // doesn't matter whether boolean is true or false
Run Code Online (Sandbox Code Playgroud)

并且根本不写入流,它的速度非常快。对我来说很好,但我还是不明白。