Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Hosting Powershell in app -- Question about Pipeline

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 01-09-2008   #1 (permalink)
thoward37
Guest


 

Hosting Powershell in app -- Question about Pipeline

Hi all,

I have a question regarding embedded Powershell within an
application.

I'm currently working on a rules engine which, as one of its rule
types, allows the user to use a Powershell script as a rule.

These rules are used in the context of applying flexible business
rules to the processing of large sets of data from a database, and get
executed at least once for each database row, for databases with row
counts of around 20 - 40 million rows. There may be multiple
Powershell rules per row (depending on the rule set).

So, it is in my interest to optimize the embedded Powershell runtime
as much as possible.

Currently, I do that by using a single instance of a Runspace, and
create a new Pipeline for each rule/script, Invoke it, and check it's
exit code for the rule's "result" which is then used by the calling
rule engine.

I would like to reuse the same Pipeline object, in order to reduce the
creation overhead associated with newing up that object for every rule
times every row of database I need to process... However after once
being invoked, there seems to be no way to reset a Pipeline object to
it's initial state (PipelineState.NotStarted).

Ideally, I would just change the script text and call invoke multiple
times on the same Pipeline object... But there doesn't seem to be any
way to do that.

All of this is in the goal of improving speed of the script evaluation
when called many times repeatedly. My current implementation executes
a simple script (a few lines doing basic comparison logic) in about 1
millisecond... Unfortunately, this is extremely slow compared to the
other rules types in the engine, which execute at roughly .001
milliseconds per rule (average performance). So, the Powershell rules,
at the moment are about 1000 times slower than the normal rules... I
don't expect to get that number to match the normal rules, but I would
like to improve it somewhat. I feel that if I didn't need to new up
the Pipeline objects I might be able to reduce the execution time by a
factor of 10.

Is there anyone out there who knows how I could do this, and if not,
do you have any ideas about how I could get around this issue?

Thanks,
Troy

My System SpecsSystem Spec
Old 01-10-2008   #2 (permalink)
Marco Shaw [MVP]
Guest


 

Re: Hosting Powershell in app -- Question about Pipeline

Quote:

> Is there anyone out there who knows how I could do this, and if not,
> do you have any ideas about how I could get around this issue?
>
> Thanks,
> Troy
I asked this of others...

Don Jones (MVP):

"You can reuse a runspace, but you're supposed to create a new pipeline
for each new command you want to run - just as the console
PowerShell.exe app does it. There's not that much overhead involved in
spinning up a pipeline; it's getting the shell itself loaded and
running initially that's the bear. PowerShell unfortunately isn't
speed-optimized at this point."

Karl Prosser (MVP):

"Each time you run something its a new pipeline.. what i would recommend
is batching things, but modifying the script...
basically take 100 or more objects at a time, have it the input object
to the pipeline, and have your cmdlet/function work asa pipeline,
running the rule and processing it.. this the code is only parsed one,
and its not jumping in and out of powershell.. but the prefiormance gain
will not to be too significant.... the question are you querying the
database for each item.. that is one sort of latency point.. depending
on the architecture there are many different things you can do to help
it be better, sometimes that might be using multiple runspaces to run
stuff in parralell, othertimes not.

but powershell really is the slowest kid on the block preformancewise"


--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 01-10-2008   #3 (permalink)
thoward37
Guest


 

Re: Hosting Powershell in app -- Question about Pipeline

On Jan 10, 1:29*pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com>
wrote:
Quote:
Quote:

> > Is there anyone out there who knows how I could do this, and if not,
> > do you have any ideas about how I could get around this issue?
>
Quote:

> > Thanks,
> > Troy
>
> I asked this of others...
>
> Don Jones (MVP):
>
> "You can reuse a runspace, but you're supposed to create a new pipeline
> for each new command you want to run - just as the console
> PowerShell.exe app does it. There's not that much overhead involved in
> spinning up a pipeline; it's getting the shell itself loaded and
> running initially that's the bear. PowerShell unfortunately isn't
> speed-optimized at this point."
>
> Karl Prosser (MVP):
>
> "Each time you run something its a new pipeline.. what i would recommend
> is batching things, but modifying the script...
> basically take 100 or more objects at a time, have it the input object
> to the pipeline, and have your cmdlet/function work asa pipeline,
> running the rule and processing it.. this the code is only parsed one,
> and its not jumping in and out of powershell.. but the prefiormance gain
> will not to be too significant.... the question are you querying the
> database for each item.. that is one sort of latency point.. depending
> on the architecture there are many different things you can do to help
> it be better, sometimes that might be using multiple runspaces to run
> stuff in parralell, othertimes not.
>
> but powershell really is the slowest kid on the block preformancewise"
>
> --
> Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp
>
> PowerGadgets MVPhttp://www.powergadgets.com/mvp
>
> Blog:http://marcoshaw.blogspot.com
Thanks alot for the quick response. This is kind of what I thought the
answer would be... I'll see if I can find some other ways of speeding
it up.

Well, since it's already there, I left the support for Powershell
scripting in, but I added support for C#, VB.NET, and JScript .NET as
well. For those I just use a CodeDom.CompilerProvider and generate
assemblies in memory from the user supplied source code.

That way, if someone really wants to use Powershell code, they can,
but it will be slow... If they want a faster script option they can
use one of the above three languages... A reasonable compromise, I
guess.

Thanks,
Troy
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Pipeline question RobertZ PowerShell 9 07-08-2008 02:53 PM
Hosting Powershell in App - Obtaining delegates to powershellfunctions thoward37 PowerShell 4 02-01-2008 01:26 PM
Hosting PowerShell & passing parameters to script Dmitry Naumov PowerShell 9 09-24-2007 03:34 AM
PowerShell hosting on the Internet Marco Shaw PowerShell 2 08-18-2007 01:04 PM
Ouch-No help for my question below Vista-Web Hosting-Please Read ladyesmile Vista General 3 06-26-2007 04:25 PM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51