2017-09-12 3 views
0

멀티 스레드 웹 요청을 사용하고 싶지만 예상 한 성능을 얻지 못하고 있습니다.멀티 스레드 웹 요청 성능 문제

저는 1Gbps의 업로드와 1Gbps의 다운로드로 4 개의 코어가있는 컴퓨터에서 실행됩니다.

테스트를 위해 Google 기본 페이지를 다운로드 중입니다.

Google 페이지의 데이터 크기는 ~ 50KB입니다.

나는 동시에 1,000 요청을 보내고 난 1 ~ 2 초 이내에 모든 요청을 완료를 제외하고는 있지만, 완료하는 데 20 초 이상 걸리는

내 코드는 다음과 같습니다

 bool success = ThreadPool.SetMinThreads(workerThreads: 2000, completionPortThreads: 1000); 
     success = ThreadPool.SetMaxThreads(2000, 2000); 
     DateTime dt = DateTime.UtcNow; 
     Parallel.For(0, 1000, (num) => 
     { 
      string url = "https://www.google.co.il/?gfe_rd=cr&dcr=0&ei=OZy3WcmoMY7b8Affj4F4&gws_rd=ssl"; 
      using (WebClient web = new WebClient()) 
      { 
       byte[] bytes = web.DownloadData(url); 
      } 
     } 
     ); 
     double sec = (DateTime.UtcNow - dt).TotalSeconds; 
     Console.WriteLine(sec); 
+2

또한 https://stackoverflow.com/questions/1361771/max-number-of-concurrent-httpwebrequests –

+0

이 Parallel.For' 하나만 사용 '주의 코어 당 스레드 (귀하의 경우 4 개의 스레드). 이는 I/O 바운드 작업에 사용할 올바른 도구가 아닙니다. –

+0

또한 ServicePiontManager는 기본적으로 모든 호스트에 대해 2 개의 동시 요청을 제한합니다. 기본 연결 제한을 변경하거나 app.config 파일에 connectionManagement 섹션을 추가해야합니다. –

답변

0

나는 생각 귀하의 경우에 더 나은 선택은 명시 적 비동기 작업과 함께 HttpClient을 사용하고 창에서 스레드 풀 자체를 관리하게합니다.

Stopwatch sw = new Stopwatch(); 
sw.Start(); 

for (int i = 0; i < 1000; i++) 
{ 
    var httpClient = new HttpClient(); 
    results[i] = httpClient.GetByteArrayAsync(@"https://www.google.co.il/?gfe_rd=cr&dcr=0&ei=OZy3WcmoMY7b8Affj4F4&gws_rd=ssl"); 
} 

var status = Task.WhenAll(results); //WhenAny if you can process results independently 
var pages = status.Result; 
sw.Stop(); 
double sec = sw.Elapsed.TotalSeconds; 
Console.WriteLine(sec); 

사이드 참고 : time measurement에 대한 Stopwatch 사용을 고려하십시오.

-1

솔루션이다 :

 System.Net.ServicePointManager.DefaultConnectionLimit = int.MaxValue; 
+4

이 답변은 혼란스럽고 문맥이 부족합니다. 설명을 추가해야합니다. –