Skip to content

Commit

Permalink
bench: Add benchmarking for program binary size (#2591)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Aug 8, 2023
1 parent abfdc4e commit 4cf447a
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 442 deletions.
49 changes: 49 additions & 0 deletions bench/BINARY_SIZE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Binary Size

All notable changes in program binary size will be documented in this file.

The changes are calculated by comparing the current results with the last version's results. Increase in size is shown with 🔴 and decrease is shown with 🟢.

The programs and their tests are located in [/tests/bench](https://github.com/coral-xyz/anchor/tree/master/tests/bench).

> **Note**
> Results documented in this file are autogenerated. Running the tests will update the current results when necessary, manually editing the results should be avoided.
> **Warning**
> Results may vary depending on Solana version.
## [Unreleased]

Solana version: 1.16.0

| Program | Binary Size | +/- |
| ------- | ----------- | --- |
| bench | 1,153,736 | - |

### Notable changes

---

## [0.28.0]

Solana version: 1.16.0

| Program | Binary Size | +/- |
| ------- | ----------- | ---------------------- |
| bench | 1,153,736 | 🔴 **+35,000 (3.13%)** |

### Notable changes

- Upgrading Solana to `1.16`. The difference in binary size between `0.27.0` and `0.28.0` is the direct result of upgrading Solana version(both build tools and crates) ([#2512](https://github.com/coral-xyz/anchor/pull/2512)).

---

## [0.27.0]

Solana version: 1.14.16

| Program | Binary Size | +/- |
| ------- | ----------- | --- |
| bench | 1,118,736 | N/A |

---
522 changes: 261 additions & 261 deletions bench/COMPUTE_UNITS.md

Large diffs are not rendered by default.

11 changes: 10 additions & 1 deletion tests/bench/bench.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"0.27.0": {
"solanaVersion": "1.14.16",
"result": {
"binarySize": {
"bench": 1118736
},
"computeUnits": {
"accountInfo1": 954,
"accountInfo2": 1567,
Expand Down Expand Up @@ -96,6 +99,9 @@
"0.28.0": {
"solanaVersion": "1.16.0",
"result": {
"binarySize": {
"bench": 1153736
},
"computeUnits": {
"accountInfo1": 1015,
"accountInfo2": 1475,
Expand Down Expand Up @@ -190,6 +196,9 @@
"unreleased": {
"solanaVersion": "1.16.0",
"result": {
"binarySize": {
"bench": 1153736
},
"computeUnits": {
"accountInfo1": 1015,
"accountInfo2": 1475,
Expand Down Expand Up @@ -281,4 +290,4 @@
}
}
}
}
}
107 changes: 48 additions & 59 deletions tests/bench/scripts/sync-markdown.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,69 @@
/** Sync Markdown files in /bench based on the data from bench.json */

import { BenchData, Markdown } from "./utils";
import { BenchData, BenchResult, Markdown } from "./utils";

(async () => {
const bench = await BenchData.open();

await BenchData.forEachMarkdown((markdown, fileName) => {
if (fileName === "COMPUTE_UNITS.md") {
const versions = bench.getVersions();
const resultType = fileName
.toLowerCase()
.replace(".md", "")
.replace(/_\w/g, (match) => match[1].toUpperCase()) as keyof BenchResult;

// On the first version, compare with itself to update it with no changes
versions.unshift(versions[0]);
const versions = bench.getVersions();

for (const i in versions) {
const currentVersion = versions[i];
const nextVersion = versions[+i + 1];
// On the first version, compare with itself to update it with no changes
versions.unshift(versions[0]);

if (currentVersion === "unreleased") {
return;
}
for (const i in versions) {
const currentVersion = versions[i];
if (currentVersion === "unreleased") return;

const newData = bench.get(nextVersion);
const oldData = bench.get(currentVersion);
const nextVersion = versions[+i + 1];
const newData = bench.get(nextVersion);
const oldData = bench.get(currentVersion);

// Create table
const table = Markdown.createTable(
"Instruction",
"Compute Units",
"+/-"
);
// Create table
const table = Markdown.createTable();

bench.compareComputeUnits(
newData.result.computeUnits,
oldData.result.computeUnits,
({ ixName, newComputeUnits, oldComputeUnits }) => {
if (newComputeUnits === null) {
// Deleted instruction
return;
}
bench.compare({
newResult: newData.result[resultType],
oldResult: oldData.result[resultType],
changeCb: ({ name, newValue, oldValue }) => {
if (newValue === null) {
// Deleted key
return;
}

let changeText;
if (oldComputeUnits === null) {
// New instruction
changeText = "N/A";
} else {
const delta = newComputeUnits - oldComputeUnits;
const percentChange = (
(newComputeUnits / oldComputeUnits - 1) *
100
).toFixed(2);
let changeText: string;
if (oldValue === null) {
// New key
changeText = "N/A";
} else {
const delta = (newValue - oldValue).toLocaleString();
const percentChange = ((newValue / oldValue - 1) * 100).toFixed(2);

if (+percentChange > 0) {
changeText = `🔴 **+${delta} (${percentChange}%)**`;
} else {
changeText = `🟢 **${delta} (${percentChange.slice(1)}%)**`;
}
if (+percentChange > 0) {
changeText = `🔴 **+${delta} (${percentChange}%)**`;
} else {
changeText = `🟢 **${delta} (${percentChange.slice(1)}%)**`;
}

table.insert(ixName, newComputeUnits.toString(), changeText);
},
(ixName, computeUnits) => {
table.insert(
ixName,
computeUnits.toString(),
+i === 0 ? "N/A" : "-"
);
}
);

// Update version data
markdown.updateVersion({
version: nextVersion,
solanaVersion: newData.solanaVersion,
table,
});
}
table.insert(name, newValue.toLocaleString(), changeText);
},
noChangeCb: ({ name, value }) => {
table.insert(name, value.toLocaleString(), +i === 0 ? "N/A" : "-");
},
});

// Update version data
markdown.updateVersion({
version: nextVersion,
solanaVersion: newData.solanaVersion,
table,
});
}
});
})();
4 changes: 4 additions & 0 deletions tests/bench/scripts/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Toml,
VersionManager,
runAnchorTest,
spawn,
} from "./utils";

(async () => {
Expand Down Expand Up @@ -75,4 +76,7 @@ import {
return;
}
}

// Sync markdown files
spawn("anchor", ["run", "sync-markdown"]);
})();
Loading

1 comment on commit 4cf447a

@vercel
Copy link

@vercel vercel bot commented on 4cf447a Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.