[小工具] C#多线程|匿名委托传参数|测试网站压力--升级版

时间:2024-09-29 22:05:38

上次文章链接:http://www.sufeinet.com/thread-11-1-1.html写这些并不是不会用测试工具,也并不是无视测试工具,而是做为一个程序员希望用自己写的东西来完成一些功能,从而更深入的学习一下相关技术

而且这样的小工具,不是测试工具那样繁琐很方便 。

上次文章引言


我们一直在做网站,但在我河南这块,对测试工作,特别是压力测试一般都不怎么在意,都是自己访问一下速度不错就行了,再就是数据库访问速度测试也是同样情况
程序员在写Sql代码时,一般是一个人写完之后,一运行可快完事
其实这些是不够的,我们根本没有进行过多用户多线程的测试,如果是100个,一千个要同时访问,还会有这样的速度吗?
我们自己反思一下是不是有这样的经历呢,我做的网站刚上传服务器,打开很快,调数据库1000条以内一秒用不了,感觉非常好,但过了不几天,就会感觉到网站很慢很慢,于是去检查测试
其实这些可以提前做的,我下面来实现一个多线程测试网站访问速度的功能。

上次文章效果


<ignore_js_op>[小工具] C#多线程|匿名委托传参数|测试网站压力--升级版 说明:

1.一次可以开N多个线程;

2.可以设置要访问的地址;

3.可以设置要循环访问的次数;

相关技术点:

1.C# Winform;

2.httpHelper类;这是我之前自己写的一个类,大家可以参考一下(带证书,无视编码,设置代理等)

3.多线程;

4.线程之间的传参;

5.委托与匿名委托的使用方法;
接下来看看本次的效果吧,
<ignore_js_op>[小工具] C#多线程|匿名委托传参数|测试网站压力--升级版

2.png (15.32 KB, 下载次数: 354)

下载附件

2012-7-1 14:28 上传

其实是应在家的需求在原来的基础之上做了简单的修改,

源代码可以下载,大家自己修改吧

我就修改到这里,希望大家多多指教啊

下面的源代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Data.SqlClient;
namespace AutoFor
{
    public partial class TextFor : Form
    {
        private delegate void UpDateDgvDelegate(string msg, int rowId, string columnName);
        private UpDateDgvDelegate _upDateStateDelegate;
        public TextFor()
        {
            InitializeComponent();
            _upDateStateDelegate = new UpDateDgvDelegate(UpDateDgv);
        }
        /// <summary>
        /// 修改表格的行数据
        /// </summary>
        /// <param name="msg">要修改为的数据</param>
        /// <param name="rowId">行号</param>
        /// <param name="columnName">列名</param>
        private void UpDateDgv(string msg, int rowId, string columnName)
        {
            try
            {
                dgvTextFor.Rows[rowId].Cells[columnName].Value = msg.ToString();
            }
            catch { }
        }
        /// <summary>
        /// 程序执行时间测试
        /// </summary>
        /// <param name="dateBegin">开始时间</param>
        /// <param name="dateEnd">结束时间</param>
        /// <returns>返回(秒)单位,比如: 0.00239秒</returns>
        public static string ExecDateDiff(DateTime dateBegin, DateTime dateEnd)
        {
            TimeSpan ts1 = new TimeSpan(dateBegin.Ticks);
            TimeSpan ts2 = new TimeSpan(dateEnd.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
            return ts3.TotalMilliseconds.ToString();
        }
        /// <summary>
        /// 执行数据
        /// </summary>
        /// <param name="dgvrowid"> 线程号行号</param>
        /// <param name="number">循环总次数</param>
        private void PingTask(int dgvrowid, int number, string url, int time)
        {
            //获取开始时间
            DateTime st = DateTime.Now;
            //开始时间
            this.BeginInvoke(_upDateStateDelegate, st.ToString("hh-mm-ss"), dgvrowid, "开始时间");
            for (int i = 0; i < number; i++)
            {
                //获取线程开始时间
                DateTime stThread = DateTime.Now;
                try
                {
                    HttpHelps hh = new HttpHelps();
                    //自动访问百度,主要是延长时间
                    string strdate = hh.GetHttpRequestStringByNUll_Get(url, null);
                    //当前循环次数
                    this.BeginInvoke(_upDateStateDelegate, strdate, dgvrowid, "数据");
                    //当前循环次数
                    this.BeginInvoke(_upDateStateDelegate, (i+1).ToString(), dgvrowid, "当前循环次数");
                    //获取结束时间
                    DateTime et = DateTime.Now;
                    //结束时间
                    this.BeginInvoke(_upDateStateDelegate, et.ToString("hh-mm-ss"), dgvrowid, "结束时间");
                    //总用时(毫秒)
                    this.BeginInvoke(_upDateStateDelegate, ExecDateDiff(st, et), dgvrowid, "总用时(毫秒)");
                    do
                    {
                        if (stThread.AddSeconds(time) <= DateTime.Now)
                        {
                            break;
                        }
                    } while (true);
                }
                catch { }
            }
        }
        /// <summary>
        /// 创建表格
        /// </summary>
        /// <param name="rows">生成多少行数</param>
        private void CreateTable(int rows)
        {
            DataTable dt_Sale = new DataTable();
            DataColumn dc = null;
            //线程ID
            dc = new DataColumn();
            dc.ColumnName = "线程ID";
            dc.DefaultValue = "1";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            //循环类型
            dc = new DataColumn();
            dc.ColumnName = "循环类型";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            //当前循环次数
            dc = new DataColumn();
            dc.ColumnName = "当前循环次数";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType(" System.String");
            dt_Sale.Columns.Add(dc);
            //开始时间
            dc = new DataColumn();
            dc.ColumnName = "开始时间";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            //结束时间
            dc = new DataColumn();
            dc.ColumnName = "结束时间";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            //总用时(毫秒)
            dc = new DataColumn();
            dc.ColumnName = "总用时(毫秒)";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            //测试数据
            dc = new DataColumn();
            dc.ColumnName = "数据";
            dc.DefaultValue = " ";
            dc.DataType = Type.GetType("System.String");
            dt_Sale.Columns.Add(dc);
            DataRow dr = dt_Sale.NewRow();
            for (int i = 1; i < rows; i++)
            {
                dr["线程ID"] = i.ToString();
                dr["循环类型"] = "For循环";
                dr["当前循环次数"] = "0";
                dr["开始时间"] = "00:00:00";
                dr["结束时间"] = "00:00:00";
                dr["总用时(毫秒)"] = "0";
                dr["数据"] = "";
                dt_Sale.Rows.Add(dr);
                dr = dt_Sale.NewRow();
            }
            dgvTextFor.DataSource = dt_Sale;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int count = Convert.ToInt32(txtCount.Text.Trim());
            int number = Convert.ToInt32(txtNumber.Text.Trim());
            CreateTable(count + 1);
            //开启number个线程
            for (int i = 0; i < count; i++)
            {
                Thread pingTask = new Thread(new ThreadStart(delegate
               {
                   PingTask(i, number, textBox1.Text.Trim(), Convert.ToInt32(txtForTime.Text.Trim()));
               }));
                pingTask.Start();
                Thread.Sleep(Convert.ToInt32(txtKtime.Text.Trim()) * 1000);
            }
        }
    }
}里面所用的HttpHelps类在打包文件里面有。

也可以参考我的文章[url=http://www.sufeinet.com/thread-3-1-1.html]http://www.sufeinet.com/thread-3-1-1.html[/url]