提供一个:用多线程模拟多用户访问网站情况!
发表时间:2024-02-19 来源:明辉站整理相关软件相关文章人气:
[摘要]当时写这个程序是公司要求有个测试环境测试多用户情况,我匆匆忙忙的用了个上午写完后,一测试能用,就交差了,不过后来在多次使用的时候发现了有时候程序回莫名退出,不过由于时间等原应,就没有在仔细修改了,朋友门如果有新区的话,可以自己修改一下,如果有什么想法也希望和我联系!功能说明:可以输入需要访问的UR...
当时写这个程序是公司要求有个测试环境测试多用户情况,我匆匆忙忙的用了个上午写完后,一测试能用,就交差了,不过后来在多次使用的时候发现了有时候程序回莫名退出,不过由于时间等原应,就没有在仔细修改了,朋友门如果有新区的话,可以自己修改一下,如果有什么想法也希望和我联系!
功能说明:可以输入需要访问的URL(注意需要加"http://"才可以,我当时程序中偷懒了,和想象中的模拟用户数,然后确定就可以了,如果你的输入都正确的话,程序会自动创建你希望的线程数目,然后一直运行,知道你电极了“停止”按扭,线程才会结束。在执行期间,线程会将访问一个网页所用到的时间,显示到文本框中,方便用户统计。
不多说了,看程序吧
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;//线程类
using System.Collections ;
using System.Net;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Drawing.Text;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox url;
private System.Windows.Forms.TextBox usernum;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;
private static stringserver;//测试页面地址
private static int intusernum;//虚拟测试人数
private System.Windows.Forms.Timer timer1;
private static bool isend ;//定义是否中断程序(线程)运行
public frmMain()
{
server="";
intusernum=0;
isend=true;
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.usernum = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.url = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// usernum
//
this.usernum.Location = new System.Drawing.Point(304, 320);
this.usernum.Name = "usernum";
this.usernum.Size = new System.Drawing.Size(48, 21);
this.usernum.TabIndex = 5;
this.usernum.Text = "";
//
// toolBarButton1
//
//this.toolBarButton1.ImageIndex = 0;
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(8, 40);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(496, 272);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// url
//
this.url.Location = new System.Drawing.Point(48, 320);
this.url.Name = "url";
this.url.Size = new System.Drawing.Size(168, 21);
this.url.TabIndex = 2;
this.url.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 328);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 16);
this.label1.TabIndex = 3;
this.label1.Text = "URL:";
this.timer1.Interval = 1000;
//
// label2
//
this.label2.Location = new System.Drawing.Point(232, 328);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 16);
this.label2.TabIndex = 4;
this.label2.Text = "虚拟人数:";
//
// button1
//
this.button1.Location = new System.Drawing.Point(368, 320);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 24);
this.button1.TabIndex = 6;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(440, 319);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 25);
this.button2.TabIndex = 7;
this.button2.Text = "中断";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// frmMain
//
this.AcceptButton = this.button1;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
//this.toolBar1,
this.button2,
this.button1,
this.usernum,
this.label2,
this.label1,
this.url,
this.richTextBox1});
this.MaximizeBox = false;
this.Name = "frmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "站点测试";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
frmMain mainForm = new frmMain();
mainForm.Menu=CreateMainMenu();
mainForm.BackColor = Color.DarkGoldenrod;
Application.Run(mainForm);
}
catch(Exception e)
{
MessageBox.Show("Error:" + e, "警告");
return;
}
}
//创建工具条
public static MainMenu CreateMainMenu()
{
MainMenu my= new MainMenu();
MenuItem menuItem1=new MenuItem();
MenuItem menuItem2=new MenuItem();
MenuItem menuItem3=new MenuItem();
MenuItem menuItem4=new MenuItem();
menuItem1.Text = "设置";
menuItem2.Text = "帮助";
menuItem3.Text = "帮助";
menuItem4.Text = "版权";
menuItem2.MenuItems.Add(menuItem3);
menuItem2.MenuItems.Add(menuItem4);
EventHandler handle=new EventHandler(SelectMenuItem) ;
menuItem1.Click +=handle;
menuItem2.Click +=handle;
menuItem3.Click +=handle;
menuItem4.Click +=handle;
my.MenuItems.Add (menuItem1);
my.MenuItems.Add(menuItem2);
return(my);
}
//点击工具条按纽后执行功能
public static void SelectMenuItem(object sender, EventArgs e)
{
MessageBox.Show(e.ToString() );
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.button1.Enabled=true;
this.button2.Enabled=false;
}
private void button1_Click(object sender, System.EventArgs e)
{
isend=false;
this.richTextBox1.Text="";
this.button1.Enabled=false;
this.button2.Enabled=true;
if (this.url.Text .Equals(""))
{
MessageBox.Show("请输入您要测试的URL地址!", "警告");
this.url.Focus();
isend =true;
this.button1.Enabled=true;
this.button2.Enabled=false;
return;
}
else
{
server=url.Text;
}
try
{
if (this.usernum.Text.Equals(""))
{
MessageBox.Show("请输入您希望的虚拟测试人数!", "警告");
isend =true;
this.button1.Enabled=true;
this.button2.Enabled=false;
this.usernum.Focus();
return;
}
else
{
intusernum=int.Parse(this.usernum.Text );
}
}
catch(Exception )
{
MessageBox.Show("您输入的测试人数不合法,请重新输入!", "警告");
this.usernum.Text="";
isend =true;
this.button1.Enabled=true;
this.button2.Enabled=false;
this.usernum.Focus();
return;
}
//开始启动线程
MainStart();
}
private void button2_Click(object sender, System.EventArgs e)
{
isend =true;
this.button1.Enabled=true;
this.button2.Enabled=false;
}
/////////////////////////////////////////////////////////////////////////////////////////
//下面为线程部分
/////////////////////////////////////////////////////////////////////////////////////////
public void MainStart()
{
if(server.Equals("") intusernum==0)
{
return;
}
//开始创建子线程
for(int i=0;i<intusernum;i++)
{
Thread Users=null;
try
{
Users = new Thread(new ThreadStart(this.ceshi) );
Users.Priority=ThreadPriority.Normal;
Users.Start();
}
catch(Exception e)
{
this.richTextBox1.Text +="发生错误:" + e + "\r\n";
isend=true;
this.button1.Enabled=true;
this.button2.Enabled=false;
return;
}
}
}
//子线程:检测得到页面时间
public void ceshi()
{
//MessageBox.Show("线程开始", "警告");
while(!isend)
{
Random rdm1 = new Random(unchecked((int)DateTime.Now.Ticks));
string tname=rdm1.Next().ToString();
try
{
DateTime temptime = new DateTime();
temptime=DateTime.Now;
WebRequest myRequest = WebRequest.Create(server);//出事化WEB页面
WebResponse myResponse = myRequest.GetResponse();//返回RESPONSE对象
Stream stream=myResponse.GetResponseStream();
StreamReader sr =new StreamReader(stream);
string retstr="";
string tmp="";
while ((tmp=sr.ReadLine())!=null)
{
retstr += tmp;
}
myResponse.Close();//管理RESPONSE对象
this.richTextBox1.Text += "线程" + tname + "执行时间为:" + DateTime.Now.Subtract(temptime) + "豪秒\r\n\r\n";
}
catch(Exception)
{
this.richTextBox1.Text+="错误:您输入的URL地址不正确,或该页面不存在!" + "\r\n\r\n";
break;
}
}
}
}
}