Skip to content

Commit 2030596

Browse files
committed
fix: property type for '$.Benchmarks[].Measurements[].Nanoseconds'
1 parent b31ca4f commit 2030596

13 files changed

+1930
-5
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//"compare", "-f", "console", "-b", "../tests/test-data/report-02/Benchmark-report-full.json", "-t", "../tests/test-data/report-01/Benchmark-report-full.json", "-tm", "4%", "-ta", "5%"
1414
//"compare", "-f", "console", "-b", "../tests/test-data/report-01/Benchmark-report-full.json", "-t", "../tests/test-data/report-04/Benchmark-report-full.json"
1515
//"compare", "-f", "markdown", "-b", "../tests/test-data/report-01/Benchmark-report-full.json", "-t", "../tests/test-data/report-02/Benchmark-report-full.json", "-tm", "12ns", "-ta", "5%"
16-
"compare", "-f", "console", "-f", "markdown", "-b", "../tests/test-data/report-10", "-t", "../tests/test-data/report-11"
16+
"compare", "-f", "console", "-f", "markdown", "-b", "../tests/test-data/report-12", "-t", "../tests/test-data/report-11"
1717
],
1818
"cwd": "${workspaceFolder}/src",
1919
"console": "internalConsole",

docs/test-data.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@ Each test data folder typically contains the following files:
115115

116116
**Best for**: Testing complex comparisons, multi-file scenarios, memory allocation changes
117117

118+
---
119+
120+
### 📊 `report-12` - Multi-Benchmark Classes (Comparison)
121+
- **Files**:
122+
- `Demo.Benchmarks.ArrayProcessorBenchmarks-report-full.json`
123+
- `Demo.Benchmarks.StringProcessorBenchmarks-report-full.json`
124+
- **Environment**: Linux Ubuntu 24.04.2 LTS (Noble Numbat)
125+
126+
| Method | Mean | Allocated |
127+
|----------------|-----------|-----------|
128+
| GenerateArray | 11.266 μs | 21.516 KB |
129+
| GenerateString | 1.915 ms | 35.19 MB |
130+
131+
**Best for**: Testing decimal precision for $.Benchmarks[].Measurements[].Nanoseconds
132+
118133

119134

120135
## Sample Comparisons

src/Helpers/IOHelpers.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,17 @@ public static BenchmarkFullJsonResport[] ReadFullJsonReport(string? path)
3838
{
3939
var content = File.ReadAllText(paths[i]);
4040

41-
reports[i] = JsonSerializer.Deserialize<BenchmarkFullJsonResport>(content)
42-
?? throw new InvalidOperationException($"Failed to deserialize the {paths[i]} file.");
41+
try
42+
{
43+
reports[i] = JsonSerializer.Deserialize<BenchmarkFullJsonResport>(content)
44+
?? throw new InvalidOperationException($"Failed to deserialize the {paths[i]} file.");
45+
}
46+
catch (JsonException jsonException)
47+
{
48+
throw new InvalidOperationException(
49+
$"Failed to deserialize the file '{paths[i]}'. {jsonException.Message}",
50+
jsonException);
51+
}
4352

4453
reports[i].FilePath = Path.GetFullPath(paths[i]);
4554
reports[i].FileName = Path.GetFileName(paths[i]);

src/Models/FullJsonResport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public sealed class MeasurementRecord
127127
public int LaunchIndex { get; set; }
128128
public int IterationIndex { get; set; }
129129
public int Operations { get; set; }
130-
public int Nanoseconds { get; set; }
130+
public decimal Nanoseconds { get; set; }
131131
}
132132

133133
public sealed class MetricRecord

tests/PowerUtils.BenchmarkDotnet.Reporter.Tests/Helpers/IOHelpersTests/ReadFullJsonReportTest.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
using System;
12
using System.IO;
23
using System.Linq;
34
using PowerUtils.BenchmarkDotnet.Reporter.Helpers;
5+
using PowerUtils.BenchmarkDotnet.Reporter.Models;
46

57
namespace PowerUtils.BenchmarkDotnet.Reporter.Tests.Helpers.IOHelpersTests;
68

7-
public sealed class ReadFullJsonReportTest
9+
public sealed class ReadFullJsonReportTest : IDisposable
810
{
11+
private readonly string _tempDirectory;
12+
13+
14+
public ReadFullJsonReportTest()
15+
{
16+
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
17+
Directory.CreateDirectory(_tempDirectory);
18+
}
19+
20+
public void Dispose()
21+
{
22+
if(Directory.Exists(_tempDirectory))
23+
{
24+
Directory.Delete(_tempDirectory, true);
25+
}
26+
}
27+
928
[Fact]
1029
public void When_Pass_Valid_File_Should_Return_Report()
1130
{
@@ -37,4 +56,31 @@ public void When_Read_Report_Should_Contain_FilePath_And_FileName()
3756
act.FilePath.ShouldBe(path);
3857
act.FileName.ShouldBe(fileName);
3958
}
59+
60+
[Fact]
61+
public void When_File_With_Invalid_PropertyType_Should_Throw_InvalidOperationException_With_InnerException_JsonException()
62+
{
63+
// Arrange
64+
var filePath = Path.Combine(_tempDirectory, $"{Guid.NewGuid()}{IOHelpers.REPORT_FILE_ENDS}");
65+
File.WriteAllText(
66+
filePath,
67+
"""
68+
{
69+
"HostEnvironmentInfo":{
70+
"ChronometerFrequency":{
71+
"Hertz":"1000000000"
72+
}
73+
}
74+
}
75+
""");
76+
77+
78+
// Act
79+
BenchmarkFullJsonResport[] act() => IOHelpers.ReadFullJsonReport(filePath);
80+
81+
82+
// Assert
83+
Should.Throw<InvalidOperationException>(act)
84+
.Message.ShouldStartWith($"Failed to deserialize the file '{filePath}'. ");
85+
}
4086
}

0 commit comments

Comments
 (0)