1
+ using BenchmarkDotNet . Exporters ;
2
+ using BenchmarkDotNet . Loggers ;
3
+ using BenchmarkDotNet . Reports ;
4
+
5
+ using System . Collections . Generic ;
6
+ using System . IO ;
7
+
8
+
9
+ public class HtmlSummaryExporter : IExporter
10
+ {
11
+ public string Name => nameof ( HtmlSummaryExporter ) ;
12
+
13
+ public void ExportToLog ( Summary summary , ILogger logger )
14
+ {
15
+
16
+ }
17
+
18
+ public IEnumerable < string > ExportToFiles ( Summary summary , ILogger consoleLogger )
19
+ {
20
+ string directoryPath = summary . ResultsDirectoryPath ;
21
+ string outputPath = Path . Combine ( directoryPath , "Summary.html" ) ;
22
+
23
+ var htmlFiles = Directory . GetFiles ( directoryPath , "*.html" ) ;
24
+
25
+ using ( StreamWriter writer = new StreamWriter ( outputPath ) )
26
+ {
27
+ writer . WriteLine ( "<html>" ) ;
28
+ writer . WriteLine ( "<head>" ) ;
29
+ writer . WriteLine ( "<title>Benchmark Summary</title>" ) ;
30
+
31
+ writer . WriteLine ( "<style>" ) ;
32
+ foreach ( var file in htmlFiles )
33
+ {
34
+ string content = File . ReadAllText ( file ) ;
35
+ string styleContent = GetStyleContent ( content ) ;
36
+ writer . WriteLine ( styleContent ) ;
37
+ }
38
+ writer . WriteLine ( "</style>" ) ;
39
+
40
+ writer . WriteLine ( "</head>" ) ;
41
+ writer . WriteLine ( "<body>" ) ;
42
+
43
+ foreach ( var file in htmlFiles )
44
+ {
45
+ string fileName = Path . GetFileName ( file ) ;
46
+ writer . WriteLine ( $ "<h2>{ fileName } </h2>") ;
47
+ string content = File . ReadAllText ( file ) ;
48
+ string bodyContent = GetBodyContent ( content ) ;
49
+ writer . WriteLine ( bodyContent ) ;
50
+ }
51
+
52
+ writer . WriteLine ( "</body>" ) ;
53
+ writer . WriteLine ( "</html>" ) ;
54
+ }
55
+
56
+ consoleLogger . WriteLine ( $ "Summary HTML file created successfully at { outputPath } .") ;
57
+
58
+ return new [ ] { outputPath } ;
59
+ }
60
+
61
+ private string GetBodyContent ( string html )
62
+ {
63
+ int bodyStartIndex = html . IndexOf ( "<body>" ) + "<body>" . Length ;
64
+ int bodyEndIndex = html . IndexOf ( "</body>" ) ;
65
+ if ( bodyStartIndex >= 0 && bodyEndIndex >= 0 )
66
+ {
67
+ return html . Substring ( bodyStartIndex , bodyEndIndex - bodyStartIndex ) ;
68
+ }
69
+ return string . Empty ;
70
+ }
71
+
72
+ private string GetStyleContent ( string html )
73
+ {
74
+ int styleStartIndex = html . IndexOf ( "<style>" ) + "<style>" . Length ;
75
+ int styleEndIndex = html . IndexOf ( "</style>" ) ;
76
+ if ( styleStartIndex >= 0 && styleEndIndex >= 0 )
77
+ {
78
+ return html . Substring ( styleStartIndex , styleEndIndex - styleStartIndex ) ;
79
+ }
80
+ return string . Empty ;
81
+ }
82
+ }
0 commit comments