-
Notifications
You must be signed in to change notification settings - Fork 273
/
Perf_Process.cs
84 lines (71 loc) · 2.5 KB
/
Perf_Process.cs
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace System.Diagnostics
{
[BenchmarkCategory(Categories.Libraries, Categories.NoWASM)]
public class Perf_Process
{
private readonly string _nonExistingName = Guid.NewGuid().ToString();
private int _currentProcessId;
[Benchmark]
public void GetCurrentProcess() => Process.GetCurrentProcess().Dispose();
[Benchmark]
public string GetCurrentProcessName()
{
using var process = Process.GetCurrentProcess();
return process.ProcessName;
}
[GlobalSetup(Target = nameof(GetProcessById))]
public void SetupGetProcessById() => _currentProcessId = Process.GetCurrentProcess().Id;
[Benchmark]
public void GetProcessById() => Process.GetProcessById(_currentProcessId).Dispose();
[Benchmark]
public void GetProcesses()
{
foreach (var process in Process.GetProcesses())
{
process.Dispose();
}
}
[Benchmark]
public void GetProcessesByName()
{
foreach (var process in Process.GetProcessesByName(_nonExistingName))
{
process.Dispose();
}
}
private static ProcessStartInfo s_startProcessStartInfo = new ProcessStartInfo() {
FileName = "whoami", // exists on both Windows and Unix, and has very short output
RedirectStandardOutput = true, // avoid visible output
UseShellExecute = false // required by Full Framework
};
private Process _startedProcess;
[Benchmark]
public void Start()
{
_startedProcess = Process.Start(s_startProcessStartInfo);
}
[IterationCleanup(Target = nameof(Start))]
public void CleanupStart()
{
if (_startedProcess != null)
{
_startedProcess.WaitForExit();
_startedProcess.Dispose();
_startedProcess = null;
}
}
[Benchmark]
public void StartAndWaitForExit()
{
using (Process p = Process.Start(s_startProcessStartInfo))
{
p.WaitForExit();
}
}
}
}