sync-markdown.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 newData = bench.get(nextVersion);
  17. const oldData = bench.get(currentVersion);
  18. // Create table
  19. const table = Markdown.createTable(
  20. "Instruction",
  21. "Compute Units",
  22. "+/-"
  23. );
  24. bench.compareComputeUnits(
  25. newData.result.computeUnits,
  26. oldData.result.computeUnits,
  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 delta = newComputeUnits - oldComputeUnits;
  38. const percentChange = (
  39. (newComputeUnits / oldComputeUnits - 1) *
  40. 100
  41. ).toFixed(2);
  42. if (+percentChange > 0) {
  43. changeText = `🔴 **+${delta} (${percentChange}%)**`;
  44. } else {
  45. changeText = `🟢 **${delta} (${percentChange.slice(1)}%)**`;
  46. }
  47. }
  48. table.insert(ixName, newComputeUnits.toString(), changeText);
  49. },
  50. (ixName, computeUnits) => {
  51. table.insert(
  52. ixName,
  53. computeUnits.toString(),
  54. +i === 0 ? "N/A" : "-"
  55. );
  56. }
  57. );
  58. // Update version data
  59. markdown.updateVersion({
  60. version: nextVersion,
  61. solanaVersion: newData.solanaVersion,
  62. table,
  63. });
  64. }
  65. }
  66. });
  67. })();