-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileCrudBenchmarks.cs
More file actions
166 lines (146 loc) · 6.18 KB
/
ProfileCrudBenchmarks.cs
File metadata and controls
166 lines (146 loc) · 6.18 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Logging.Abstractions;
using S7Tools.Core.Interfaces.Services;
using S7Tools.Core.Models;
using S7Tools.Core.Models.Configuration;
using S7Tools.Core.Services.Interfaces;
using S7Tools.Services;
namespace S7Tools.Benchmarks;
/// <summary>
/// Benchmarks for Profile CRUD operations to measure performance of profile management.
/// </summary>
[MemoryDiagnoser]
[SimpleJob(warmupCount: 3, iterationCount: 5)]
public class ProfileCrudBenchmarks
{
private ISerialPortProfileService _profileManager = null!;
private SerialPortProfile _testProfile = null!;
private int _createdProfileId;
/// <summary>
/// Sets up the benchmark environment before each iteration.
/// </summary>
[GlobalSetup]
public void Setup()
{
// Create mock path service for benchmarking
var mockPathService = new MockPathService();
// Create profile manager with null logger for benchmarking
_profileManager = new SerialPortProfileService(
NullLogger<SerialPortProfileService>.Instance,
mockPathService);
// Create a test profile
_testProfile = new SerialPortProfile
{
Name = "Benchmark Profile",
Description = "Profile for performance testing",
Configuration = new SerialPortConfiguration
{
BaudRate = 9600,
CharacterSize = 8,
ParityEnabled = false
}
};
}
/// <summary>
/// Cleans up after benchmarks complete.
/// </summary>
[GlobalCleanup]
public async Task Cleanup()
{
// Clean up any created profiles
try
{
IEnumerable<SerialPortProfile> profiles = await _profileManager.GetAllAsync();
foreach (SerialPortProfile profile in profiles)
{
await _profileManager.DeleteAsync(profile.Id);
}
}
catch
{
// Ignore cleanup errors
}
}
/// <summary>
/// Benchmarks profile creation performance.
/// </summary>
[Benchmark]
public async Task<SerialPortProfile> CreateProfile()
{
SerialPortProfile profile = await _profileManager.CreateAsync(_testProfile);
_createdProfileId = profile.Id;
return profile;
}
/// <summary>
/// Benchmarks profile retrieval by ID performance.
/// </summary>
[Benchmark]
public async Task<SerialPortProfile?> GetProfileById()
{
return await _profileManager.GetByIdAsync(_createdProfileId);
}
/// <summary>
/// Benchmarks retrieving all profiles performance.
/// </summary>
[Benchmark]
public async Task<IEnumerable<SerialPortProfile>> GetAllProfiles()
{
return await _profileManager.GetAllAsync();
}
/// <summary>
/// Benchmarks profile update performance.
/// </summary>
[Benchmark]
public async Task<SerialPortProfile> UpdateProfile()
{
SerialPortProfile? profile = await _profileManager.GetByIdAsync(_createdProfileId);
if (profile != null)
{
profile.Description = "Updated description";
return await _profileManager.UpdateAsync(profile);
}
return _testProfile;
}
/// <summary>
/// Benchmarks profile duplication performance.
/// </summary>
[Benchmark]
public async Task<SerialPortProfile> DuplicateProfile()
{
return await _profileManager.DuplicateAsync(_createdProfileId, "Duplicated Profile");
}
/// <summary>
/// Mock path service for benchmarking
/// </summary>
private class MockPathService : IPathService
{
private readonly string _tempDir = Path.Combine(Path.GetTempPath(), "S7Tools_Benchmarks");
public string BaseDirectory => _tempDir;
public string ResourcesDirectory => Path.Combine(_tempDir, "Resources");
public string AppSettingsPath => Path.Combine(_tempDir, "AppSettings", "AppSettings.json");
public string ProfilesDirectory => Path.Combine(ResourcesDirectory, "Profiles");
public string SerialProfilesPath => Path.Combine(ProfilesDirectory, "Serial", "SerialProfiles.json");
public string SocatProfilesPath => Path.Combine(ProfilesDirectory, "Socat", "SocatProfiles.json");
public string PowerSupplyProfilesPath => Path.Combine(ProfilesDirectory, "PowerSupply", "PowerSupplyProfiles.json");
public string MemoryRegionProfilesPath => Path.Combine(ProfilesDirectory, "MemoryRegion", "MemoryRegionProfiles.json");
public string PayloadSetProfilesPath => Path.Combine(ProfilesDirectory, "PayloadSets", "PayloadSetProfiles.json");
public string LogsDirectory => Path.Combine(ResourcesDirectory, "Logs");
public string MainLogsDirectory => Path.Combine(LogsDirectory, "Main");
public string ExportedLogsDirectory => Path.Combine(LogsDirectory, "Exported");
public string JobsPath => Path.Combine(ResourcesDirectory, "Jobs", "Jobs.json");
public string TasksPath => Path.Combine(ResourcesDirectory, "Tasks", "Tasks.json");
public string PayloadsDirectory => Path.Combine(ResourcesDirectory, "Payloads");
public string DumpsDirectory => Path.Combine(ResourcesDirectory, "Dumps");
public Task<PathConfiguration> InitializeAsync() => Task.FromResult(new PathConfiguration { BaseDirectory = _tempDir });
public Task<bool> EnsureDirectoryExistsAsync(string directoryPath)
{
Directory.CreateDirectory(directoryPath);
return Task.FromResult(true);
}
public string ResolvePath(string relativePath) => Path.Combine(_tempDir, relativePath);
public Task<PathValidationResult> ValidatePathsAsync() => Task.FromResult(new PathValidationResult { IsValid = true });
public string GetMainLogPath(int rollingNumber = 0) => Path.Combine(MainLogsDirectory, $"main_{rollingNumber}.log");
public string GetExportedLogPath(string format) => Path.Combine(ExportedLogsDirectory, $"export.{format.ToLowerInvariant()}");
public string GetResourcePath(params string[] pathComponents) => Path.Combine(new[] { ResourcesDirectory }.Concat(pathComponents).ToArray());
}
}