啊鑫
2024-07-11 afbf8700d137710713db61955879d0f5acb73738
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using CSFramework.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace CSFramework.DBTester
{
    /// <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;
 
        static List<String> _DocNos = new List<string>();//本次测试生成的单据号码
 
        static object _ThreadFinish = 0;
 
        static object _Rows_Inserted = 0;
        static object _Rows_Updated = 0;
        static object _Rows_Deleted = 0;
        static object _Failed = 0;
 
        public frmSQLConcurrentTester()
        {
            InitializeComponent();
        }
 
        private void btnTest_Click(object sender, EventArgs e)
        {
            txtResult.Text = "";
 
            OnResult("开始测试...");
            btnTest.Enabled = false;
 
            _connCount = 0;
            _threadCount = 0;
            _Failed = 0;
            _ThreadFinish = 0;
            _Rows_Inserted = 0;
            _Rows_Updated = 0;
            _Rows_Deleted = 0;
            _startTime = DateTime.Now;
            _DocNos.Clear();
 
            //初始化进度条
            this.DoPrepareProgress();
 
            //启动多线程
            int t = int.Parse(txtThreads.Text);
            int count = 1;
            while (count <= t)
            {
                Task.Run(() => { DoThread(); });
                Application.DoEvents();
                count++;
            }
 
            //等待全部线程完成处理         
            Thread.Sleep(1000); //必须等待第1个线程
            WaitThreads();
 
            OnResult($"本次测试用户数据, Insert={_Rows_Inserted},Update={_Rows_Updated},Delete={_Rows_Deleted},失败数={_Failed}");
 
            OnResult("测试结束...");
 
            TimeSpan ts = DateTime.Now - _startTime;
            lblTime.Text = "用时:" + ts.TotalSeconds.ToString() + "秒.";
 
            btnTest.Enabled = true;
            btnTest.Invalidate();
 
            GC.Collect();//回收垃圾
        }
 
        /// <summary>
        /// 等待线程
        /// </summary>
        private void WaitThreads()
        {
            while (int.Parse(_ThreadFinish.ToString()) < int.Parse(txtThreads.Text))
            {
                //处理线程....
                Application.DoEvents();
            }
        }
 
        /// <summary>
        /// 启动一个线程
        /// </summary>
        private void DoThread()
        {
            try
            {
                //线程任务计数器
                lock (_threadCount)
                {
                    _threadCount = int.Parse(_threadCount.ToString()) + 1;
                }
 
                int currentID = int.Parse(_threadCount.ToString());
                if (currentID > int.Parse(txtThreads.Text)) return;
 
                lock (_threadList) _threadList.Add(currentID);
 
                this.Invoke(new SyncCall(this.OnResult), new object[] { "开始线程:" + currentID.ToString() });
 
                string connStr = "";
 
                //本地连接
                if (rbConnLocal.Checked)
                    connStr = "Server =.; Database = CSFrameworkV5_Normal; User ID = sa; Password = test;";
                else//远程连接
                    connStr = "Server =120.77.22.34; Database = CSFrameworkV5_Normal; 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,Sales,CreationDate,CreatedBy,LastUpdateDate,LastUpdatedBy)" +
                             "VALUES(@QONO, @DocDate, @CustomerCode, @RefPONO, @Currency, @Sales, @CreationDate, @CreatedBy, @LastUpdateDate, @LastUpdatedBy)";
 
                string SQL1 = "UPDATE tb_QO SET Client=@Client,Remark=@Remark,LastUpdateDate=@LastUpdateDate WHERE QONO=@QONO";
 
                string SQL2 = "DELETE tb_QO WHERE QONO=@QONO";
 
                int sub_total = 0;
                int go = 0;
                CommandHelper cmd = null;
 
                while (int.Parse(_connCount.ToString()) < int.Parse(txtConns.Text))
                {
 
                    //锁定连接数计时器+1
                    lock (_connCount)
                    {
                        _connCount = int.Parse(_connCount.ToString()) + 1;
                        if (int.Parse(_connCount.ToString()) > int.Parse(txtConns.Text))
                            break;
                    }
 
                    IDatabase db = DatabaseFactory.CreateDatabase(DatabaseType.SqlServer, connStr);
 
                    //抽签,go<=10,模拟1个用户新增一条数据
                    //     go>10<=20,模拟1个用户随机更新一条数据
                    //     go>=20,模拟1个用户随机删除一条数据
                    Thread.Sleep(100);
                    go = new Random().Next(1, 30);
 
                    //新增记录
                    if (go <= 10 || _DocNos.Count < 1)
                    {
                        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("@Sales", "admin");
                        cmd.AddParam("@CreationDate", DateTime.Now);
                        cmd.AddParam("@CreatedBy", "admin");
                        cmd.AddParam("@LastUpdatedBy", "admin");
                        cmd.AddParam("@LastUpdateDate", DateTime.Now);
 
                        int i = db.ExecuteCommand(cmd.Command);
                        if (i > 0)
                        {
                            //执行命令:处理的记录数
                            lock (_Rows_Inserted)
                            {
                                _Rows_Inserted = int.Parse(_Rows_Inserted.ToString()) + 1;
                            }
                        }
                    }
 
                    //修改记录
                    if (go > 10 && go <= 20)
                    {
                        cmd = db.CreateCommand(SQL1);
                        cmd.AddParam("@QONO", GetDocNoExists());
                        cmd.AddParam("@Client", "csframework.com");
                        cmd.AddParam("@Remark", "用户更新了数据");
                        cmd.AddParam("@LastUpdateDate", DateTime.Now);
 
                        int i = db.ExecuteCommand(cmd.Command);
 
                        //执行命令:处理的记录数                         
                        lock (_Rows_Updated)
                        {
                            _Rows_Updated = int.Parse(_Rows_Updated.ToString()) + 1;
                        }
 
                    }
 
                    //删除记录
                    if (go > 20 && go <= 30)
                    {
                        cmd = db.CreateCommand(SQL2);
                        cmd.AddParam("@QONO", GetDocNoExists());
 
                        //执行命令:处理的记录数
                        int i = db.ExecuteCommand(cmd.Command);
 
                        lock (_Rows_Deleted)
                        {
                            _Rows_Deleted = int.Parse(_Rows_Deleted.ToString()) + 1;//仅统计删除成功的记录
                        }
 
                    }
 
                    sub_total++;
 
                    if (cmd != null) cmd.Command.Dispose();
 
                    db = null;
 
                    //通知处理1次
                    this.Invoke(new SyncCall(this.OnProgress), new object[] { 1 });
 
                }
 
                lock (_threadList) _threadList.Remove(currentID);
 
                lock (_ThreadFinish) _ThreadFinish = int.Parse(_ThreadFinish.ToString()) + 1;
 
                //通知事件:线程处理完成
                this.Invoke(new SyncCall(this.OnResult), new object[] { $"线程<{currentID}>结束,共处理:{sub_total}个SQL连接!" });
 
                Application.DoEvents();
            }
            catch (Exception ex)
            {
                this.Invoke(new SyncCall(this.OnResult), new object[] { "错误:" + ex.Message });
 
                lock (_ThreadFinish) _ThreadFinish = int.Parse(_ThreadFinish.ToString()) + 1;
                lock (_Failed) _Failed = int.Parse(_Failed.ToString()) + 1;
            }
        }
 
        private string GetDocNoExists()
        {
            lock (_DocNos)
            {
                int i = new Random().Next(1, _DocNos.Count);
                return _DocNos[i - 1];
            }
        }
 
        /// <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);
            var docNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + Guid.NewGuid().ToString().ToLower().Replace("-", "").Substring(0, 10);
            lock (_DocNos)
            {
                _DocNos.Add(docNo);
            }
            return docNo;
        }
 
        /// <summary>
        /// 显示结果信息
        /// </summary>
        /// <param name="value"></param>
        private void OnResult(object value)
        {
            string text = value.ToString();
            txtResult.AppendText(text + "\r\n");
            txtResult.ScrollToCaret();
 
            if (text.IndexOf("开始线程") >= 0)
                txtRunThreads.Text = _threadCount.ToString();
        }
 
        /// <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
            {
 
            }
        }
    }
 
    /// <summary>
    /// 异步调用过程
    /// </summary>
    /// <param name="value"></param>
    public delegate void SyncCall(object value);
 
 
}