C/S架构快速开发框架SQL并发测试压力测试 | www.csframework.com
作者:作者不详  发布日期:2020-04-18 21:27:00
  C/S架构快速开发框架SQL并发测试压力测试 | www.csframework.com

C/S架构快速开发框架SQL并发测试压力测试 | www.csframework.com


文章介绍

C/S框架旗舰版V5.0用户要求,写了个C/S架构快速开发框架SQL并发测试压力测试工具,
针对CSFramework.DB数据底层进行压力测试,模拟1000~10000个SQL连接数进行测试,成功通过。


测试工具支持:
1. 本地数据库
2. 阿里云ECS服务器的数据
3. SQL连接池Polling


贴图图片-CS架构快速开发框架SQL并发测试压力测试1


贴图图片-CS架构快速开发框架SQL并发测试压力测试2


贴图图片-CS架构快速开发框架SQL并发测试压力测试3


贴图图片-CS架构快速开发框架SQL并发测试压力测试4



C# Code:

/// <summary>
/// C/S框架网,CSFramework.DB,C/S框架数据库并发测试
/// </summary>
public partial class frmSQLConcurrentTester : Form
{
  
  
static object _threadCount = 0;//线程数量
  
static object _connCount = 0;//已建立的SQL连接数
  
static List<object> _threadList = new List<object>();//线程数量
  
static DateTime _startTime = DateTime.Now;
  
  
public frmSQLConcurrentTester()
  {
    InitializeComponent();
  }
  
  
private void btnTest_Click(object sender, EventArgs e)
  {
    txtResult.Text
= "";
    
    OnResult(
"开始测试...");
    btnTest.Enabled
= false;
    
    _connCount
= 0;
    _threadCount
= 0;
    
    _startTime
= DateTime.Now;
    
    
//初始化进度条
    
this.DoPrepareProgress();
    
    
//启动多线程
    
int t = int.Parse(txtThreads.Text);
    
int count = 1;
    
while (count <= t)
    {
      Task.Run(()
=> { DoThread(); });
      Application.DoEvents();
      count++;
    }
    
    
//等待全部线程完成处理
    
Thread.Sleep(500);
    WaitThreads();
    
    OnResult(
"测试结束...");
    
    TimeSpan ts
= DateTime.Now - _startTime;
    lblTime.Text
= "用时:" + ts.TotalSeconds.ToString() + "秒.";
    
    btnTest.Enabled
= true;
    btnTest.Invalidate();
    
    GC.Collect();
//回收垃圾
    
}
    
    
/// <summary>
    
/// 等待线程
    
/// </summary>
    
private void WaitThreads()
    {
      
while (_threadList.Count > 0)
      {
        
//处理线程....
        
Application.DoEvents();
      }
    }
    
    
/// <summary>
    
/// 启动一个线程
    
/// </summary>
    
private void DoThread()
    {
      
lock (_threadCount)
      {
        _threadCount
= int.Parse(_threadCount.ToString()) + 1;
      }
      
      
int currentID = int.Parse(_threadCount.ToString());
      
if (currentID > int.Parse(txtThreads.Text)) return;
      
      _threadList.Add(currentID);
      
      
this.Invoke(new SyncCall(this.OnResult), new object[] { "开始线程:" + currentID.ToString() });
      
      
string connStr = "";
      
      
//本地连接
      
if (rbConnLocal.Checked)
      connStr
= "Server =.; Database = CSFrameworkV5_System; User ID = sa; Password = test;";
      
else//远程连接
      
connStr = "Server =120.77.22.34; Database = CSFrameworkV5_System; User ID = sa; Password = test;";
      
      
//启动线程池
      
if (chkPooling.Checked)
      connStr
= connStr + "pooling = true; connection lifetime = 0; min pool size = 1; max pool size = 32767";
      
else
      connStr
= connStr + "pooling = false; ";
      
      
string SQL = "INSERT INTO tb_QO(QONO,DocDate,CustomerCode,RefPONO,Currency,CreationDate,CreatedBy,LastUpdateDate,LastUpdatedBy)" +
      
"VALUES(@QONO, @DocDate, @CustomerCode, @RefPONO, @Currency, @CreationDate, @CreatedBy, @LastUpdateDate, @LastUpdatedBy)";
      
      
int threadProcess = 0;
      
      
      
while (int.Parse(_connCount.ToString()) < int.Parse(txtConns.Text))
      {
        IDatabase db
= DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, connStr);
        
        CommandHelper cmd
= db.CreateCommand(SQL);
        cmd.AddParam(
"@QONO", GetDocNo());
        cmd.AddParam(
"@DocDate", DateTime.Now);
        cmd.AddParam(
"@CustomerCode", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
        cmd.AddParam(
"@RefPONO", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
        cmd.AddParam(
"@Currency", "RMB");
        cmd.AddParam(
"@CreatedBy", "admin");
        cmd.AddParam(
"@LastUpdatedBy", "admin");
        cmd.AddParam(
"@LastUpdateDate", DateTime.Now);
        cmd.AddParam(
"@CreationDate", DateTime.Now);
        
        threadProcess +
= db.ExecuteCommand(cmd.Command);
        db
= null;
        
        
this.Invoke(new SyncCall(this.OnProgress), new object[] { 1 });
        
        
//锁定连接数计时器
        
lock (_connCount)
        {
          _connCount
= int.Parse(_connCount.ToString()) + 1;
        }
      }
      
      
this.Invoke(new SyncCall(this.OnResult), new object[] { "线程<" + currentID.ToString() + ">结束,共处理:" + threadProcess.ToString() +"条记录!"});
      
      _threadList.Remove(currentID);
    }
    
    
/// <summary>
    
/// 初始化进度条
    
/// </summary>
    
private void DoPrepareProgress()
    {
      lblProgress.Text
= "0/0";
      pb.Minimum
= 0;
      pb.Maximum
= int.Parse(txtConns.Text);
      pb.Value
= 0;
    }
    
    
/// <summary>
    
/// 获取单据号码
    
/// </summary>
    
/// <returns></returns>
    
private string GetDocNo()
    {
      Thread.Sleep(
5);
      
return DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString().ToLower().Replace("-", "").Substring(0, 10);
    }
    
    
/// <summary>
    
/// 显示结果信息
    
/// </summary>
    
/// <param name="value"></param>
    
private void OnResult(object value)
    {
      
string text = value.ToString();
      txtResult.AppendText(text +
"\r\n");
      txtResult.ScrollToCaret();
    }
    
    
/// <summary>
    
/// 更新进度条
    
/// </summary>
    
/// <param name="value"></param>
    
private void OnProgress(object value)
    {
      
if (pb.Value + 1 <= pb.Maximum)
      {
        pb.Value++;
        
        lblProgress.Text
= pb.Value.ToString() + "/" + pb.Maximum.ToString();
        
        TimeSpan ts
= DateTime.Now - _startTime;
        lblTime.Text
= "用时:" + ts.TotalSeconds.ToString() + "秒.";
      }
      
else
      {
        
      }
    }
    
  }
  
  
  
//来源:C/S框架网 | www.csframework.com | QQ:23404761



C# Code:

/// <summary>
/// 异步调用过程
/// </summary>
/// <param name="value"></param>
public delegate void SyncCall(object value);

//来源:C/S框架网 | www.csframework.com | QQ:23404761




SQL Code:


CREATE TABLE [dbo].[tb_QO](
  
[isid] [int] IDENTITY(1,1) NOT NULL,
  
[QONO] [varchar](32) NOT NULL,
  
[DocDate] [datetime] NULL,
  
[CustomerCode] [varchar](20) NULL,
  
[RefPONO] [varchar](50) NULL,
  
[Currency] [varchar](20) NULL,
  
[PaymentType] [varchar](20) NULL,
  
[Sales] [varchar](20) NULL,
  
[Client] [nvarchar](20) NULL,
  
[Remark] [nvarchar](200) NULL,
  
[FlagApp] [char](1) NULL,
  
[AppDate] [datetime] NULL,
  
[AppUser] [varchar](20) NULL,
  
[CreationDate] [datetime] NULL,
  
[CreatedBy] [varchar](50) NULL,
  
[LastUpdateDate] [datetime] NULL,
  
[LastUpdatedBy] [varchar](50) NULL,
  
CONSTRAINT [PK_tb_QO] PRIMARY KEY CLUSTERED
  (
    
[QONO] ASC
    )
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    )
ON [PRIMARY]
    
    
    
//来源:C/S框架网 | www.csframework.com | QQ:23404761






C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务


上一篇 下一篇