|
1 | 1 | #nullable disable |
2 | | -using System; |
| 2 | +namespace Benchmark; |
| 3 | + |
3 | 4 | using System.IO; |
4 | | -using System.Linq; |
5 | 5 | using BenchmarkDotNet.Attributes; |
6 | 6 |
|
7 | | -namespace Benchmark |
| 7 | +[Config(typeof(BenchmarkConfig))] |
| 8 | +public class WritableOptionsBenchmark |
8 | 9 | { |
9 | | - [Config(typeof(BenchmarkConfig))] |
10 | | - public class WritableOptionsBenchmark |
| 10 | + public class SampleOption |
11 | 11 | { |
12 | | - public class SampleOption |
13 | | - { |
14 | | - public DateTime LastLaunchedAt { get; set; } |
15 | | - public string[] StringSettings { get; set; } |
16 | | - public int[] IntSettings { get; set; } |
17 | | - } |
| 12 | + public DateTime LastLaunchedAt { get; set; } |
| 13 | + public string[] StringSettings { get; set; } |
| 14 | + public int[] IntSettings { get; set; } |
| 15 | + } |
18 | 16 |
|
19 | | - private Awesome.Net.WritableOptions.WritableOptions<SampleOption> _awesomeWritableOptions; |
20 | | - private Nogic.WritableOptions.JsonWritableOptions<SampleOption> _myWritableOptions; |
21 | | - private SampleOption _option; |
22 | | - private string _jsonFilePath; |
| 17 | + private Awesome.Net.WritableOptions.WritableOptions<SampleOption> _awesomeWritableOptions; |
| 18 | + private Nogic.WritableOptions.JsonWritableOptions<SampleOption> _myWritableOptions; |
| 19 | + private SampleOption _option; |
| 20 | + private string _jsonFilePath; |
23 | 21 |
|
24 | | - [GlobalSetup] |
25 | | - public void GlobalSetup() => _option = new() |
26 | | - { |
27 | | - LastLaunchedAt = DateTime.Now, |
28 | | - StringSettings = Enumerable.Range(1, 100).Select((_) => Guid.NewGuid().ToString()).ToArray(), |
29 | | - IntSettings = Enumerable.Range(1, 100).ToArray() |
30 | | - }; |
| 22 | + [GlobalSetup] |
| 23 | + public void GlobalSetup() => _option = new() |
| 24 | + { |
| 25 | + LastLaunchedAt = DateTime.Now, |
| 26 | + StringSettings = Enumerable.Range(1, 100).Select((_) => Guid.NewGuid().ToString()).ToArray(), |
| 27 | + IntSettings = Enumerable.Range(1, 100).ToArray() |
| 28 | + }; |
31 | 29 |
|
32 | | - [IterationSetup] |
33 | | - public void ItarationSetup() |
34 | | - { |
35 | | - _jsonFilePath = Path.GetTempFileName(); |
36 | | - File.AppendAllText(_jsonFilePath, "{}"); |
37 | | - _awesomeWritableOptions = new(_jsonFilePath, nameof(SampleOption), null!, null); |
38 | | - _myWritableOptions = new(_jsonFilePath, nameof(SampleOption), null!, null); |
39 | | - } |
| 30 | + [IterationSetup] |
| 31 | + public void ItarationSetup() |
| 32 | + { |
| 33 | + _jsonFilePath = Path.GetTempFileName(); |
| 34 | + File.AppendAllText(_jsonFilePath, "{}"); |
| 35 | + _awesomeWritableOptions = new(_jsonFilePath, nameof(SampleOption), null!, null); |
| 36 | + _myWritableOptions = new(_jsonFilePath, nameof(SampleOption), null!, null); |
| 37 | + } |
40 | 38 |
|
41 | | - [Benchmark(Baseline = true)] |
42 | | - public void AwesomeWritableOptions_Update() |
| 39 | + [Benchmark(Baseline = true)] |
| 40 | + public void AwesomeWritableOptions_Update() |
| 41 | + { |
| 42 | + for (int i = 0; i < 1000; i++) |
43 | 43 | { |
44 | | - for (int i = 0; i < 1000; i++) |
| 44 | + _awesomeWritableOptions.Update(o => |
45 | 45 | { |
46 | | - _awesomeWritableOptions.Update(o => |
47 | | - { |
48 | | - o.LastLaunchedAt = _option.LastLaunchedAt; |
49 | | - o.StringSettings = _option.StringSettings; |
50 | | - o.IntSettings = _option.IntSettings; |
51 | | - }); |
52 | | - } |
| 46 | + o.LastLaunchedAt = _option.LastLaunchedAt; |
| 47 | + o.StringSettings = _option.StringSettings; |
| 48 | + o.IntSettings = _option.IntSettings; |
| 49 | + }); |
53 | 50 | } |
| 51 | + } |
54 | 52 |
|
55 | | - [Benchmark] |
56 | | - public void MyWritableOptions_Update() |
| 53 | + [Benchmark] |
| 54 | + public void MyWritableOptions_Update() |
| 55 | + { |
| 56 | + for (int i = 0; i < 1000; i++) |
57 | 57 | { |
58 | | - for (int i = 0; i < 1000; i++) |
59 | | - { |
60 | | - _myWritableOptions.Update(_option); |
61 | | - } |
| 58 | + _myWritableOptions.Update(_option); |
62 | 59 | } |
| 60 | + } |
63 | 61 |
|
64 | | - [IterationCleanup] |
65 | | - public void Teardown() |
66 | | - { |
67 | | - if (File.Exists(_jsonFilePath)) |
68 | | - File.Delete(_jsonFilePath); |
69 | | - } |
| 62 | + [IterationCleanup] |
| 63 | + public void Teardown() |
| 64 | + { |
| 65 | + if (File.Exists(_jsonFilePath)) |
| 66 | + File.Delete(_jsonFilePath); |
70 | 67 | } |
71 | 68 | } |
0 commit comments