• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Slow performance initializing PowerShell from managed code

  • Thread starter =?Utf-8?B?ZGg=?=
  • Start date
?

=?Utf-8?B?ZGg=?=

#1
I am building a Ex2007 client in managed code and have been consistently
seeing very slow performance from Monad/PowerShell initialization every time
I execute/debug.

The following two statements:
runspaceConfiguration = RunspaceConfiguration.Create();
and
runspaceConfiguration.AddMshSnapIn(EXCHANGE_MANAGEMENT_ADMIN, out
mshSnapinException);

are the two slowest lines of code, each taking about 13 seconds to execute.

Running release code instead of debug code moves the
RunspaceConfiguration.Create(); 13 second delay to the runspace.Open();
statement for some reason, but it still takes at least 25 seconds to
initialize before I can invoke a cmdlet call.

Using static Runspace and RunspaceConfiguration helps for repeated calls,
but I still have to reload them with each debug session, so a lot of
expensive, unproductive wait time.

Any recommendations on how to get better response time out of PS
initialization from managed code?
 

My Computer

?

=?Utf-8?B?ZGg=?=

#2
btw - this is the code I used to measure performance:


using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace PowerShellDemo
{
class Program
{
static void Main(string[] args)
{
RunspaceConfiguration runspaceConfiguration = null;
MshSnapInException mshSnapinException = null;
Runspace runspace = null;
string EXCHANGE_MANAGEMENT_ADMIN =
"Microsoft.Exchange.Management.Msh.Admin";

Console.WriteLine("press any key to make PowerShell call...");
Console.ReadKey();
Output("step", "msec");
Output("----", "----");

DateTime dtStart;
DateTime dtStartStep;

dtStart = DateTime.Now;

dtStartStep = DateTime.Now;
runspaceConfiguration = RunspaceConfiguration.Create();
Output("RunspaceConfigurationCreated", dtStartStep);

dtStartStep = DateTime.Now;
runspaceConfiguration.AddMshSnapIn(EXCHANGE_MANAGEMENT_ADMIN,
out mshSnapinException);
Output("MshSnapIn Added", dtStartStep);

dtStartStep = DateTime.Now;
runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
Output("RunspaceCreated", dtStartStep);

dtStartStep = DateTime.Now;
runspace.Open();
Output("RunspaceOpened", dtStartStep);

Command cmd = new Command("get-Date");
ICollection<MshObject> results = null;

Pipeline pipeLine = null;

try
{
dtStartStep = DateTime.Now;
pipeLine = runspace.CreatePipeline();
Output("PipelineCreated", dtStartStep);

dtStartStep = DateTime.Now;
using (pipeLine)
{
pipeLine.Commands.Add(cmd);
results = pipeLine.Invoke();
}
Output("PipelineInvoked", dtStartStep);
}
finally
{
pipeLine = null;
if (null != runspace)
runspace.Close();
runspace.Dispose();
runspace = null;
}

dtStartStep = DateTime.Now;
string result = "";
foreach (MshObject obj in results)
{
result = obj.ToString();
}
Output("ResultsRetrieved", dtStartStep);
Output("Total", dtStart);

Console.WriteLine(string.Format("\nget-Date result:\t{0}",
result));
Console.WriteLine("press any key to end...");
Console.ReadKey();
}

private static void Output(string stepName, DateTime dtStartStep)
{
TimeSpan tsStep = DateTime.Now - dtStartStep;
int seconds = (int)tsStep.TotalMilliseconds;
Output(stepName + ":", seconds.ToString());
}

private static void Output(string stepName, string data)
{
if (stepName.Length < 8)
Console.WriteLine(string.Format("{0}\t\t\t\t{1,11}",
stepName, data));
else if (stepName.Length < 16)
Console.WriteLine(string.Format("{0}\t\t\t{1,11}", stepName,
data));
else if (stepName.Length < 24)
Console.WriteLine(string.Format("{0}\t\t{1,11}", stepName,
data));
else
Console.WriteLine(string.Format("{0}\t{1,11}", stepName,
data));
}
}
}
 

My Computer

?

=?Utf-8?B?S2FybCBQcm9zc2Vy?=

#3
it looks like you are using a very old version of powershell, actually before
powershell was powershell back in the monad 3 betas when it was called MSH?

my only thoughts is that slowdown in compiling the Pcode for powershell to
native code, which normally happens the first time code in a referenced
assembly is run. so you can predo it but doing some strategic references
earlier, and of course you could NGEN it beforehand

but i could be barking up the wrong tree also

Karl
 

My Computer

?

=?Utf-8?B?ZGg=?=

#4
Exchange 2007 is not yet upgraded to the latest PowerShell release, I think
they are calling their version RC0. So that's what I'm stuck with until
Exchange 2007 comes out with their release cand. which is supposed to sync up
with a newer PowerShell.

I will give ngen a try, but my impression is that seems to only contribute a
few seconds. I'm guessing the same delay I see starting up a PowerShell or
Monad command shell, initalizing runspace etc., is what I am seeing everytime
I debug. I communicated with an Exchange guy and he said to talk to the
PowerShell folks. Hopefully I can get some recommendations from the PS team.

thanks,

dh
 

My Computer

?

=?Utf-8?B?S2FybCBQcm9zc2Vy?=

#5
so if you are not debugging , then it works fine?


"dh" wrote:

> Exchange 2007 is not yet upgraded to the latest PowerShell release, I think
> they are calling their version RC0. So that's what I'm stuck with until
> Exchange 2007 comes out with their release cand. which is supposed to sync up
> with a newer PowerShell.
>
> I will give ngen a try, but my impression is that seems to only contribute a
> few seconds. I'm guessing the same delay I see starting up a PowerShell or
> Monad command shell, initalizing runspace etc., is what I am seeing everytime
> I debug. I communicated with an Exchange guy and he said to talk to the
> PowerShell folks. Hopefully I can get some recommendations from the PS team.
>
> thanks,
>
> dh
 

My Computer

J

Jeffrey Snover [MSFT]

#6

My Computer

?

=?Utf-8?B?ZGg=?=

#7
After more investigation it looks like the performance problem I described
may be mostly due to running the test on a virtual server. When I have
access to a physical server with Exch2007 installed I will run the same tests
again for comparison. Then I will also look at the impact of NGen.

thanks for your help,
-dh
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)