您的位置:首页 > 其它

微型玩具长连接简单测试工具

2010-02-25 22:08 344 查看
本文章为本人个人博客相应文章的镜像:

原文地址: 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
private const string ServiceUrl = "http://192.168.0.104:8000/msg/get";

// The supposed response
private const string SupposedResponse = "Hello, world";

// Concurrent connection count
private const int RequestCount = 10000;

// Global lock
private static readonly object Lock = new object();

// The number of requests in progress
private static int _requesting = 0;

// The number of successful requests
private static int _succeed = 0;

// The number of failed requests
private static int _failed = 0;

static void Main(string[] args)
{
// prelimit the number of connections per domain limitation (the original value is 2,
// which specified in the HTTP 1.1 specification)
ServicePointManager.DefaultConnectionLimit = 30000;

// create request with asynchronize network io api
for(var i = 0; i < RequestCount; i++)
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += WebClientDownloadStringCompleted;
webClient.DownloadStringAsync(new Uri(ServiceUrl));

lock(Lock)
{
_requesting++;
}
}

Action writeStatus = () => Console.Write("\rRequesting: {0,8}, Succeed: {1,8}, Failed: {2,8}", _requesting, _succeed, _failed);

// loop while some of the requests are still in progress
while(_requesting > 0)
{
writeStatus();
Thread.Sleep(100);
}

// finished write the final result
writeStatus();
}

static void WebClientDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
lock (Lock)
{
_requesting--;
if (e.Error != null || e.Result != SupposedResponse)
_failed++;
else
_succeed++;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: