循环性能测试

问题描述 投票:0回答:2

我有无限的文件执行以下代码。列表文件只有5条记录用于测试目的。为了提高性能,我希望一夜之间用相同的数据执行测试。请建议如何增加执行次数。

list.txt file has the following records :
microsoft.com
twitter.com
facebook.com
amazon.com
google.com

$InputFile = 'C:\Temp\list.txt'
$addresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile

    while($reader.ReadLine() -ne $null){ $TotalIPs++ }
    write-Host $TotalIPs
    write-host    ""    
    write-Host "Executing nslookup query for all ..."    
        foreach($address in $addresses) {
            ## Progress bar
            $i++
            <# $percentdone = (($i / $TotalIPs) * 100)
            $percentdonerounded = "{0:N0}" -f $percentdone
            Write-Progress -Activity "Executing nslookup queries" -CurrentOperation "Performing on IP: $address (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone #>
            ## End progress bar
            try {
                [system.net.dns]::resolve($address) | Select HostName,AddressList
                }
                catch {
                    Write-host "$address was not found. $_" -ForegroundColor Green
                }
           }



    write-host    ""            
    write-Host "Pinging each address..."
        foreach($address in $addresses) {
            ## Progress bar
            $j++
            $percentdone2 = (($j / $TotalIPs) * 100)
            $percentdonerounded2 = "{0:N0}" -f $percentdone2
            Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
            ## End progress bar
                if (test-Connection -ComputerName $address -Count 2 -Quiet ) 
{  
                    write-Host "$address responded" -ForegroundColor Green 
                    } else 
                    { Write-Warning "$address does not respond to pings"              
                    }  
        }
write-host    ""        
write-host "Complete!"
powershell performance-testing
2个回答
1
投票

首先,我会在开始时将IP的总数设置为0。其次,我会在你文本文件中提供的地址之间放置一个永远不会结束的while循环。第三,我会在每个周期结束时将$ j设置为0。

这是您发布的一些修改后的代码:

$InputFile = 'C:\Temp\list.txt'
$addresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile
$TotalIPs = 0

while($reader.ReadLine() -ne $null){ $TotalIPs++ }
write-Host $TotalIPs
write-host    ""    
write-Host "Executing nslookup query for all ..."    

While($true)
{
    foreach($address in $addresses) 
    {
        ## Progress bar
        $i++

        ## End progress bar
        try 
        {
            [system.net.dns]::resolve($address) | Select HostName,AddressList
        }
        catch 
        {
            Write-host "$address was not found. $_" -ForegroundColor Green
        }
    }

    write-host    ""            
    write-Host "Pinging each address..."
    foreach($address in $addresses) {
    ## Progress bar

    $j++
    $percentdone2 = (($j / $TotalIPs) * 100)
    $percentdonerounded2 = "{0:N0}" -f $percentdone2

    Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2

    ## End progress bar
    if (test-Connection -ComputerName $address -Count 2 -Quiet ) 
        {  
            write-Host "$address responded" -ForegroundColor Green 
        } 
        else 
        { 
            Write-Warning "$address does not respond to pings"              
        }  
    }
    $j = 0

    write-host    ""        
    write-host "Complete!"
}

0
投票
I have solved it by using the counters as well. Here is the code :

    $InputFile = 'C:\Temp\list.txt'
$addresses = get-content $InputFile
$reader = New-Object IO.StreamReader $InputFile
$TotalIPs = 0
$Ctr = 5

while($reader.ReadLine() -ne $null){ $TotalIPs++ }
write-Host $TotalIPs
write-host    ""    
write-Host "Executing nslookup query for all ..."    

While($Ctr)
{
    foreach($address in $addresses) 
    {
        ## Progress bar
        $i++

        ## End progress bar
        try 
        {
            [system.net.dns]::resolve($address) | Select HostName,AddressList
        }
        catch 
        {
            Write-host "$address was not found. $_" -ForegroundColor Green
        }
    }
    $i=0

    write-host    ""            
    write-Host "Pinging each address..."
    foreach($address in $addresses) {
    ## Progress bar

    $j++
    $percentdone2 = (($j / $TotalIPs) * 100)
    $percentdonerounded2 = "{0:N0}" -f $percentdone2

    Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2

    ## End progress bar
    if (test-Connection -ComputerName $address -Count 2 -Quiet ) 
        {  
            write-Host "$address responded" -ForegroundColor Green 
        } 
        else 
        { 
            Write-Warning "$address does not respond to pings"              
        }  
    }
    $j = 0

    write-host    ""        
    write-host "Complete!"
    $Ctr--
}
© www.soinside.com 2019 - 2024. All rights reserved.