我正在使用此代码来强调测试我的服务器。我试过webpound X 1000 http://local_dev.com
。
代码似乎工作正常。它< 100行并且不难理解。简而言之,它使用webclient在阻塞和异步模式下点击一次站点(因此旁边的while循环)。然后它反复点击网站计算有多少成功。
问题出现在两台不同的机器上,我在异步模式下获得600-850(使用0作为第一个arg),并在单线程模式下随机获得160-350。
运行此应用时,我的CPU处于空闲状态。根据任务经理的说法,我的webapp从未达到1%,webpound不超过2%,而cpu总数从未超过5%。
问题出在这里?服务器是使用fastcgi的nginx。我怀疑问题是webclient / C#。什么是我可能使用的具有相同想法的另一种工具?或者我如何修改这个,这样我每秒钟可以打多少?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace webpound
{
class Program
{
static long count = 0;
static void Main(string[] args)
{
var s = new Stopwatch();
var wc = new WebClient();
int time = 100, mthread=0;
if (args.Count() == 0)
{
Console.WriteLine("[1 (for single) [ms time]] url");
return;
}
string url = null;
bool useSingle = false;
if (args.Count() == 1)
{
url = args[0];;
}
else if (args.Count() == 2)
{
useSingle = args[0] == "1";
url = args[1];
}
else if (args.Count() == 3)
{
useSingle = args[0] == "1";
time = int.Parse(args[1]);
url = args[2];
}
else
{
throw new Exception();
}
var uri = new Uri(url);
wc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
wc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
//wc.DownloadProgressChanged += DownloadProgressChangedEventHandler;
wc.DownloadString(uri);
wc.DownloadStringAsync(uri);
while (System.Threading.Interlocked.Read(ref count)==0)
{
System.Threading.Thread.Sleep(1);
}
count = 0;
WebException ex1 = null;
s.Start();
try
{
while (s.ElapsedMilliseconds < time)
{
if (useSingle)
{
wc.DownloadString(url);
count++;
}
else
{
var wwc = new WebClient();
wwc.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
wwc.DownloadStringCompleted += DownloadStringCompletedEventHandler;
var url2 = url + mthread.ToString();
wwc.DownloadStringAsync(new Uri(url2));
mthread++;
System.Threading.Thread.Sleep(1);
}
}
}
catch(WebException ex)
{
ex1 = ex;
}
var mycount = count;
s.Stop();
if (ex1 == null)
{
Console.WriteLine("Finished with {0}/{1} in {2}", mycount,mthread, s.ElapsedMilliseconds);
}
else
{
var aa = new StreamReader(ex1.Response.GetResponseStream()).ReadToEnd();
Console.WriteLine(ex1.Message);
Console.WriteLine("Finished with {0}/{1} in {2}", mycount, mthread, s.ElapsedMilliseconds);
}
}
static void DownloadProgressChangedEventHandler(object sender, DownloadProgressChangedEventArgs e)
{
}
static void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e)
{
System.Threading.Interlocked.Increment(ref count);
}
}
}