Skip to content

Commit df5d64d

Browse files
authored
summarize methods (#96)
1 parent 4198273 commit df5d64d

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

BenchmarkDotNet.AsmSlicer/BenchMethodAsmWriter.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,36 @@
22

33
public class BenchMethodAsmWriter : IDisposable
44
{
5-
private readonly StreamWriter writer;
5+
private readonly StreamWriter asmWriter;
6+
private readonly StreamWriter summaryWriter;
7+
8+
private string? currentMethod;
9+
10+
private List<MethodSummary> methodSummaries;
611

712
// 1 .asm file per method name
813
public BenchMethodAsmWriter(string path, string? methodName)
914
{
10-
string filePath = Path.Combine(path, $"{DeNamespace(methodName)}-asm.md");
15+
this.currentMethod = methodName;
16+
17+
string asmFilePath = Path.Combine(path, $"{DeNamespace(methodName)}-asm.md");
18+
string summaryPath = Path.Combine(path, $"{DeNamespace(methodName)}-summary.md");
1119

1220
FileStreamOptions fileStreamOptions = new FileStreamOptions();
1321
fileStreamOptions.Access = FileAccess.Write;
1422
fileStreamOptions.Mode = FileMode.Create;
1523

16-
this.writer = new StreamWriter(filePath, fileStreamOptions);
17-
this.writer.WriteLine("```assembly");
18-
this.writer.WriteLine($"; {methodName}()"); // reconstruct
24+
this.asmWriter = new StreamWriter(asmFilePath, fileStreamOptions);
25+
this.summaryWriter = new StreamWriter(summaryPath, fileStreamOptions);
26+
27+
28+
this.asmWriter.WriteLine("```assembly");
29+
this.asmWriter.WriteLine($"; {methodName}()"); // reconstruct
30+
31+
this.summaryWriter.WriteLine("| # | Method | Size (bytes) |");
32+
this.summaryWriter.WriteLine("| -- | ----------- | ------------ |");
33+
34+
this.methodSummaries = new List<MethodSummary>();
1935
}
2036

2137
private static string? DeNamespace(string? methodName)
@@ -26,12 +42,39 @@ public BenchMethodAsmWriter(string path, string? methodName)
2642

2743
public void WriteLine(string line)
2844
{
29-
this.writer.WriteLine(line);
45+
if (line.StartsWith("; Total bytes of code"))
46+
{
47+
string size = line.Replace("; Total bytes of code ", string.Empty);
48+
this.methodSummaries.Add(new MethodSummary { Name = this.currentMethod, Size = size });
49+
}
50+
else if (line.StartsWith(";"))
51+
{
52+
this.currentMethod = line?.TrimStart(';', ' ').TrimEnd('(', ')');
53+
}
54+
55+
this.asmWriter.WriteLine(line);
3056
}
3157

3258
public void Dispose()
3359
{
34-
this.writer.Dispose();
60+
this.asmWriter.Dispose();
61+
WriteSummary();
62+
this.summaryWriter.Dispose();
63+
}
64+
65+
private void WriteSummary()
66+
{
67+
int count = 0;
68+
foreach (var m in this.methodSummaries.OrderBy(ms => ms.Name))
69+
{
70+
this.summaryWriter.WriteLine($"| {count++} | {m.Name} | {m.Size} |");
71+
}
72+
}
73+
74+
private record MethodSummary
75+
{
76+
public string? Name { get; init; }
77+
public string? Size { get; init; }
3578
}
3679
}
3780

0 commit comments

Comments
 (0)