sync-markdown.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /** Sync Markdown files in /bench based on the data from bench.json */
  2. import { BenchData, Markdown } from "./utils";
  3. (async () => {
  4. const bench = await BenchData.open();
  5. await BenchData.forEachMarkdown((markdown, fileName) => {
  6. if (fileName === "COMPUTE_UNITS.md") {
  7. const versions = bench.getVersions();
  8. // On the first version, compare with itself to update it with no changes
  9. versions.unshift(versions[0]);
  10. for (const i in versions) {
  11. const currentVersion = versions[i];
  12. const nextVersion = versions[+i + 1];
  13. if (currentVersion === "unreleased") {
  14. return;
  15. }
  16. const newComputeUnitsResult =
  17. bench.get(nextVersion).result.computeUnits;
  18. const oldComputeUnitsResult =
  19. bench.get(currentVersion).result.computeUnits;
  20. // Create table
  21. const table = Markdown.createTable(
  22. "Instruction",
  23. "Compute Units",
  24. "+/-"
  25. );
  26. bench.compareComputeUnits(
  27. newComputeUnitsResult,
  28. oldComputeUnitsResult,
  29. ({ ixName, newComputeUnits, oldComputeUnits }) => {
  30. if (newComputeUnits === null) {
  31. // Deleted instruction
  32. return;
  33. }
  34. let changeText;
  35. if (oldComputeUnits === null) {
  36. // New instruction
  37. changeText = "N/A";
  38. } else {
  39. const percentChange = (
  40. (newComputeUnits / oldComputeUnits - 1) *
  41. 100
  42. ).toFixed(2);
  43. if (+percentChange > 0) {
  44. changeText = `🔴 **+${percentChange}%**`;
  45. } else {
  46. changeText = `🟢 **${percentChange}%**`;
  47. }
  48. }
  49. table.insert(ixName, newComputeUnits.toString(), changeText);
  50. },
  51. (ixName, computeUnits) => {
  52. table.insert(
  53. ixName,
  54. computeUnits.toString(),
  55. +i === 0 ? "N/A" : "-"
  56. );
  57. }
  58. );
  59. // Update version's table
  60. markdown.updateTable(nextVersion, table);
  61. }
  62. }
  63. });
  64. })();