7

我创建了以下简单HttpListener的同时服务多个请求(在 .NET 4.5 上):

class Program {

    static void Main(string[] args) {

        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://+:8088/");
        listener.Start();
        ProcessAsync(listener).ContinueWith(task => { });
        Console.ReadLine();
    }

    static async Task ProcessAsync(HttpListener listener) {

        HttpListenerContext ctx = await listener.GetContextAsync();

        // spin up another listener
        Task.Factory.StartNew(() => ProcessAsync(listener));

        // Simulate long running operation
        Thread.Sleep(1000);

        // Perform
        Perform(ctx);

        await ProcessAsync(listener);
    }

    static void Perform(HttpListenerContext ctx) {

        HttpListenerResponse response = ctx.Response;
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        byte[] buffer = Encoding.UTF8.GetBytes(responseString);

        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);

        // You must close the output stream.
        output.Close();
    }
}

我使用 Apache Benchmark Tool 对此进行负载测试。当我发出 1 个请求时,我得到一个请求的最大等待时间为 1 秒。例如,如果我发出 10 个请求,响应的最大等待时间会上升到 2 秒。

您将如何更改我上面的代码以使其尽可能高效?

编辑

在@JonSkeet 的回答之后,我将代码更改如下。最初,我试图模拟一个阻塞调用,但我想这是核心问题。所以,我接受了@JonSkeet 的建议并将其更改为 Task.Delay(1000)。现在,下面的代码给出了最大值。等待时间约为。10 个并发请求需要 1 秒:

class Program {

    static bool KeepGoing = true;
    static List<Task> OngoingTasks = new List<Task>();

    static void Main(string[] args) {

        HttpListener listener = new HttpListener();
        listener.Prefixes.Add("http://+:8088/");
        listener.Start();
        ProcessAsync(listener).ContinueWith(async task => {

            await Task.WhenAll(OngoingTasks.ToArray());
        });

        var cmd = Console.ReadLine();

        if (cmd.Equals("q", StringComparison.OrdinalIgnoreCase)) {
            KeepGoing = false;
        }

        Console.ReadLine();
    }

    static async Task ProcessAsync(HttpListener listener) {

        while (KeepGoing) {
            HttpListenerContext context = await listener.GetContextAsync();
            HandleRequestAsync(context);

            // TODO: figure out the best way add ongoing tasks to OngoingTasks.
        }
    }

    static async Task HandleRequestAsync(HttpListenerContext context) {

        // Do processing here, possibly affecting KeepGoing to make the 
        // server shut down.

        await Task.Delay(1000);
        Perform(context);
    }

    static void Perform(HttpListenerContext ctx) {

        HttpListenerResponse response = ctx.Response;
        string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
        byte[] buffer = Encoding.UTF8.GetBytes(responseString);

        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);

        // You must close the output stream.
        output.Close();
    }
}
4

1 回答 1

7

在我看来,您最终会得到听众的分歧。在ProcessAsync中,您启动一​​个新任务来监听(通过 Task.Factory.StartNew),然后在方法结束时ProcessAsync 再次调用。那怎么可能结束?目前尚不清楚这是否是您的性能问题的原因,但它看起来肯定是一个普遍的问题。

我建议将您的代码更改为一个简单的循环:

static async Task ProcessAsync(HttpListener listener) {
    while (KeepGoing) {
        var context = await listener.GetContextAsync();
        HandleRequestAsync(context);         
    }
}

static async Task HandleRequestAsync(HttpListenerContext context) {
    // Do processing here, possibly affecting KeepGoing to make the 
    // server shut down.
}

现在,上面的代码目前忽略了HandleRequestAsync. 您可能想要保留“当前运行中”任务的列表,并且当您被要求关闭时,使用它await Task.WhenAll(inFlightTasks)来避免过快地关闭服务器。

另请注意,这Thread.Sleep是一个阻塞延迟。异步延迟将是await Task.Delay(1000).

于 2012-12-04T19:04:35.660 回答