|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Collections.ObjectModel; |
| 6 | +using System.Management.Automation; |
| 7 | +using System.Management.Automation.Runspaces; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using BenchmarkDotNet.Attributes; |
| 10 | +using MicroBenchmarks; |
| 11 | + |
| 12 | +namespace Engine |
| 13 | +{ |
| 14 | + [BenchmarkCategory(Categories.Engine, Categories.Public)] |
| 15 | + public class Scripting |
| 16 | + { |
| 17 | + private Runspace runspace; |
| 18 | + private ScriptBlock scriptBlock; |
| 19 | + |
| 20 | + private void SetupRunspace() |
| 21 | + { |
| 22 | + // Unless you want to run commands from any built-in modules, using 'CreateDefault2' is enough. |
| 23 | + runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2()); |
| 24 | + runspace.Open(); |
| 25 | + Runspace.DefaultRunspace = runspace; |
| 26 | + } |
| 27 | + |
| 28 | + #region Invoke-Method |
| 29 | + |
| 30 | + [ParamsSource(nameof(ValuesForScript))] |
| 31 | + public string InvokeMethodScript { get; set; } |
| 32 | + |
| 33 | + public IEnumerable<string> ValuesForScript() |
| 34 | + { |
| 35 | + yield return @"'String'.GetType()"; |
| 36 | + yield return @"[System.IO.Path]::HasExtension('')"; |
| 37 | + |
| 38 | + // Test on COM method invocation. |
| 39 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 40 | + { |
| 41 | + yield return @"$sh=New-Object -ComObject Shell.Application; $sh.Namespace('c:\')"; |
| 42 | + yield return @"$fs=New-Object -ComObject scripting.filesystemobject; $fs.Drives"; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + [GlobalSetup(Target = nameof(InvokeMethod))] |
| 47 | + public void GlobalSetup() |
| 48 | + { |
| 49 | + SetupRunspace(); |
| 50 | + scriptBlock = ScriptBlock.Create(InvokeMethodScript); |
| 51 | + |
| 52 | + // Run it once to get the C# code jitted and the script compiled. |
| 53 | + // The first call to this takes relatively too long, which makes the BDN's heuristic incorrectly |
| 54 | + // believe that there is no need to run many ops in each interation. However, the subsequent runs |
| 55 | + // of this method is much faster than the first run, and this causes 'MinIterationTime' warnings |
| 56 | + // to our benchmarks and make the benchmark results not reliable. |
| 57 | + // Calling this method once in 'GlobalSetup' is a workaround. |
| 58 | + // See https://github.com/dotnet/BenchmarkDotNet/issues/837#issuecomment-828600157 |
| 59 | + scriptBlock.Invoke(); |
| 60 | + } |
| 61 | + |
| 62 | + [Benchmark] |
| 63 | + public Collection<PSObject> InvokeMethod() |
| 64 | + { |
| 65 | + return scriptBlock.Invoke(); |
| 66 | + } |
| 67 | + |
| 68 | + #endregion |
| 69 | + |
| 70 | + [GlobalCleanup] |
| 71 | + public void GlobalCleanup() |
| 72 | + { |
| 73 | + runspace.Dispose(); |
| 74 | + Runspace.DefaultRunspace = null; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments