简体   繁体   English

如何在4.0环境中加速Powershell的启动?

[英]How to speed up startup of Powershell in the 4.0 environment?

I'm considering using Powershell scripts for triggers on our Perforce server. 我正在考虑在Perforce服务器上使用Powershell脚本作为触发器。 Unfortunately, even on our fast hardware it still takes 2 seconds for Powershell to start up. 不幸的是,即使在我们的快速硬件上,Powershell启动仍需要2秒钟。 Now, this doesn't seem like much time unless you consider that 200 users are pounding the server constantly, and a trigger may (depending on conditions) cause a table lock while it runs, stalling out other related requests. 现在,除非您考虑到200个用户不断地敲击服务器,并且触发器可能(取决于条件)在运行时导致表锁定,停止其他相关请求,否则这似乎不是很多时间。

I'm looking for ways to reduce the startup time of Powershell. 我正在寻找减少Powershell启动时间的方法。 Things that I've found on Google: 我在Google上找到的东西:

  • Reduce the profile. 减少个人资料。 This is a default installation, no profile customization, no PSCX, etc. so this doesn't help. 这是默认安装,没有配置文件自定义,没有PSCX等,所以这没有帮助。
  • Run NGEN on Posh binaries. 在Posh二进制文件上运行NGEN。 This is old advice for a V1 issue. 对于V1问题,这是一个老建议。 We're on V2. 我们在V2。
  • Keep it warm. 保持温暖。 Initial Posh startup is slow but later startups are fast. 初始的Posh启动很慢但后来的初创公司很快。 Also not helpful for us. 对我们也没有帮助。 On the server the warm start is 2 seconds and it's kept warm by frequent requests. 在服务器上,热启动是2秒,并且经常请求保持温暖。
  • Don't use Powershell for this purpose. 不要将Powershell用于此目的。 Well, I'm hoping to avoid that "solution"... 好吧,我希望避免那个“解决方案”......

Does anyone have any other suggestions for cutting startup time? 有没有人有任何其他建议减少启动时间? I'm not expecting a startup as fast as cmd.exe or a .NET command line app, but if I can get this to .5 seconds I think we'll be ok. 我不希望像cmd.exe或.NET命令行应用程序那样快速启动,但是如果我能得到这个.5秒,我想我们会没事的。 Impossible? 不可能?

UPDATE - it turns out this is a 4.0 issue. 更新 - 事实证明这是一个4.0问题。 If I add a config file as described in another SO question the startup takes 2 seconds. 如果我按照另一个SO问题中的描述添加配置文件,启动需要2秒。 If I leave it at its default, then startup is under a quarter second. 如果我将其保留为默认值,那么启动时间不到四分之一秒。

Then I thought..perhaps it's a 4.0 GAC issue. 然后我想..也许这是4.0 GAC问题。 So I ran the script on the PowerShell Blog but it says all the assemblies are already ngen'd. 所以我在PowerShell博客上运行了脚本,但它说所有的程序集都已经完成了。 I made sure that it is indeed using the 4.0 ngen as well. 我确信它确实也使用了4.0 ngen。

So I'm left with two options: 所以我有两个选择:

  • Figure out why 4.0 is so much more expensive than 3.5. 弄清楚为什么4.0比3.5贵得多。
  • Switch to the 3.5 framework where I do not need it and I do need the performance. 切换到我不需要它的3.5框架,我确实需要性能。

I'd love to solve the first but don't know where to start. 我想先解决第一个但不知道从哪里开始。 Can anyone help? 有人可以帮忙吗?

Run NGEN on Posh binaries. 在Posh二进制文件上运行NGEN。 This is old advice for a V1 issue. 对于V1问题,这是一个老建议。 We're on V2. 我们在V2。

I've been playing with this under V3, and I found out that some images were generated. 我一直在V3下玩这个,我发现有些图像是生成的。

And it certainly feels like startup is now much faster. 当然,创业公司现在感觉要快得多。 I'll know better after a reboot, as the first time is always the worst. 重启后我会更清楚,因为第一次总是最糟糕的。 The (my updated) script to do so with Win 7 is 使用Win 7执行此操作的(我更新的)脚本是

$env:path = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
[AppDomain]::CurrentDomain.GetAssemblies() | % {
  if (! $_.location) {continue}
  $Name = Split-Path $_.location -leaf
  Write-Host -ForegroundColor Yellow "NGENing : $Name"
  ngen install $_.location | % {"`t$_"}
}

Following an excellent answer to my question on Technet , I discovered a much easier way to trigger NGEN manually (since it seems it doesn't happen automatically on a clean install of Windows 8.1): Technet我的问题得到了很好的答案后,我发现了一种更简单的手动触发NGEN的方法(因为它似乎不会在Windows 8.1的全新安装中自动发生):

schtasks /Run /TN "\Microsoft\Windows\.NET Framework\.NET Framework NGEN v4.0.30319"
schtasks /Run /TN "\Microsoft\Windows\.NET Framework\.NET Framework NGEN v4.0.30319 64"

I'm afraid you're probably out of luck if you stick with the console host powershell.exe, unless you feel like writing an optimized script processor by hosting a runspace/runspacefactory in a custom console application - or a windows service - you roll yourself. 如果你坚持使用控制台主机powershell.exe,我担心你可能会运气不好,除非你想通过在自定义控制台应用程序中托管运行空间/运行空间来编写优化的脚本处理器 - 或者是Windows服务 - 你滚动你自己。 You might shave a second off, but 0.5s startup for a .NET application is unlikely, especially if the servers are tight for memory. 你可能会刮掉第二个,但是不太可能为.NET应用程序启动0.5秒,特别是如果服务器内存紧张的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM