/
LoadEngine.cs
604 lines (499 loc) · 23 KB
/
LoadEngine.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#region
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
#endregion
namespace SQLQueryStress
{
internal class LoadEngine
{
private static readonly Queue<QueryOutput> QueryOutInfo = new Queue<QueryOutput>();
private readonly bool _collectIoStats;
private readonly bool _collectTimeStats;
private readonly List<SqlCommand> _commandPool = new List<SqlCommand>();
private readonly int _commandTimeout;
private readonly string _connectionString;
private readonly bool _forceDataRetrieval;
private readonly int _iterations;
private readonly string _paramConnectionString;
private readonly Dictionary<string, string> _paramMappings;
//private readonly List<Queue<queryOutput>> queryOutInfoPool = new List<Queue<queryOutput>>();
private readonly string _paramQuery;
private readonly string _query;
private readonly List<Thread> _threadPool = new List<Thread>();
private readonly int _threads;
private int _queryDelay;
public LoadEngine(string connectionString, string query, int threads, int iterations, string paramQuery, Dictionary<string, string> paramMappings,
string paramConnectionString, int commandTimeout, bool collectIoStats, bool collectTimeStats, bool forceDataRetrieval)
{
//Set the min pool size so that the pool does not have
//to get allocated in real-time
var builder = new SqlConnectionStringBuilder(connectionString) {MinPoolSize = threads};
_connectionString = builder.ConnectionString;
_query = query;
_threads = threads;
_iterations = iterations;
_paramQuery = paramQuery;
_paramMappings = paramMappings;
_paramConnectionString = paramConnectionString;
_commandTimeout = commandTimeout;
_collectIoStats = collectIoStats;
_collectTimeStats = collectTimeStats;
_forceDataRetrieval = forceDataRetrieval;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public static bool ExecuteCommand(string connectionString, string sql)
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (var cmd = new SqlCommand(sql, conn))
{
cmd.ExecuteNonQuery();
return true;
}
}
}
public void StartLoad(BackgroundWorker worker, int queryDelay)
{
_queryDelay = queryDelay;
StartLoad(worker);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
private void StartLoad(BackgroundWorker worker)
{
var useParams = false;
var badParams = new List<string>();
foreach (var theKey in _paramMappings.Keys)
{
if ((_paramMappings[theKey] == null) || (_paramMappings[theKey].Length == 0))
{
badParams.Add(theKey);
}
}
foreach (var theKey in badParams)
{
_paramMappings.Remove(theKey);
}
//Need some parameters?
if (_paramMappings.Count > 0)
{
ParamServer.Initialize(_paramQuery, _paramConnectionString, _paramMappings);
useParams = true;
}
//Initialize the connection pool
var conn = new SqlConnection(_connectionString);
//TODO: use this or not??
SqlConnection.ClearPool(conn);
conn.Open();
conn.Dispose();
//make sure the run cancelled flag is not set
QueryInput.RunCancelled = false;
//Spin up the load threads
for (var i = 0; i < _threads; i++)
{
conn = new SqlConnection(_connectionString);
//TODO: Figure out how to make this option work (maybe)
//conn.FireInfoMessageEventOnUserErrors = true;
SqlCommand statsComm = null;
var queryComm = new SqlCommand {CommandTimeout = _commandTimeout, Connection = conn, CommandText = _query};
if (useParams)
{
queryComm.Parameters.AddRange(ParamServer.GetParams());
}
var setStatistics = (_collectIoStats ? @"SET STATISTICS IO ON;" : "") + (_collectTimeStats ? @"SET STATISTICS TIME ON;" : "");
if (setStatistics.Length > 0)
{
statsComm = new SqlCommand {CommandTimeout = _commandTimeout, Connection = conn, CommandText = setStatistics};
}
//Queue<queryOutput> queryOutInfo = new Queue<queryOutput>();
var input = new QueryInput(statsComm, queryComm,
// this.queryOutInfo,
_iterations, _forceDataRetrieval, _queryDelay);
var theThread = new Thread(input.StartLoadThread) {Priority = ThreadPriority.BelowNormal};
_threadPool.Add(theThread);
_commandPool.Add(queryComm);
//queryOutInfoPool.Add(queryOutInfo);
}
//Start the load threads
for (var i = 0; i < _threads; i++)
{
_threadPool[i].Start();
}
//Start reading the queue...
var finishedThreads = 0;
var cancelled = false;
while (finishedThreads < _threads)
{
// for (int i = 0; i < threads; i++)
// {
// try
// {
QueryOutput theOut = null;
//lock (queryOutInfoPool[i])
lock (QueryOutInfo)
{
//if (queryOutInfoPool[i].Count > 0)
//theOut = (queryOutput)queryOutInfoPool[i].Dequeue();
if (QueryOutInfo.Count > 0)
theOut = QueryOutInfo.Dequeue();
else
Monitor.Wait(QueryOutInfo);
}
if (theOut != null)
{
//Report output to the UI
worker.ReportProgress((int) (finishedThreads / (decimal) _threads * 100), theOut);
//TODO: Make this actually remove the queue from the pool so that it's not checked again -- maintain this with a bitmap, perhaps?
if (theOut.Finished)
finishedThreads++;
}
/* }
catch (InvalidOperationException e)
{
}
*/
/*
if (theOut != null)
Thread.Sleep(200);
else
Thread.Sleep(10);
*/
// }
//TODO: Remove this ?
GC.Collect();
if (worker.CancellationPending && !cancelled)
{
QueryInput.RunCancelled = true;
//First, kill connections as fast as possible
SqlConnection.ClearAllPools();
//for each 20 threads, create a new thread dedicated
//to killing them
var threadNum = _threadPool.Count;
var killerThreads = new List<Thread>();
while (threadNum > 0)
{
var i = threadNum <= 20 ? 0 : threadNum - 20;
var killThreads = new Thread[threadNum - i < 1 ? threadNum : threadNum - i];
var killCommands = new SqlCommand[threadNum - i < 1 ? threadNum : threadNum - i];
_threadPool.CopyTo(i, killThreads, 0, killThreads.Length);
_commandPool.CopyTo(i, killCommands, 0, killCommands.Length);
for (var j = threadNum - 1; j >= i; j--)
{
_threadPool.RemoveAt(j);
_commandPool.RemoveAt(j);
}
var kill = new ThreadKiller(killThreads, killCommands);
var killer = new Thread(kill.KillEm);
killer.Start();
Thread.Sleep(0);
killerThreads.Add(killer);
threadNum = i;
}
//wait for the kill threads to return
//before exiting...
foreach (var theThread in killerThreads)
{
theThread.Join();
}
cancelled = true;
}
}
//clear any remaining messages -- these are almost certainly
//execeptions due to thread cancellation
//queryOutInfo.Clear();
}
//TODO: Monostate pattern to be investigated (class is never instantiated)
private class ParamServer
{
private static int _currentRow;
private static int _numRows;
//The actual params that will be filled
private static SqlParameter[] _outputParams;
//Map the param columns to ordinals in the data table
private static int[] _paramDtMappings;
private static DataTable _theParams;
public static void GetNextRow_Values(SqlParameterCollection newParam)
{
var rowNum = Interlocked.Increment(ref _currentRow);
var dr = _theParams.Rows[rowNum % _numRows];
for (var i = 0; i < _outputParams.Length; i++)
{
newParam[i].Value = dr[_paramDtMappings[i]];
}
}
public static SqlParameter[] GetParams()
{
var newParam = new SqlParameter[_outputParams.Length];
for (var i = 0; i < _outputParams.Length; i++)
{
newParam[i] = (SqlParameter) ((ICloneable) _outputParams[i]).Clone();
}
return newParam;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
public static void Initialize(string paramQuery, string connString, Dictionary<string, string> paramMappings)
{
var a = new SqlDataAdapter(paramQuery, connString);
_theParams = new DataTable();
a.Fill(_theParams);
_numRows = _theParams.Rows.Count;
_outputParams = new SqlParameter[paramMappings.Keys.Count];
_paramDtMappings = new int[paramMappings.Keys.Count];
//Populate the array of parameters that will be cloned and filled
//on each request
var i = 0;
foreach (var parameterName in paramMappings.Keys)
{
_outputParams[i] = new SqlParameter {ParameterName = parameterName};
var paramColumn = paramMappings[parameterName];
//if there is a param mapped to this column
if (paramColumn != null)
_paramDtMappings[i] = _theParams.Columns[paramColumn].Ordinal;
i++;
}
}
}
private class QueryInput
{
[ThreadStatic] private static QueryOutput _outInfo;
private static bool _runCancelled;
//This regex is used to find the number of logical reads
//in the messages collection returned in the queryOutput class
private static readonly Regex FindReads = new Regex(@"(?:Table \'\w{1,}\'. Scan count \d{1,}, logical reads )(\d{1,})", RegexOptions.Compiled);
//This regex is used to find the CPU and elapsed time
//in the messages collection returned in the queryOutput class
private static readonly Regex FindTimes =
new Regex(
@"(?:SQL Server Execution Times:|SQL Server parse and compile time:)(?:\s{1,}CPU time = )(\d{1,})(?: ms,\s{1,}elapsed time = )(\d{1,})",
RegexOptions.Compiled);
private readonly SqlCommand _queryComm;
private readonly SqlCommand _statsComm;
//private static Dictionary<int, List<string>> theInfoMessages = new Dictionary<int, List<string>>();
private readonly Stopwatch _sw = new Stopwatch();
private readonly bool _forceDataRetrieval;
// private readonly Queue<queryOutput> queryOutInfo;
private readonly int _iterations;
private readonly int _queryDelay;
public QueryInput(SqlCommand statsComm, SqlCommand queryComm,
// Queue<queryOutput> queryOutInfo,
int iterations, bool forceDataRetrieval, int queryDelay)
{
_statsComm = statsComm;
_queryComm = queryComm;
// this.queryOutInfo = queryOutInfo;
_iterations = iterations;
_forceDataRetrieval = forceDataRetrieval;
_queryDelay = queryDelay;
//Prepare the infoMessages collection, if we are collecting statistics
//if (stats_comm != null)
// theInfoMessages.Add(stats_comm.Connection.GetHashCode(), new List<string>());
}
public static bool RunCancelled
{
set { _runCancelled = value; }
}
private static void GetInfoMessages(object sender, SqlInfoMessageEventArgs args)
{
foreach (SqlError err in args.Errors)
{
var matches = FindReads.Split(err.Message);
//we have a read
if (matches.Length > 1)
{
_outInfo.LogicalReads += Convert.ToInt32(matches[1]);
continue;
}
matches = FindTimes.Split(err.Message);
//we have times
if (matches.Length > 1)
{
_outInfo.CpuTime += Convert.ToInt32(matches[1]);
_outInfo.ElapsedTime += Convert.ToInt32(matches[2]);
}
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
public void StartLoadThread()
{
try
{
//do the work
using (var conn = _queryComm.Connection)
{
SqlInfoMessageEventHandler handler = GetInfoMessages;
for (var i = 0; i < _iterations; i++)
{
if (_runCancelled)
throw new Exception();
Exception outException = null;
try
{
//initialize the outInfo structure
_outInfo = new QueryOutput();
if (conn != null)
{
conn.Open();
//set up the statistics gathering
if (_statsComm != null)
{
_statsComm.ExecuteNonQuery();
Thread.Sleep(0);
conn.InfoMessage += handler;
}
}
//Params are assigned only once -- after that, their values are dynamically retrieved
if (_queryComm.Parameters.Count > 0)
{
ParamServer.GetNextRow_Values(_queryComm.Parameters);
}
_sw.Start();
//TODO: This could be made better
if (_forceDataRetrieval)
{
var reader = _queryComm.ExecuteReader();
Thread.Sleep(0);
do
{
Thread.Sleep(0);
while (reader.Read())
{
//grab the first column to force the row down the pipe
// ReSharper disable once UnusedVariable
var x = reader[0];
Thread.Sleep(0);
}
} while (reader.NextResult());
}
else
{
_queryComm.ExecuteNonQuery();
Thread.Sleep(0);
}
_sw.Stop();
}
catch (Exception e)
{
if (_runCancelled)
throw;
else
outException = e;
if (_sw.IsRunning)
{
_sw.Stop();
}
}
finally
{
//Clean up the connection
if (_statsComm != null && conn != null)
conn.InfoMessage -= handler;
conn?.Close();
}
var finished = i == _iterations - 1;
//List<string> infoMessages = null;
//infoMessages = (stats_comm != null) ? theInfoMessages[connectionHashCode] : null;
/*
queryOutput theout = new queryOutput(
outException,
sw.Elapsed,
finished,
(infoMessages == null || infoMessages.Count == 0) ? null : infoMessages.ToArray());
*/
_outInfo.E = outException;
_outInfo.Time = _sw.Elapsed;
_outInfo.Finished = finished;
lock (QueryOutInfo)
{
QueryOutInfo.Enqueue(_outInfo);
Monitor.Pulse(QueryOutInfo);
}
//Prep the collection for the next round
//if (infoMessages != null && infoMessages.Count > 0)
// infoMessages.Clear();
_sw.Reset();
Thread.Sleep(_queryDelay);
}
}
}
catch
{
if (_runCancelled)
{
//queryOutput theout = new queryOutput(null, new TimeSpan(0), true, null);
_outInfo.Time = new TimeSpan(0);
_outInfo.Finished = true;
lock (QueryOutInfo)
{
QueryOutInfo.Enqueue(_outInfo);
}
}
else
throw;
}
}
}
public class QueryOutput
{
public int CpuTime;
public Exception E;
public int ElapsedTime;
public bool Finished;
public int LogicalReads;
public TimeSpan Time;
/*
public queryOutput(
Exception e,
TimeSpan time,
bool finished,
string[] infoMessages)
{
this.e = e;
this.time = time;
this.finished = finished;
this.infoMessages = infoMessages;
}
*/
}
private class ThreadKiller
{
private readonly SqlCommand[] _theCommands;
private readonly Thread[] _theThreads;
public ThreadKiller(Thread[] theThreads, SqlCommand[] theCommands)
{
_theThreads = theThreads;
_theCommands = theCommands;
}
public void KillEm()
{
foreach (var comm in _theCommands)
{
comm.Cancel();
comm.Connection.Dispose();
comm.Connection = null;
comm.Dispose();
Thread.Sleep(0);
}
var keepKilling = true;
while (keepKilling)
{
keepKilling = false;
foreach (var theThread in _theThreads)
{
if (theThread.IsAlive)
{
keepKilling = true;
theThread.Abort();
Thread.Sleep(0);
}
}
}
}
}
}
}