SQL Serverへの接続にかかる時間が気になったので、sqlcmdでdb接続を繰り返し行うテストのためのスクリプトを作りました。
スクリプト自体は、特定のコマンドラインを指定回数実行して、その実行時間を計測するという動作をします。
executeRepeater.ps1
<#
.SYNOPSIS
特定のコマンドを指定回数実行して、その実行時間を計測します。
コマンドの標準出力は$out変数に格納の上、捨てます。
.EXAMPLE
.\executeRepeater.ps1 -commandLine "sqlcmd -S dbserver -E -d testdb -i .\exit.sql" -exeNum 100 -testNum 10
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]
#実行するコマンドライン
$commandLine,
[Parameter(Mandatory=$true)]
[ValidateRange(1,9999)]
[int]
#1計測単位のコマンドライン実行回数
$exeNum,
[Parameter(Mandatory=$true)]
[ValidateRange(1,9999)]
[int]
#テスト試行回数
$testNum
)
Write-Output "TestCount`tDistance(ms)"
for($testCnt=0; $testCnt -lt $testNum; $testCnt++){
$startTime = Get-Date
for($exeCnt=0; $exeCnt -lt $exeNum; $exeCnt++){
$out = Invoke-Expression $commandLine
}
$finishTime = Get-Date
$result = $finishTime - $startTime
Write-Output "$testCnt`t$result"
}
exit.sql
exit
使い方
.\executeRepeater.ps1 -commandLine "sqlcmd -S dbserver -E -d testdb -i .\exit.sql" -exeNum 100 -testNum 10
上記は、”sqlcmd -S dbserver -E -d testdb -i .\exit.sql”を 100回繰り返し実行してその時間を計測して、それを10回計測するという動作になります。
実行結果例
TestCount Distance(ms) 0 00:00:05.0145014 1 00:00:04.9894989 2 00:00:05.0875087 3 00:00:04.8354835 4 00:00:04.9194919 5 00:00:04.7834783 6 00:00:05.0385038 7 00:00:05.1055105 8 00:00:05.0655065 9 00:00:05.7085708