-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-latency.ps1
More file actions
64 lines (60 loc) · 2.07 KB
/
sql-latency.ps1
File metadata and controls
64 lines (60 loc) · 2.07 KB
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
<#
param(
[String]$Server="sql.premcloud.local",
[String]$Database="vra",
[String]$User="sa",
[String]$Password="YourDBPassword",
[Int32]$Attempts=101
)
$ConnectionString = "Data Source=$server;Initial Catalog=master;"
if ([String]::IsNullOrEmpty($User))
{
$ConnectionString = $ConnectionString + "Integrated Security=True;"
}
else
{
$ConnectionString = $ConnectionString + "User Id=$User;Password=$Password;"
}
#>
$Attempts=1000
$ConnectionString ="server=$server ;database=$Database;user id=$User;password=$PassWord ;trusted_connection=true;"
$durations = @()
$sw = [Diagnostics.Stopwatch ]::StartNew()
for($i = 1; $i -le $Attempts; $i ++)
{
$transactionScope = New-Object System.Transactions.TransactionScope ([System.Transactions.TransactionScopeOption ]::RequiresNew)
Try
{
$stopwatch = [Diagnostics.Stopwatch ]::StartNew()
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $ConnectionString
$connection.Open()
$commmand = New-Object System.Data.SqlClient.SqlCommand
$commmand.Connection = $connection
$commmand.CommandText = "select * from sys.sysprocesses"
$connection2 = New-Object System.Data.SqlClient.SqlConnection
$connection2.ConnectionString = $ConnectionString
$connection2.Open()
$result = $commmand. ExecuteNonQuery()
$connection.Close()
$connection2.Close()
$transactionScope.Complete()
$durations += $stopwatch. Elapsed.Milliseconds
$stopwatch.Stop()
$stopwatch.Reset()
}
Catch [ system.exception]
{
$message = [String ]::Format("The following exception occurred, while executing SQL command. Message: {0}", $_. Exception.Message)
$stackTrace = [String ]::Format("Stack trace: {0}" , $_.Exception .StackTrace)
Write-Output $message
Write-Output $stackTrace
throw
}
Finally
{
$transactionScope.Dispose()
}
}
Write-Output $sw .Elapsed
$durations | Measure-Object -Average -Sum -Maximum -Minimum