|
4 | 4 | using System.Globalization; |
5 | 5 | using System.IO; |
6 | 6 | using System.Linq; |
7 | | -using System.Text; |
8 | | -using System.Threading.Tasks; |
9 | 7 | using CsvHelper; |
| 8 | +using Microsoft.FSharp.Core; |
| 9 | +using Plotly.NET; |
| 10 | +using Plotly.NET.ImageExport; |
| 11 | +using Plotly.NET.LayoutObjects; |
| 12 | +using Chart = Plotly.NET.CSharp.Chart; |
10 | 13 |
|
11 | 14 | namespace BitFaster.Caching.ThroughputAnalysis |
12 | 15 | { |
@@ -67,5 +70,105 @@ public void ExportCsv(Mode mode, int cacheSize) |
67 | 70 | } |
68 | 71 | } |
69 | 72 | } |
| 73 | + |
| 74 | + public void ExportPlot(Mode mode, int cacheSize) |
| 75 | + { |
| 76 | + var columns = new List<string>(); |
| 77 | + |
| 78 | + for(int i = 1; i < resultTable.Columns.Count; i++) |
| 79 | + { |
| 80 | + columns.Add(resultTable.Columns[i].ColumnName); |
| 81 | + } |
| 82 | + |
| 83 | + List<GenericChart.GenericChart> charts = new List<GenericChart.GenericChart>(); |
| 84 | + |
| 85 | + foreach (DataRow row in resultTable.Rows) |
| 86 | + { |
| 87 | + var rowData = new List<double>(); |
| 88 | + string name = row[0].ToString(); |
| 89 | + for (var i = 1; i < resultTable.Columns.Count; i++) |
| 90 | + { |
| 91 | + rowData.Add(double.Parse(row[i].ToString()) * 1_000_000); |
| 92 | + } |
| 93 | + |
| 94 | + var chart = Chart.Line<string, double, string>(columns, rowData, Name: name, MarkerColor: MapColor(name)); |
| 95 | + charts.Add(chart); |
| 96 | + |
| 97 | + var combined = Chart.Combine(charts); |
| 98 | + |
| 99 | + combined |
| 100 | + .WithLayout(MapTitle(mode, cacheSize)) |
| 101 | + .WithoutVerticalGridlines() |
| 102 | + .WithAxisTitles("Number of threads", "Ops/sec") |
| 103 | + .SaveSVG($"Results_{mode}_{cacheSize}", Width: 1000, Height: 600); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public string MapTitle(Mode mode, int cacheSize) |
| 108 | + { |
| 109 | + switch (mode) |
| 110 | + { |
| 111 | + case Mode.Read: |
| 112 | + return $"Read throughput (100% cache hit) for size {cacheSize}"; |
| 113 | + case Mode.ReadWrite: |
| 114 | + return $"Read + Write throughput for size {cacheSize}"; |
| 115 | + case Mode.Update: |
| 116 | + return $"Update throughput for size {cacheSize}"; |
| 117 | + case Mode.Evict: |
| 118 | + return $"Eviction throughput (100% cache miss) for size {cacheSize}"; |
| 119 | + default: |
| 120 | + return $"{mode} {cacheSize}"; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public Color MapColor(string name) |
| 125 | + { |
| 126 | + switch (name) |
| 127 | + { |
| 128 | + case "ClassicLru": |
| 129 | + return Plotly.NET.Color.fromKeyword(Plotly.NET.ColorKeyword.Limegreen); |
| 130 | + case "MemryCache": |
| 131 | + return Plotly.NET.Color.fromKeyword(Plotly.NET.ColorKeyword.FireBrick); |
| 132 | + case "FsTConcLRU": |
| 133 | + return Plotly.NET.Color.fromKeyword(Plotly.NET.ColorKeyword.Silver); |
| 134 | + case "ConcurrLRU": |
| 135 | + return Plotly.NET.Color.fromKeyword(Plotly.NET.ColorKeyword.RoyalBlue); |
| 136 | + case "ConcurrLFU": |
| 137 | + return Plotly.NET.Color.fromRGB(255, 192, 0); |
| 138 | + default: |
| 139 | + return Plotly.NET.Color.fromKeyword(Plotly.NET.ColorKeyword.FireBrick); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static class PlotExt |
| 145 | + { |
| 146 | + public static GenericChart.GenericChart WithAxisTitles(this GenericChart.GenericChart chart, string xTitle, string yTitle) |
| 147 | + { |
| 148 | + var font = new FSharpOption<Font>(Font.init(Size: new FSharpOption<double>(16))); |
| 149 | + FSharpOption<string> xt = new FSharpOption<string>(xTitle); |
| 150 | + FSharpOption<string> yt = new FSharpOption<string>(yTitle); |
| 151 | + return chart.WithXAxisStyle(Title.init(xt, Font: font)).WithYAxisStyle(Title.init(yt, Font: font)); |
| 152 | + } |
| 153 | + |
| 154 | + public static GenericChart.GenericChart WithoutVerticalGridlines(this GenericChart.GenericChart chart) |
| 155 | + { |
| 156 | + var gridColor = new FSharpOption<Color>(Color.fromKeyword(ColorKeyword.Gainsboro)); |
| 157 | + var yaxis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>( |
| 158 | + GridColor: gridColor, |
| 159 | + ZeroLineColor: gridColor); |
| 160 | + |
| 161 | + var axis = LinearAxis.init<IConvertible, IConvertible, IConvertible, IConvertible, IConvertible, IConvertible>(ShowGrid: new FSharpOption<bool>(false)); |
| 162 | + return chart.WithXAxis(axis).WithYAxis(yaxis); |
| 163 | + } |
| 164 | + |
| 165 | + public static GenericChart.GenericChart WithLayout(this GenericChart.GenericChart chart, string title) |
| 166 | + { |
| 167 | + var font = new FSharpOption<Font>(Font.init(Size: new FSharpOption<double>(24))); |
| 168 | + FSharpOption<Title> t = Title.init(Text: title, X: 0.5, Font: font); |
| 169 | + FSharpOption<Color> plotBGColor = new FSharpOption<Color>(Color.fromKeyword(ColorKeyword.WhiteSmoke)); |
| 170 | + Layout layout = Layout.init<IConvertible>(PaperBGColor: plotBGColor, PlotBGColor: plotBGColor, Title: t); |
| 171 | + return chart.WithLayout(layout); |
| 172 | + } |
70 | 173 | } |
71 | 174 | } |
0 commit comments