HttpWebResponse Image stream performance degradation

118 views Asked by At

I have a following code.

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 request.Proxy = WebRequest.DefaultWebProxy; //or skipped
 request.UserAgent = "Mozilla";
 request.Referer = "http://google.com;
 request.Accept = "image/png,image/*;q=0.8,*/*;q=0.5";
 request.Host = "google.com";
// request.KeepAlive = false;
 request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
 request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
 request.CookieContainer = cookieContainer;

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 {
     using (Stream stream = response.GetResponseStream())
     {
         Bitmap bmp = new Bitmap(Image.FromStream(stream));

         return bmp;
     }
 }

It is executed in multiple threads. Each thread runs separate loop with above code. I run simple performance tests (Stopwatch) and observed that after 3-5 minutes Image.FromStream(stream) takes 10x more time then at the beginning.

I tried to set KeepAlive and Proxy settings in request object. Also added following in app.config:

<system.net>
    <connectionManagement>
        <add address="*" maxconnection="5000"/>
    </connectionManagement>
    <defaultProxy enabled="false">
        <proxy/>
        <bypasslist/>
        <module/>
    </defaultProxy>
</system.net>

Unfortunately I have still performance degradation over time. My performance tool (dotTrace) shows that the significant part of the time is spend Image.FromStream(stream)

Any ideas where is the bug ?

0

There are 0 answers