私はPowerShellスクリプトを使って、pingログをグラフデータに変換しています。Powershellの配列が遅い
スクリプトが正常に動作しているが、非常にゆっくりと配列操作によって引き起こさ稼働。
スクリプトは10Kラインファイル上で実行した場合、それは約7秒かかります。 配列操作が削除された場合、完了までに要する時間はそれよりも短くなります。
私は一時的な配列を使用せずに、呼び出し元の関数にデータを返すために別の解決策を探しています。入力ログの
例:
02.01.2017-14:53:54> Reply from 8.8.8.8: bytes=32 time=184ms TTL=57
02.01.2017-14:53:54> Reply from 8.8.8.8: bytes=32 time=18ms TTL=57
02.01.2017-14:53:59> Request timed out.
02.01.2017-14:54:01> Reply from 192.168.2.186: Destination host unreachable.
02.01.2017-14:54:05> Request timed out.
02.01.2017-14:54:07> Reply from 192.168.2.186: Destination host unreachable.
スクリプト:実は
function Convert-V4PingLog2ChartData
{
param($V4PingLogFile, $AvarageRespondTime, $ChartCounter)
$ConvertedData=""
$var=Get-Content $V4PingLogFile
$varArray=$var.split("`n")
$varArray=$varArray | Select-Object -Skip 2
$CommandExecuteTime=Measure-Command{
$pattern = "^([0-9]{2})\.([0-9]{2})\.([0-9]{4})-([0-9]{2}):([0-9]{2}):([0-9]{2})> Reply from [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: bytes=32 time=([0-9]{1,4})ms TTL=[0-9]{1,3}$";
$pattern2="^([0-9]{2})\.([0-9]{2})\.([0-9]{4})-([0-9]{2}):([0-9]{2}):([0-9]{2})> (Request timed out.|Reply from [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: Destination host unreachable.)$"
foreach($nextLine in $varArray)
{
if($nextLine -like "* time=*")
{
$ConvertedData+=$nextLine -replace $pattern, "data$ChartCounter.addRow([new Date(`$3, `$2, `$1, `$4, `$5, `$6, 00), `$7, $AvarageRespondTime]);"
}
else
{
$ConvertedData+=$nextLine -replace $pattern2, "data$ChartCounter.addRow([new Date(`$3, `$2, `$1, `$4, `$5, `$6, 00), 0, $AvarageRespondTime]);"
}
}
}
Write-Host $CommandExecuteTime
return $ConvertedData
}
ありがとう、それは完全に動作します – Krisz
良い1つのAnsgar :) –