如何获得快速的.Net的HTTP请求 [英] How to get a Fast .Net Http Request

查看:174
本文介绍了如何获得快速的.Net的HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要,我可以在.net这需要在100毫秒使用一个HTTP请求。我能够做到这一点在我的浏览器,所以我真的不明白为什么这是在code这样的问题。

我试过的WinHTTP以及WebRequest.Create和他们两个都超过500毫秒这是不能接受的我的使用情况。

下面是简单的测试,我想传递的例子。 (WinHttpFetcher是一个简单的包装我写的,但它确实一个GET请求,我不知道它的价值粘贴的最简单的例子。)

我收到与LibCurlNet可以接受的结果,但如果有类的同时惯例,我得到一个访问冲突。此外,由于它不是管理code和已被复制到bin目录下,它不是理想的部署与我的开源项目。

另一个实施任何想法来试试呢?

  [测试]
    公共无效WinHttp_Should_Get_Html_Quickly()
    {
        VAR读取器=新WinHttpFetcher();
        VAR的startTime = DateTime.Now;
        VAR的结果= fetcher.Fetch(新的URI(HTTP:// localhost的));
        VAR endTime的= DateTime.Now;
        Assert.Less((endTime的 - 的startTime).TotalMilliseconds,100);
    }
    [测试]
    公共无效WebRequest_Should_Get_Html_Quickly()
    {
        VAR的startTime = DateTime.Now;
        VAR REQ =(HttpWebRequest的)WebRequest.Create(HTTP:// localhost的);
        变种响应= req.GetResponse();
        VAR endTime的= DateTime.Now;
        Assert.Less((endTime的 - 的startTime).TotalMilliseconds,100);
    }
 

解决方案

当标杆,这是最好的,因为他们很可能会影响结果丢弃至少前两个计时:

  • 时间1:由JIT高架为主,即转向字节code到本机code的过程
  • 定时2:一个可能的优化过程为这些JIT过code

在这个时序将反映重复的性能要好得多。

下面是一个测试工具,该工具会自动忽略JIT和优化过程,并取平均值断言演出前运行测试迭代一组数的一个例子。正如你所看到的JIT传花的时间相当量。

  

JIT: 410.79ms

     

优化: 0.98ms

     

平均超过10次迭代: 0.38ms

code:

  [测试]
公共无效WebRequest_Should_Get_Html_Quickly()
{
    私人const int的TestIterations = 10;
    私人const int的MaxMilliseconds = 100;

    动作试验=()=>
    {
       WebRequest.Create(HTTP://localhost/iisstart.htm).GetResponse();
    };

    AssertTimedTest(TestIterations,MaxMilliseconds,测试);
}

私有静态无效AssertTimedTest(INT迭代,诠释maxMs,动作测试)
{
    双JIT =执行(试验); //不顾JIT通
    Console.WriteLine(JIT:{0:F2}毫秒。,JIT);

    双优化=执行(试验); //不顾优化通
    Console.WriteLine(优化:{0:F2}毫秒。,优化);

    双totalElapsed = 0;
    的for(int i = 0; I<迭代;我++)totalElapsed + =执行(试验);

    双averageMs =(totalElapsed /迭代);
    Console.WriteLine(平均:{0:F2}毫秒,averageMs);
    Assert.Less(averageMs,maxMs,经过的平均测试时间。);
}

私有静态双重执行(动作的动作)
{
    秒表计时秒表= Stopwatch.StartNew();
    行动();
    返回stopwatch.Elapsed.TotalMilliseconds;
}
 

I need an Http request that I can use in .Net which takes under 100 ms. I'm able to achieve this in my browser so I really don't see why this is such a problem in code.

I've tried WinHTTP as well as WebRequest.Create and both of them are over 500ms which isn't acceptable for my use case.

Here are examples of the simple test I'm trying to pass. (WinHttpFetcher is a simple wrapper I wrote but it does the most trivial example of a Get Request that I'm not sure it's worth pasting.)

I'm getting acceptable results with LibCurlNet but if there are simultaneous usages of the class I get an access violation. Also since it's not managed code and has to be copied to bin directory, it's not ideal to deploy with my open source project.

Any ideas of another implementation to try?

    [Test]
    public void WinHttp_Should_Get_Html_Quickly()
    {
        var fetcher = new WinHttpFetcher();
        var startTime = DateTime.Now;          
        var result = fetcher.Fetch(new Uri("http://localhost"));
        var endTime = DateTime.Now;
        Assert.Less((endTime - startTime).TotalMilliseconds, 100);
    }
    [Test]
    public void WebRequest_Should_Get_Html_Quickly()
    {
        var startTime = DateTime.Now;
        var req = (HttpWebRequest) WebRequest.Create("http://localhost");
        var response = req.GetResponse();
        var endTime = DateTime.Now;
        Assert.Less((endTime - startTime).TotalMilliseconds, 100);
    }

解决方案

When benchmarking, it is best to discard at least the first two timings as they are likely to skew the results:

  • Timing 1: Dominated by JIT overhead i.e. the process of turning byte code into native code.
  • Timing 2: A possible optimization pass for the JIT'd code.

Timings after this will reflect repeat performance much better.

The following is an example of a test harness that will automatically disregard JIT and optimization passes, and run a test a set number of iterations before taking an average to assert performance. As you can see the JIT pass takes a substantial amount of time.

JIT:410.79ms

Optimize:0.98ms.

Average over 10 iterations:0.38ms

Code:

[Test]
public void WebRequest_Should_Get_Html_Quickly()
{
    private const int TestIterations = 10;
    private const int MaxMilliseconds = 100;

    Action test = () =>
    {
       WebRequest.Create("http://localhost/iisstart.htm").GetResponse();
    };

    AssertTimedTest(TestIterations, MaxMilliseconds, test);
}

private static void AssertTimedTest(int iterations, int maxMs, Action test)
{
    double jit = Execute(test); //disregard jit pass
    Console.WriteLine("JIT:{0:F2}ms.", jit);

    double optimize = Execute(test); //disregard optimize pass
    Console.WriteLine("Optimize:{0:F2}ms.", optimize);

    double totalElapsed = 0;
    for (int i = 0; i < iterations; i++) totalElapsed += Execute(test);

    double averageMs = (totalElapsed / iterations);
    Console.WriteLine("Average:{0:F2}ms.", averageMs);
    Assert.Less(averageMs, maxMs, "Average elapsed test time.");
}

private static double Execute(Action action)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    action();
    return stopwatch.Elapsed.TotalMilliseconds;
}

这篇关于如何获得快速的.Net的HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆