HttpListener性能优化 [英] HttpListener Performance Optimization
问题描述
我将HttpListener用作简单的http服务器.我一直在通过让HttpListener向每个请求返回字符串"OK"来对req/sec性能进行基准测试-这已经达到了每秒16,000个请求的限制.根据以下代码,我可以做些什么来进一步优化性能?还是我们达到了Windows Http.sys的极限?
I'm using HttpListener as a simple http server. I've been benchmarking req/sec performance by having HttpListener return the string "OK" to each request - this has hit a limit at 16,000 requests per second. Based on the following code, is there anything that I can do to further optimize performance? Or are we hitting the limits of Windows Http.sys?
无法上传图片,这是指向Visual Studio性能跟踪的链接:
Image uploading isn't working, here's a link to the Visual Studio performance trace:
Public Class HTTPServer
Shared Listener As HttpListener = New HttpListener
Public Shared Sub Start()
ServicePointManager.DefaultConnectionLimit = 500
ServicePointManager.Expect100Continue = False
ServicePointManager.MaxServicePoints = 500
Listener.Prefixes.Add("http://localhost/")
Listener.Start()
For i As Integer = 1 To (System.Environment.ProcessorCount * 2)
Dim NewThread As New System.Threading.Thread(AddressOf ListenerThread)
NewThread.Priority = ThreadPriority.Normal
NewThread.IsBackground = True
NewThread.Start()
Next
End Sub
Private Shared Sub ListenerThread()
Dim SyncResult As IAsyncResult
While True
SyncResult = Listener.BeginGetContext(New AsyncCallback(AddressOf ListenerCallback), Listener)
SyncResult.AsyncWaitHandle.WaitOne()
End While
End Sub
Private Shared Sub ListenerCallback(ByVal StateObject As IAsyncResult)
Dim Listener As HttpListener = DirectCast(StateObject.AsyncState, HttpListener)
Dim Context As HttpListenerContext = Listener.EndGetContext(StateObject)
Dim Request As HttpListenerRequest = Context.Request
Dim Response As HttpListenerResponse = Context.Response
Dim ResponseString As String = "OK"
Dim Buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(ResponseString)
Response.ContentLength64 = Buffer.Length
Dim OutputStream As System.IO.Stream = Response.OutputStream
OutputStream.Write(Buffer, 0, Buffer.Length)
OutputStream.Close()
OutputStream.Dispose()
End Sub
End Class
推荐答案
一个可以做的事情是只调用一次Encoding.UTF8.GetBytes("OK")
,而不是在每个请求上都调用一次.我怀疑这会带来很大的不同,但是如果您对每个请求执行完全相同的操作,那么您也可以按照需要的形式获得该响应.
Well one thing you can do is only call Encoding.UTF8.GetBytes("OK")
once, instead of on every request. I doubt that it'll make much difference, but if you're doing exactly the same thing on every request, you might as well have that response in the form you need it.
我也将使用Using
语句,而不是显式调用Close和Dispose.这不是性能问题,只是一种通用的好习惯,可以避免在发生异常时未关闭的流出现问题.
I would also use a Using
statement rather than calling Close and Dispose explicitly. That's not a performance thing - just general good practice to avoid problems with unclosed streams when there are exceptions.
实际上,您是否需要达到16K以上的QPS?您是否确定自己是CPU约束还是IO约束?一旦您的程序实际上需要对每个请求做一些实际的工作,我想这将使您在此所做的任何微优化都相形见
Realistically, do you need to hit more than 16K QPS? Have you worked out whether you're being CPU-bound or IO-bound? As soon as your program needs to actually do some real work on each request, I would imagine that will dwarf any micro-optimizations you make here anyway.
这篇关于HttpListener性能优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!