| |
| |
|
|
| using System.Collections.Generic; |
| using System.Collections.ObjectModel; |
| using System.Management.Automation; |
| using System.Management.Automation.Runspaces; |
| using System.Runtime.InteropServices; |
| using BenchmarkDotNet.Attributes; |
| using MicroBenchmarks; |
|
|
| namespace Engine |
| { |
| [BenchmarkCategory(Categories.Engine, Categories.Public)] |
| public class Scripting |
| { |
| private Runspace runspace; |
| private ScriptBlock scriptBlock; |
|
|
| private void SetupRunspace() |
| { |
| |
| runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2()); |
| runspace.Open(); |
| Runspace.DefaultRunspace = runspace; |
| } |
|
|
| #region Invoke-Method |
|
|
| [ParamsSource(nameof(ValuesForScript))] |
| public string InvokeMethodScript { get; set; } |
|
|
| public IEnumerable<string> ValuesForScript() |
| { |
| yield return @"'String'.GetType()"; |
| yield return @"[System.IO.Path]::HasExtension('')"; |
|
|
| |
| if (Platform.IsWindows) |
| { |
| yield return @"$sh=New-Object -ComObject Shell.Application; $sh.Namespace('c:\')"; |
| yield return @"$fs=New-Object -ComObject scripting.filesystemobject; $fs.Drives"; |
| } |
| } |
|
|
| [GlobalSetup(Target = nameof(InvokeMethod))] |
| public void GlobalSetup() |
| { |
| SetupRunspace(); |
| scriptBlock = ScriptBlock.Create(InvokeMethodScript); |
|
|
| |
| |
| |
| |
| |
| |
| |
| scriptBlock.Invoke(); |
| } |
|
|
| [Benchmark] |
| public Collection<PSObject> InvokeMethod() |
| { |
| return scriptBlock.Invoke(); |
| } |
|
|
| #endregion |
|
|
| [GlobalCleanup] |
| public void GlobalCleanup() |
| { |
| runspace.Dispose(); |
| Runspace.DefaultRunspace = null; |
| } |
| } |
| } |
|
|