【问题标题】:\network adapter(*)\bytes total/sec vs. task manager/performance -powershell\network adapter(*)\bytes total/sec vs. task manager/performance -powershell
【发布时间】:2018-04-23 18:11:18
【问题描述】:

在 Windows 10 上,在任务管理器/性能中,它为网络适配器提供了“接收”和“发送”的值。我很好奇 MS 是如何做到这一点的,所以我想到了 powershell 中的性能计数器。所以我拼凑了一些代码来进行实验,把它放在后台作业中,这样:

$Computer = hostname;
$sndrecsum = {((get-counter -Counter '\network adapter(*)\Bytes Received/sec' -MaxSamples 30).CounterSamples | measure cookedvalue –Sum).Sum};
$sptstb = New-Object System.Net.WebClient;
$sptstb.DownloadFile('http://client.akamai.com/install/test-objects/10MB.bin', 'Out-Null');
Start-Job -Name sxrxs -scriptblock $sndrecsum -ArgumentList $Computer;
Get-Job -Name sxrxs | Wait-Job; $sndrecsumout = Get-Job -Name sxrxs | Receive-Job; ($sndrecsumout | measure -Maximum).Maximum

这就是问题所在。输出 '($sndrecsumout | measure -Maximum).Maximum' 应该得到数组 $sndrecsumout 中的最大值。它确实这样做了,但数组中的最大值总是比它应该的低,并且永远不会与任务管理器/性能中显示的“接收”和“发送”匹配(转换为任务管理器/性能中显示的主要单位后)同时)。所以我不确定任务管理器/性能是否正在使用性能计数器,或者我是否在某个地方搞砸了。

无论如何,有两个问题:首先,任务管理器/性能如何获取“接收”和“发送”的值? - 和 - 二,我是否搞砸了我正在尝试这样做的方式?

提前感谢您的回复和建议。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    好吧,我想我已经弄明白了,至少有一点,也许吧。这与界面的 Windows 10 性能监视器一起跟踪;

    $sptst = New-Object System.Net.WebClient; $Computer = hostname; $strlf += $("" | Out-String);
    $totx = {
      get-counter -Counter '\Network Interface(Intel[R] Ethernet Connection [2] I218-V)\Bytes Total/sec' -MaxSamples 40 | ForEach {[math]::Round((($_.countersamples.cookedvalue | measure -sum).sum / 1Mb), 4)}
        };
      $recx = {
      get-counter -Counter '\network Interface(Intel[R] Ethernet Connection [2] I218-V)\Bytes Received/sec' -MaxSamples 40 | ForEach {[math]::Round((($_.countersamples.cookedvalue | measure -sum).sum / 1Mb), 4)}
        };
        $sntx = {
    get-counter -Counter '\network Interface(Intel[R] Ethernet Connection [2] I218-V)\Bytes Sent/sec' -MaxSamples 40 | ForEach {[math]::Round((($_.countersamples.cookedvalue | measure -sum).sum / 1Mb), 4)}
        };
    
    Start-Job -Name xtot -scriptblock $totx  -ArgumentList $Computer;
    Start-Job -Name xrec -scriptblock $recx  -ArgumentList $Computer;
    Start-Job -Name xsnt -scriptblock $sntx  -ArgumentList $Computer;
    
    $ts = Measure-Command -Expression {$sptst.DownloadFile('http://testmy.net/dl-10MB', 'Out-Null')}; $dxtmex = [math]::Round($ts.TotalSeconds, 4);
    
    Get-Job -Name xtot -HasMoreData $True | Wait-Job ; $outxtot = Get-Job -Name xtot | Receive-Job; 
    $xthrx = ($outxtot | measure -Maximum).Maximum; $testnuma = $xthrx -match '^[\d\.]+$'; if ($testnuma -eq 'True') {$MBxthrx = $xthrx; $Mbpsxthrx = $xthrx / 0.125};
    
    Get-Job -Name xrec -HasMoreData $True | Wait-Job; $outxrec = Get-Job -Name xrec | Receive-Job; 
    $xrecx = ($outxrec | measure -Maximum).Maximum  ; $testnumb = $xrecx -match '^[\d\.]+$'; if ($testnumb -eq 'True') {$MBxrecx = $xrecx; $Mbpsxrecx = $xrecx / 0.125};
    
    Get-Job -Name xsnt -HasMoreData $True | Wait-Job; $outxsnt = Get-Job -Name xsnt | Receive-Job; 
    $xsntx = ($outxsnt | measure -Maximum).Maximum  ; $testnumb = $xsntx -match '^[\d\.]+$'; if ($testnumb -eq 'True') {$MBxsntx = $xsntx; $Mbpsxsntx = $xsntx / 0.125};
    
    $txtl = '|Throughput = ' + $Mbpsxthrx + ' Mbps (' + $MBxthrx + ' MB/s) [ Test Time = ' + $dxtmex + ' seconds ]';
    
    $rxsx = 'Received (D/L) : ' + $Mbpsxrecx + ' Mbps (' + $MBxrecx + ' MB/s)' + ' / Sent (U/L) : ' + $Mbpsxsntx + ' Mbps (' + $MBxsntx + ' MB/s)';
    
    Return $txtl + $strlf + $rxsx
    

    编辑:删除了以前的答案 - 这个更好和改进了。忽略标签“吞吐量”-“已接收 (D/L)”-“已发送 (U/L)”,因为只是在玩弄它时称它们为区分的东西,所以如果需要,可以随意称呼它们。 Windows 性能监视器中的“吞吐量”表示为百分比,而不是这样,但是当转换为数学百分比时,上面代码中“吞吐量”表示的结果(未在上面的代码中显示)似乎与 perf 监视器跟踪,即为什么我在这里选择标签“吞吐量”。同样在此期间,我想出了如何为在计数器中使用的接口获取正确的实例名称,我没有在这篇文章中展示它的用途(或它的代码),只是将接口名称放在上面的代码中,以保持严格按主题,但如果有人想看它并且可以这样做,我也会在这里发布。我可能会稍后回来并发布它,如果它在这篇文章中可以,如果不是,你可以删除它,如果你希望它会偏离主题的话。欢迎任何改进/建议。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2013-01-16
      • 2019-11-10
      • 1970-01-01
      • 2015-06-17
      相关资源
      最近更新 更多