Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/benchmarks/micro/libraries/System.Diagnostics/Perf_Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;

Expand Down Expand Up @@ -80,5 +82,47 @@ public void StartAndWaitForExit()
p.WaitForExit();
}
}

private static readonly ProcessStartInfo s_outputStartInfo = new ProcessStartInfo()
{
FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "cmd" : "sh",
Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? "/c for /L %i in (1,1,1000) do @echo Line %i"
: "-c \"for i in $(seq 1 1000); do echo Line $i; done\"",
RedirectStandardOutput = true,
UseShellExecute = false
};

private static readonly DataReceivedEventHandler s_ignoreOutputLine = static (sender, e) => { };

[Benchmark]
public void ReadOutputLineByLine()
{
using var process = new Process();
process.StartInfo = s_outputStartInfo;
process.OutputDataReceived += s_ignoreOutputLine;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
process.OutputDataReceived -= s_ignoreOutputLine;
}

#if NET
[Benchmark]
public async Task ReadOutputToEndAsync()
{
using Process process = Process.Start(s_outputStartInfo);
_ = await process.StandardOutput.ReadToEndAsync();
await process.WaitForExitAsync();
}
#endif

[Benchmark]
public void ReadOutputToEnd()
{
using Process process = Process.Start(s_outputStartInfo);
_ = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
}
}