-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathPerf_Parser_Options_Bare.cs
More file actions
68 lines (59 loc) · 2.58 KB
/
Perf_Parser_Options_Bare.cs
File metadata and controls
68 lines (59 loc) · 2.58 KB
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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.CommandLine.Benchmarks.Helpers;
using System.CommandLine.Parsing;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace System.CommandLine.Benchmarks.CommandLine
{
/// <summary>
/// Measures the performance of <see cref="CliParser"/> when parsing options without arguments.
/// </summary>
[BenchmarkCategory(Categories.CommandLine)]
public class Perf_Parser_Options_Bare
{
private IEnumerable<Option> _testSymbols;
private Command _testCommand;
private string _testSymbolsAsString;
private IEnumerable<Option> GenerateTestOptions(int count, ArgumentArity arity)
=> Enumerable.Range(0, count)
.Select(i =>
new Option<string>($"-option{i}")
{
Arity = arity,
Description = $"Description for -option {i} ...."
}
);
/// <remarks>
/// count=1 : cmd-root -option0
/// count=5 : cmd-root -option0 -option1 ... -option4
/// count=20 : cmd-root -option0 -option1 ... -option19
/// </remarks>
private string GenerateTestOptionsAsStringExpr(int count)
=> Enumerable.Range(0, count)
.Select(i => $"-option{i}")
.Aggregate("", (ac, next) => ac + " " + next);
[Params(1, 5, 20)]
public int TestSymbolsCount;
[GlobalSetup(Target = nameof(ParserFromOptions_Ctor))]
public void SetupTestOptions()
{
_testSymbols = GenerateTestOptions(TestSymbolsCount, ArgumentArity.Zero);
}
[Benchmark]
public Command ParserFromOptions_Ctor()
{
return _testSymbols.CreateConfiguration();
}
[GlobalSetup(Target = nameof(ParserFromOptions_Parse))]
public void SetupParserFromOptions_Parse()
{
var testSymbolsArr = GenerateTestOptions(TestSymbolsCount, ArgumentArity.Zero).ToArray();
_testCommand = testSymbolsArr.CreateConfiguration();
_testSymbolsAsString = GenerateTestOptionsAsStringExpr(testSymbolsArr.Length);
}
[Benchmark]
public ParseResult ParserFromOptions_Parse() => _testCommand.Parse(_testSymbolsAsString);
}
}