我使用HttpListener作爲簡單的http服務器。我已經通過讓HttpListener爲每個請求返回字符串「OK」來對req/sec性能進行基準測試 - 這已達到每秒16,000個請求的限制。根據以下代碼,我可以做些什麼來進一步優化性能?或者我們是否達到了Windows Http.sys的限制?HttpListener性能優化
圖片上傳不工作,這裏有一個鏈接到Visual Studio性能跟蹤:
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
對不起,你是怎麼得到每秒16K的請求?我用300個並行請求進行了測試,每40秒獲得30K個請求。並且使用1-500個代理對Pylot進行壓力測試,並且每秒獲得約250個請求。請解釋它;)我在同一臺機器上進行的所有測試(本地主機)。我做錯了什麼?謝謝!!! – Edward83
P.S.從一個線程(只有一個並行線程)我提出了16K個請求,並且完成了約54秒;我不明白:))) – Edward83
現在我每2-4秒約13-16K個請求)))據我瞭解,我的第一個測試很慢,因爲增量鎖定,跟蹤文件等;我刪除所有這些「垃圾」:) – Edward83