sync-markdown.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 = bench.get(nextVersion).computeUnits;
  17. const oldComputeUnitsResult = bench.get(currentVersion).computeUnits;
  18. // Create table
  19. const table = Markdown.createTable(
  20. "Instruction",
  21. "Compute Units",
  22. "+/-"
  23. );
  24. bench.compareComputeUnits(
  25. newComputeUnitsResult,
  26. oldComputeUnitsResult,
  27. ({ ixName, newComputeUnits, oldComputeUnits }) => {
  28. if (newComputeUnits === null) {
  29. // Deleted instruction
  30. return;
  31. }
  32. let changeText;
  33. if (oldComputeUnits === null) {
  34. // New instruction
  35. changeText = "N/A";
  36. } else {
  37. const percentChange = (
  38. (newComputeUnits / oldComputeUnits - 1) *
  39. 100
  40. ).toFixed(2);
  41. if (+percentChange > 0) {
  42. changeText = `🔴 **+${percentChange}%**`;
  43. } else {
  44. changeText = `🟢 **${percentChange}%**`;
  45. }
  46. }
  47. table.insert(ixName, newComputeUnits.toString(), changeText);
  48. },
  49. (ixName, computeUnits) => {
  50. table.insert(
  51. ixName,
  52. computeUnits.toString(),
  53. +i === 0 ? "N/A" : "-"
  54. );
  55. }
  56. );
  57. // Update version's table
  58. markdown.updateTable(nextVersion, table);
  59. }
  60. }
  61. });
  62. })();