【IT168 技术文章】
前一段时间,为了测试的需要,需要编写一个多线程的程序,就是最大限度的访问某个网页上认证标签,以获得大概该认证标签每秒钟能响应多少次,于是搜索了一些网上的资料,于是编写了下面的这个程序,虽然这个程序不是我们使用的最终版本,但是也反映了测试的思想,大家略微进行改造,就可以用于类似的测试。
using System;
using System.Net;
using System.IO;
using System.Threading;
using System.Timers;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace TestUrl
{
internal class HiPerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(
out long lpPerformanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(
out long lpFrequency);
private long startTime, stopTime;
private long freq;
// Constructor
public HiPerfTimer()
{
startTime = 0;
stopTime = 0;
if (QueryPerformanceFrequency(out freq) == false)
{
// high-performance counter not supported
throw new Win32Exception();
}
}
// Start the timer
public void Start()
{
// lets do the waiting threads there work
Thread.Sleep(0);
QueryPerformanceCounter(out startTime);
}
// Stop the timer
public void Stop()
{
QueryPerformanceCounter(out stopTime);
}
// Returns the duration of the timer (in seconds)
public double Duration
{
get
{
return (double)(stopTime - startTime) / (double)freq;
}
}
}
class Program
{
static long rt = 0;
static long wr = 0;
public static int PostData(string url, string data, out string info)
{
info = "";
CookieContainer cc = new CookieContainer();
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.CookieContainer = cc;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = request.GetRequestStream();
byte[] byteArray = Encoding.UTF8.GetBytes(data);
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Uri responseUri = response.ResponseUri;
Stream receiveStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
string result = readStream.ReadToEnd();
info = result;
return 0;
}
public static bool GetBool(string html)
{
Regex regtable = new Regex(@"http://img.dotad.net/lg.gif", RegexOptions.Singleline);
Match ma = regtable.Match(html);
if (ma.Success)
{
return true;
}
else
return false;
}
public static void ThreadProc()
{
string info = "";
string data = "";
string url = "http://203.86.46.53/lg/index.aspx";
long i=0;
HiPerfTimer pt = new HiPerfTimer(); // create a new PerfTimer object
pt.Start(); // start the timer
while (i++<100)
{
PostData(url, data, out info);
//System.Console.WriteLine(info);
if (GetBool(info) == true)
{
rt++;
}
else
{
wr++;
}
}
pt.Stop();
System.Console.WriteLine("成功" + rt + " 次,失败 " + wr + " 次\n");
Console.WriteLine("用时: {0} sec\n",
pt.Duration); // print the duration of the timed code
}
static void Main(string[] args)
{
Console.WriteLine("Test\n"); // the code to be timed
long j = 0;
while(j++<15)
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
}
}
}