之前我在SO上阅读了一篇很棒的answer,我想知道为什么在PowerShell中还没有模仿它。
在unix / linux中,我们可以使用time
命令作为简单的基准测试工具。
$ time ./script1.sh
real 0m1.005s
user 0m0.000s
sys 0m0.008s
在powershell中,我们可以类似地使用measure-command
:
$ Measure-Command {java post_incr}
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 18
Ticks : 10188003
TotalDays : 1.17916701388889E-05
TotalHours : 0.000283000083333333
TotalMinutes : 0.016980005
TotalSeconds : 1.0188003
TotalMilliseconds : 1018.8003
但这与time
不同,time
报告真实,用户和系统(请参阅链接SO答案中三者之间的差异。)
这个({{1}})显然是一个非常有用的小工具。是否有任何用户编写此功能的cmdlet,或者它已经在V3中或计划用于将来的版本?
答案 0 :(得分:3)
警告,PowerShell领先。
有些咖啡让我想出了这个:
let path = UIBezierPath(roundedRect: self.myview.bounds, byRoundingCorners:.topLeft, cornerRadii: CGSize.init(width: 60, height:0))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.myView.layer.mask = maskLayer
如果你想要它什么都不输出,只需用out-null替换:
function time { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-default} }
你这样使用它:
function timequiet { $Command = "$args"; Measure-Command { Invoke-Expression $Command 2>&1 | out-null} }
答案 1 :(得分:2)
使用GetProcessTimes函数编写C程序,您可以在http://msdn.microsoft.com/en-us/library/ms683223%28VS.85%29.aspx中找到它的文档。
在“Windows系统编程”一书的示例中,出现了一个程序,它使用该函数来准确检索所需内容(经过时间,内核时间,用户时间)。该程序称为“timep.exe”,它可以在压缩示例存档中的runX子目录中找到(X来自编译中使用的Visual Studio版本)。以下是您可以从中下载该存档http://www.jmhartsoftware.com/的作者页面,当然源代码也在那里,因此您可以准确了解timep.exe的工作原理。
答案 2 :(得分:1)
我使用System.Diagnostics.Process
和GetProcessTimes
提供的时间来为time
提供实施:
$source=@'
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Timer
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetProcessTimes(IntPtr handle, out long creation, out long exit, out long kernel,
out long user);
public static void Time(string file,string args)
{
long user,kernel,exit,creation;
Process proc = null;
proc = Process.Start(file,args);
proc.WaitForExit();
GetProcessTimes(proc.Handle, out creation, out exit, out kernel, out user);
long real = exit - creation;
Console.WriteLine("real {0}\nuser {1}\nsys {2}", real / 10000000.0, user/10000000.0,kernel/10000000.0);
}
}
'@
Add-Type -TypeDefinition $source -Language CSharpVersion3
function time ($scriptblock) {
$file = "powershell";
$args = $scriptblock;
$startInfo = new-object Diagnostics.ProcessStartInfo;
$startInfo.FileName = $file;
$startInfo.Arguments = $args;
$startInfo.CreateNoWindow = $true;
$startInfo.UseShellExecute = $false;
$startInfo.RedirectStandardOutput = $true;
$process = [Diagnostics.Process]::Start($startInfo);
$process.WaitForExit();
write-host $process.StandardOutput.ReadToEnd();
write-host real: ($process.ExitTime - $process.StartTime)
write-host user: $process.UserProcessorTime;
write-host sys: $process.PrivilegedProcessorTime;
write-host using GetProcessTimes
[Timer]::Time($file,$args)
}
time {sleep 10}
由于我正在创建一个powershell进程并在其中运行命令,因此实时出现大约11秒(对于sleep 10
)并不是很完美。我将看看我是否可以为此实现cmdlet。
答案 3 :(得分:1)
您尝试测量的过程也可能在后台分叉和运行,导致测量 - 命令测量被缩短。您可以将开始流程与 -Wait 参数一起使用,以获得您期望的全时测量。这是一个示例,显示stackoverflow.com需要34秒才能关闭空闲连接:
$program = 'telnet'
$ArgumentList = 'stackoverflow.com 80'
$WorkingDirectory = 'c:\'
Measure-Command {
$p = Start-Process -FilePath $program -ArgumentList $ArgumentList -Wait -PassThru -WorkingDirectory $WorkingDirectory -NoNewWindow;
Write-Host $p.ExitCode;
}
#Output:
0
Days : 0
Hours : 0
Minutes : 0
Seconds : 34
Milliseconds : 37
Ticks : 340370635
TotalDays : 0.000393947494212963
TotalHours : 0.00945473986111111
TotalMinutes : 0.567284391666667
TotalSeconds : 34.0370635
TotalMilliseconds : 34037.0635