本文章为本人个人博客相应文章的镜像:

原文地址: http://www.greatony.com/index.php/2010/02/25/mini-toy-long-connection-tester/

好吧,我承认我起了个很罗嗦的名字。


事实是这样的,今天研究mochiweb,然后利用mochiweb写了一个长连接web服务器用以实现游戏中的聊天功能。


写到一半的时候,当然要弄个小测试自high一下啦,于是就有了这个“微型玩具长连接简单测试工具”,写的非常简单粗暴,大家就当玩笑看看吧:

 

 1 using System;
 2 using System.Net;
 3 using System.Threading;
 4 
 5 namespace AutoLongConnectionTester
 6 {
 7     class Program
 8     {
 9         // The service url
10         private const string ServiceUrl = "http://192.168.0.104:8000/msg/get";
11 
12         // The supposed response
13         private const string SupposedResponse = "Hello, world";
14 
15         // Concurrent connection count
16         private const int RequestCount = 10000;
17 
18         // Global lock
19         private static readonly object Lock = new object();
20 
21         // The number of requests in progress
22         private static int _requesting = 0;
23 
24         // The number of successful requests
25         private static int _succeed = 0;
26 
27         // The number of failed requests
28         private static int _failed = 0;
29         
30         static void Main(string[] args)
31         {
32             // prelimit the number of connections per domain limitation (the original value is 2,
33             // which specified in the HTTP 1.1 specification)
34             ServicePointManager.DefaultConnectionLimit = 30000;
35 
36             // create request with asynchronize network io api
37             for(var i = 0; i < RequestCount; i++)
38             {
39                 var webClient = new WebClient();
40                 webClient.DownloadStringCompleted += WebClientDownloadStringCompleted;
41                 webClient.DownloadStringAsync(new Uri(ServiceUrl));
42 
43                 lock(Lock)
44                 {
45                     _requesting++;
46                 }
47             }
48 
49             Action writeStatus = () => Console.Write("\rRequesting: {0,8}, Succeed: {1,8}, Failed: {2,8}", _requesting, _succeed, _failed);
50 
51             // loop while some of the requests are still in progress
52             while(_requesting > 0)
53             {
54                 writeStatus();
55                 Thread.Sleep(100);
56             }
57 
58             // finished write the final result
59             writeStatus();
60         }
61 
62         static void WebClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
63         {
64             lock (Lock)
65             {
66                 _requesting--;
67                 if (e.Error != null || e.Result != SupposedResponse)
68                     _failed++;
69                 else
70                     _succeed++;
71             }
72         }
73     }
74 }

 

 

posted on 2010-02-25 22:08  TonyHuang  阅读(766)  评论(0编辑  收藏  举报