Windows Vista Forums

Slow performance initializing PowerShell from managed code

  1. #1


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

    Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec

  2. #2


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

    RE: Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec

  3. #3


    =?Utf-8?B?S2FybCBQcm9zc2Vy?= Guest

    RE: Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec

  4. #4


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

    RE: Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec

  5. #5


    =?Utf-8?B?S2FybCBQcm9zc2Vy?= Guest

    RE: Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec

  6. #6


    Jeffrey Snover [MSFT] Guest

    Re: Slow performance initializing PowerShell from managed code

    Do the NGEN and let us know what effect that has. If that is the issue - it
    can be quite noticable.

    --
    Jeffrey Snover [MSFT]
    Windows PowerShell/Aspen Architect
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, no confers rights.
    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx



      My System SpecsSystem Spec

  7. #7


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

    Re: Slow performance initializing PowerShell from managed code

    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 System SpecsSystem Spec


Slow performance initializing PowerShell from managed code
Similar Threads
Thread Forum
Managed/unmanaged code exceptions handling tool (C# / ++) .NET General
Managed Folders will not be renamed until your system administrator runs the managed folder assistant. SBS Server
OS=Windows Vista Ultimate x64 In PowerShell Managed Reference (Win PowerShell
Exporting Managed Code as Flat API to Unmanaged World .NET General
Base Filtering Engine in Vista causing very slow DNS lookups and slow internet performance Vista security