Skip to content

Commit

Permalink
github markdown exporter for Disassembler, fixes #560
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsitnik committed Oct 15, 2018
1 parent 922dfff commit 1e62355
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet.Disassembler.x64/DataContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Asm : Code
/// The native end offset of this ASM representation
/// </summary>
public ulong EndAddress { get; set; }

public uint SizeInBytes { get; set; }
}

public class Map
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet.Disassembler.x64/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ static IEnumerable<Asm> GetAsm(ILToNativeMap map, State state, int depth, ClrMet
TextRepresentation = disasmBuffer.ToString(),
Comment = calledMethodName,
StartAddress = disasmAddress,
EndAddress = endOffset
EndAddress = endOffset,
SizeInBytes = (uint)(endOffset - disasmAddress)
};

if (endOffset >= map.EndAddress)
Expand Down
2 changes: 2 additions & 0 deletions src/BenchmarkDotNet/Diagnosers/CopiedDataContracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class Asm : Code
/// The native end offset of this ASM representation
/// </summary>
public ulong EndAddress { get; set; }

public uint SizeInBytes { get; set; }
}

public class Map
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Diagnosers/DisassemblyDiagnoser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private DisassemblyDiagnoser(WindowsDisassembler windowsDisassembler, MonoDisass
{
new CombinedDisassemblyExporter(results),
new RawDisassemblyExporter(results),
new PrettyDisassemblyExporter(results)
new PrettyHtmlDisassemblyExporter(results),
new PrettyGithubMarkdownDisassemblyExporter(results)
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using StreamWriter = BenchmarkDotNet.Portability.StreamWriter;

namespace BenchmarkDotNet.Exporters
{
public class PrettyGithubMarkdownDisassemblyExporter : IExporter
{
private readonly IReadOnlyDictionary<BenchmarkCase, DisassemblyResult> results;

public PrettyGithubMarkdownDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, DisassemblyResult> results) => this.results = results;

public string Name => nameof(PrettyGithubMarkdownDisassemblyExporter);

public void ExportToLog(Summary summary, ILogger logger) { }

public IEnumerable<string> ExportToFiles(Summary summary, ILogger consoleLogger)
=> summary.BenchmarksCases
.Where(results.ContainsKey)
.Select(benchmark => Export(summary, benchmark));

private string Export(Summary summary, BenchmarkCase benchmarkCase)
{
string filePath = $"{Path.Combine(summary.ResultsDirectoryPath, benchmarkCase.FolderInfo)}-asm.pretty.md";
if (File.Exists(filePath))
File.Delete(filePath);

using (var stream = StreamWriter.FromPath(filePath))
{
Export(new StreamLogger(stream), results[benchmarkCase], benchmarkCase);
}

return filePath;
}

private static void Export(ILogger logger, DisassemblyResult disassemblyResult, BenchmarkCase benchmarkCase)
{
int methodIndex = 0;
foreach (var method in disassemblyResult.Methods.Where(method => string.IsNullOrEmpty(method.Problem)))
{
logger.WriteLine("```assembly");
logger.WriteLine($"; {method.Name}");

var pretty = DisassemblyPrettifier.Prettify(method, $"M{methodIndex++:00}");

uint totalSizeInBytes = 0;
foreach (var element in pretty)
{
if (element is DisassemblyPrettifier.Label label)
{
logger.WriteLine($"{label.TextRepresentation}:");

continue;
}
if (element.Source is Asm asm)
{
totalSizeInBytes += asm.SizeInBytes;
}

logger.WriteLine($" {element.TextRepresentation}");
}

logger.WriteLine($"; Total bytes of code {totalSizeInBytes}");
logger.WriteLine("```");
}

foreach (var withProblems in disassemblyResult.Methods
.Where(method => !string.IsNullOrEmpty(method.Problem))
.GroupBy(method => method.Problem))
{
logger.WriteLine($"**{withProblems.Key}**");
foreach (var withProblem in withProblems)
{
logger.WriteLine(withProblem.Name);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@

namespace BenchmarkDotNet.Exporters
{
public class PrettyDisassemblyExporter : IExporter
public class PrettyHtmlDisassemblyExporter : IExporter
{
private static readonly Lazy<string> HighlightingLabelsScript = new Lazy<string>(() => ResourceHelper.LoadTemplate("highlightingLabelsScript.js"));

private readonly IReadOnlyDictionary<BenchmarkCase, DisassemblyResult> results;

public PrettyDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, DisassemblyResult> results) => this.results = results;
public PrettyHtmlDisassemblyExporter(IReadOnlyDictionary<BenchmarkCase, DisassemblyResult> results) => this.results = results;

public string Name => nameof(RawDisassemblyExporter);
public string Name => nameof(PrettyHtmlDisassemblyExporter);

public void ExportToLog(Summary summary, ILogger logger) { }

Expand Down

0 comments on commit 1e62355

Please sign in to comment.