全面的
运行的两个测试(C# HttpListener 服务文件和 smb 文件复制测试)包含太多变量,无法得出关于 HttpListener 与本机代码性能的任何有用结论。
在这种情况下,所有其他代码都应该被怀疑会导致性能问题,并且应该从测试用例中删除。
不幸的是,提供文件的问题的实现不是最佳的,因为它将文件中的一个块读取到托管字节数组中,然后阻塞调用将该块写入内核。它将文件的字节复制到托管数组中并从托管数组中返回(在此过程中不添加任何值)。使用 .Net 4.5,您可以在文件流和输出流之间调用 CopyToAsync,这样您就无需考虑如何并行执行此操作。
结论
下面的测试表明,HttpListener 发送回字节的速度与 IIS Express 8.0 返回文件的速度一样快。对于这篇文章,我在一个普通的 802.11n 网络上使用虚拟机上的服务器对其进行了测试,并且使用 HttpListener 和 IIS Express 仍然达到了 100+ Mbps。
原始帖子中唯一需要更改的是它如何读取文件以将其转发回客户端。
如果您想通过 HTTP 提供文件,您可能应该使用现有的 Web 服务器来处理事物的 HTTP 端和文件打开/缓存/中继。您会发现很难击败现有的 Web 服务器,尤其是当您将 gzipping 动态响应带入图片时(在这种情况下,天真的方法会在发送任何响应之前意外地 gzip 整个响应,这会浪费本可用于发送字节)。
更好的测试来隔离 HttpListener 的性能
我创建了一个测试,它返回一个 10 MB 的整数字符串(在启动时生成一次),以允许测试 HttpListener 在预先给定整个块时返回数据的速度(这类似于使用 CopyToAsync 时可以执行的操作)。
测试设置
客户端计算机:MacBook Air 2013 年中,1.7 GHz Core i7 服务器计算机:iMac 2011 年中,3.4 GHz Core i7 - Windows 8.1 托管在 VMWare Fusion 6.0 中,桥接网络:802.11n via Airport Extreme(位于 8 英尺外)下载客户端:Mac OS X 上的 curl
试验结果
IIS Express 8.0 配置为提供 18 MB 的文件,HttpListenerSpeed 程序设置为返回 10 MB 和 100 MB 的响应。测试结果基本相同。
IIS Express 8.0 结果
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8000/TortoiseSVN-1.8.2.24708-x64-svn-1.8.3.msi > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 13.1M 0 0:00:01 0:00:01 --:--:-- 13.1M
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8000/TortoiseSVN-1.8.2.24708-x64-svn-1.8.3.msi > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 13.0M 0 0:00:01 0:00:01 --:--:-- 13.1M
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8000/TortoiseSVN-1.8.2.24708-x64-svn-1.8.3.msi > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 9688k 0 0:00:01 0:00:01 --:--:-- 9737k
HttpListenerSpeed 结果
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8080/garbage > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 12.6M 0 0:00:01 0:00:01 --:--:-- 13.1M
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8080/garbage > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 13.1M 0 0:00:01 0:00:01 --:--:-- 13.1M
Harolds-MacBook-Air:~ huntharo$ curl -L http://iMacWin81.local:8080/garbage > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 18.4M 100 18.4M 0 0 13.2M 0 0:00:01 0:00:01 --:--:-- 13.2M
HttpListenerSpeed 代码
using System;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
namespace HttpListenerSpeed
{
class Program
{
static void Main(string[] args)
{
var listener = new Listener();
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
listener.Shutdown();
}
}
internal class Listener
{
private const int RequestDispatchThreadCount = 4;
private readonly HttpListener _httpListener = new HttpListener();
private readonly Thread[] _requestThreads;
private readonly byte[] _garbage;
internal Listener()
{
_garbage = CreateGarbage();
_httpListener.Prefixes.Add("http://*:8080/");
_httpListener.Start();
_requestThreads = new Thread[RequestDispatchThreadCount];
for (int i = 0; i < _requestThreads.Length; i++)
{
_requestThreads[i] = new Thread(RequestDispatchThread);
_requestThreads[i].Start();
}
}
private static byte[] CreateGarbage()
{
int[] numbers = new int[2150000];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = 1000000 + i;
}
Shuffle(numbers);
return System.Text.Encoding.UTF8.GetBytes(string.Join<int>(", ", numbers));
}
private static void Shuffle<T>(T[] array)
{
Random random = new Random();
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}
private void RequestDispatchThread()
{
while (_httpListener.IsListening)
{
string url = string.Empty;
try
{
// Yeah, this blocks, but that's the whole point of this thread
// Note: the number of threads that are dispatching requets in no way limits the number of "open" requests that we can have
var context = _httpListener.GetContext();
// For this demo we only support GET
if (context.Request.HttpMethod != "GET")
{
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.Close();
}
// Don't care what the URL is... you're getting a bunch of garbage, and you better like it!
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentLength64 = _garbage.Length;
context.Response.OutputStream.BeginWrite(_garbage, 0, _garbage.Length, result =>
{
context.Response.OutputStream.EndWrite(result);
context.Response.Close();
}, context);
}
catch (System.Net.HttpListenerException e)
{
// Bail out - this happens on shutdown
return;
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception: {0}", e.Message);
}
}
}
internal void Shutdown()
{
if (!_httpListener.IsListening)
{
return;
}
// Stop the listener
_httpListener.Stop();
// Wait for all the request threads to stop
for (int i = 0; i < _requestThreads.Length; i++)
{
var thread = _requestThreads[i];
if (thread != null) thread.Join();
_requestThreads[i] = null;
}
}
}
}