-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSimulate_Databse_Load.ps1
77 lines (70 loc) · 2.51 KB
/
Simulate_Databse_Load.ps1
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
# https://www.scarydba.com/2019/01/07/powershell-to-simulate-load/
# GRANT FRITCHEY
# 2019-01-07
# connect to the database
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = 'Server=WIN-8A2LQANSO51;Database=AdventureWorks2017;trusted_connection=true'
# gather values
$RefCmd = New-Object System.Data.SqlClient.SqlCommand
$RefCmd.CommandText = "SELECT th.ReferenceOrderID,
COUNT(th.ReferenceOrderID) AS RefCount
FROM Production.TransactionHistory AS th
GROUP BY th.ReferenceOrderID;"
$RefCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $RefCmd
$RefData = New-Object System.Data.DataSet
$SqlAdapter.Fill($RefData)
# Execute a stored procedure
$Sniffcmd = New-Object System.Data.SqlClient.SqlCommand
$Sniffcmd.CommandType = [System.Data.CommandType]'StoredProcedure'
$Sniffcmd.CommandText = "dbo.ProductTransactionHistoryByReference"
$Sniffcmd.Parameters.Add("@ReferenceOrderID",[System.Data.SqlDbType]"Int")
$Sniffcmd.Connection = $SqlConnection
# Optionally, clear the cache
$Freecmd = New-Object System.Data.SqlClient.SqlCommand
$Freecmd.CommandText = "DECLARE @plan_handle VARBINARY(64);
SELECT @plan_handle = deps.plan_handle
FROM sys.dm_exec_procedure_stats AS deps
WHERE deps.object_id = OBJECT_ID('dbo.ProductTransactionHistoryByReference');
DBCC FREEPROCCACHE(@plan_handle);"
$Freecmd.Connection = $SqlConnection
# Count the executions
$x = 0
# Run forever
while(1 -ne 0)
{
foreach($row in $RefData.Tables[0])
{
# Establish an occasional wait
$check = get-random -Minimum 7 -Maximum 20
$wait = $x % $check
if ($wait -eq 0)
{
#set a random sleep period in seconds
$waittime = get-random -minimum 3 -maximum 13
start-sleep -s $waittime
$x = 0}
# Up the count
$x += 1
# Execute the procedure
$RefID = $row[0]
$SqlConnection.Open()
$Sniffcmd.Parameters["@ReferenceOrderID"].Value = $RefID
$Sniffcmd.ExecuteNonQuery() | Out-Null
$SqlConnection.Close()
# clear the cache on each execution
#$SqlConnection.Open()
#$Freecmd.ExecuteNonQuery() | Out-Null
#$SqlConnection.Close()
# clear the cache based on random
$check = get-random -Minimum 7 -Maximum 20
$clear = $x % $check
if($clear -eq 4)
{
$SqlConnection.Open()
$Freecmd.ExecuteNonQuery() | Out-Null
$SqlConnection.Close()
}
}
}