harness-visualization.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. (function() {
  2. // The benchmarks that we want to display, in the order they should appear
  3. // on the plot axis.
  4. var benchmarks = [
  5. 'New message', 'Populate fields',
  6. 'Populate fields with with',
  7. 'Encode binary', 'Decode binary',
  8. 'Encode JSON', 'Decode JSON',
  9. 'Encode text', 'Decode text',
  10. 'Equality',
  11. ];
  12. // The harnessSize keys we want to print in the summary table, in the order
  13. // they should be displayed.
  14. var harnessSizeKeys = ['Unstripped', 'Stripped'];
  15. // Common layout properties for the plot.
  16. var layout = {
  17. boxmode: 'group',
  18. xaxis: {
  19. showgrid: false,
  20. showline: false,
  21. autotick: true,
  22. ticks: 'outside',
  23. },
  24. yaxis: {
  25. title: 'Runtime (μs)',
  26. autorange: true,
  27. },
  28. margin: {
  29. l: 60,
  30. r: 60,
  31. t: 0,
  32. b: 60,
  33. },
  34. font: {
  35. family: 'Helvetica',
  36. },
  37. hovermode: 'closest',
  38. legend: {
  39. font: {
  40. size: 12,
  41. },
  42. yanchor: 'middle',
  43. xanchor: 'right',
  44. },
  45. };
  46. // Creates and return a series for the given language's results in a session.
  47. function createSeries(session, series) {
  48. var x = [];
  49. var y = [];
  50. // The x-axis is categorical over the benchmark names. Adding the same
  51. // benchmark multiple times will collapse all the points on the same
  52. // vertical, which is what we want.
  53. for (var i = 0; i < benchmarks.length; i++) {
  54. var benchmark = benchmarks[i];
  55. var timings = series.data[benchmark];
  56. if (timings) {
  57. for (var j = 0; j < timings.length; j++) {
  58. x.push(benchmark.replace(" ", "<br>"));
  59. y.push(timings[j]);
  60. }
  61. }
  62. }
  63. return {
  64. name: series.name,
  65. x: x,
  66. y: y,
  67. type: 'box',
  68. boxpoints: 'all',
  69. whiskerwidth: 0.5,
  70. pointpos: 0,
  71. jitter: 0.3,
  72. mode: 'marker',
  73. marker: {
  74. symbol: 'circle',
  75. size: 8,
  76. opacity: 0.6,
  77. },
  78. line: {
  79. width: 1,
  80. },
  81. };
  82. }
  83. // Computes and returns the median of the given array of values.
  84. function median(values) {
  85. values.sort(function(a,b){return a - b});
  86. var mid = Math.floor(values.length / 2);
  87. if (values.length % 2) {
  88. return values[mid];
  89. } else {
  90. return (values[mid - 1] + values[mid]) / 2.0;
  91. }
  92. }
  93. // Decorate a cell with the an appropriate background based
  94. // on the magnitude of the multiplier.
  95. function decorateMultiplierCell(cell, multiplier) {
  96. if (multiplier == 1) {
  97. // Decorate the best case with green
  98. cell.addClass('bg-success');
  99. } else if (multiplier < 3) {
  100. // < 3: Leave this cell white
  101. } else if (multiplier < 10) {
  102. // 3 - 10: Mark this cell yellow
  103. cell.addClass('bg-warning');
  104. } else {
  105. // > 10: Mark this cell red
  106. cell.addClass('bg-danger');
  107. }
  108. }
  109. // Creates and returns the summary table displayed next to the chart for a
  110. // given session.
  111. function createSummaryTable(session) {
  112. var table = $('<table></table>').addClass('table table-condensed numeric');
  113. var tbody = $('<tbody></tbody>').appendTo(table);
  114. // Insert the runtime stats.
  115. var header = $('<tr></tr>').appendTo(table);
  116. header.append($('<th>Median runtimes</th>'));
  117. for (var j = 0; j < session.series.length; j++) {
  118. header.append($('<th colspan="2"></th>').text(session.series[j].name));
  119. }
  120. for (var i = 0; i < benchmarks.length; i++) {
  121. var benchmark = benchmarks[i];
  122. var tr = $('<tr></tr>')
  123. table.append(tr);
  124. tr.append($('<td></td>').text(benchmark));
  125. // Compute the median time for each language,
  126. // Track which language was the fastest
  127. var timings = [];
  128. var bestLanguage = 0;
  129. for (var j = 0; j < session.series.length; j++) {
  130. var languageTimings = session.series[j].data[benchmark];
  131. if (languageTimings) {
  132. var med = median(languageTimings);
  133. timings.push(med);
  134. if (med < timings[bestLanguage]) {
  135. bestLanguage = j;
  136. }
  137. }
  138. }
  139. // Insert the per-language timings into the table
  140. var bestValue = timings[bestLanguage];
  141. for (var j = 0; j < session.series.length; j++) {
  142. var med = timings[j];
  143. var valueCell = $('<td></td>').appendTo(tr);
  144. var multiplierCell = $('<td></td>').appendTo(tr);
  145. if (med) {
  146. var formattedMedian = med.toFixed(3) + '&nbsp;&micro;s';
  147. valueCell.html(formattedMedian);
  148. var multiplier = med / bestValue;
  149. decorateMultiplierCell(valueCell, multiplier);
  150. if (j != bestLanguage) {
  151. multiplierCell.text('(' + multiplier.toFixed(1) + 'x)');
  152. }
  153. decorateMultiplierCell(multiplierCell, multiplier);
  154. }
  155. }
  156. }
  157. // Insert the binary size stats.
  158. header = $('<tr></tr>').appendTo(table);
  159. header.append($('<th>Harness size</th>'));
  160. for (var j = 0; j < session.series.length; j++) {
  161. header.append($('<th></th>'));
  162. header.append($('<th></th>'));
  163. }
  164. for (var i = 0; i < harnessSizeKeys.length; i++) {
  165. var harnessSizeKey = harnessSizeKeys[i];
  166. var tr = $('<tr></tr>')
  167. table.append(tr);
  168. tr.append($('<td></td>').text(harnessSizeKey));
  169. var bestLanguage = 0;
  170. var sizes = [];
  171. for (var j = 0; j < session.series.length; j++) {
  172. var size = session.series[j].data.harnessSize[harnessSizeKey];
  173. sizes.push(size);
  174. if (size < sizes[bestLanguage]) {
  175. bestLanguage = j;
  176. }
  177. }
  178. for (var j = 0; j < session.series.length; j++) {
  179. var size = sizes[j];
  180. var multiplier = size / sizes[bestLanguage];
  181. var formattedSize = size.toLocaleString() + '&nbsp;b';
  182. var valueCell = $('<td></td>').html(formattedSize).appendTo(tr);
  183. decorateMultiplierCell(valueCell, multiplier);
  184. var multiplierCell = $('<td></td>').appendTo(tr);
  185. if (j != bestLanguage) {
  186. multiplierCell.text('(' + multiplier.toFixed(1) + 'x)');
  187. }
  188. decorateMultiplierCell(multiplierCell, multiplier);
  189. }
  190. }
  191. var tfoot = $('<tfoot></tfoot>').appendTo(table);
  192. var footerRow = $('<tr></tr>').appendTo(tfoot);
  193. var colspan = 3 * session.series.length + 1;
  194. var footerCell =
  195. $('<td colspan="' + colspan + '"></td>').appendTo(footerRow);
  196. footerCell.text('Green highlights the best result for each test. ' +
  197. 'Multipliers indicate how much worse ' +
  198. '(slower/larger) a particular result is ' +
  199. 'compared to the best result.');
  200. return table;
  201. }
  202. $(function() {
  203. if (!window.sessions) {
  204. return;
  205. }
  206. // Iterate the sessions in reverse order so that the most recent ones
  207. // appear at the top. We create one chart for each session and tile them
  208. // down the page.
  209. for (var i = sessions.length - 1; i >= 0; i--) {
  210. var session = sessions[i];
  211. var allSeries = [];
  212. formattedDate =
  213. moment(new Date(session.date)).format('MMM Do h:mm:ss a');
  214. var title = session.type;
  215. var header = $('<h3></h3>').addClass('row').text(title);
  216. var subtitle = 'Working tree was at <tt>' + session.branch +
  217. '</tt>, commit <tt>' + session.commit.substr(0, 6) + '</tt>';
  218. if (session.uncommitted_changes) {
  219. subtitle += ' (with uncommited changes)';
  220. }
  221. subtitle += ' &ndash; ' + formattedDate;
  222. header.append($('<small></small>').html(subtitle));
  223. $('#container').append('<hr>');
  224. $('#container').append(header);
  225. var id = 'chart' + i;
  226. var row = $('<div></div>').addClass('row');
  227. var chartColumn = $('<div></div>').addClass('col-md-8');
  228. var tableColumn = $('<div></div>').addClass('col-md-4');
  229. row.append(chartColumn);
  230. row.append(tableColumn);
  231. $('#container').append(row);
  232. var chart = $('<div></div>').attr('id', id).addClass('chart');
  233. chartColumn.append(chart);
  234. for (var j = 0; j < session.series.length; j++) {
  235. var series = createSeries(session, session.series[j]);
  236. allSeries.push(series);
  237. }
  238. Plotly.newPlot(id, allSeries, layout, {
  239. displayModeBar: false,
  240. });
  241. var table = createSummaryTable(session);
  242. tableColumn.append(table);
  243. }
  244. window.onresize = function() {
  245. $('.chart').each(function() {
  246. Plotly.Plots.resize(this);
  247. });
  248. };
  249. });
  250. })();