Skip to content

Commit 9cb41df

Browse files
committed
add fibs
1 parent 603b0d0 commit 9cb41df

File tree

25 files changed

+725
-287
lines changed

25 files changed

+725
-287
lines changed

cli/bench_deploy.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env -S deno run --env-file --allow-all
2+
import { deploy } from '../tools/lib/index.ts'
3+
import { readBytecode } from '../utils/index.ts'
4+
5+
// Fibonacci
6+
7+
await deploy({
8+
name: { name: 'Fibonacci', mappedTo: 'FibonacciEvm' },
9+
args: [],
10+
})
11+
12+
await deploy({
13+
name: { name: 'Fibonacci', mappedTo: 'FibonacciPvm' },
14+
args: [],
15+
bytecodeType: 'polkavm',
16+
})
17+
18+
await deploy({
19+
name: { name: 'Fibonacci', mappedTo: 'FibonacciInk' },
20+
args: [],
21+
bytecode: readBytecode('./ink/fibonacci/target/ink/fibonacci.polkavm'),
22+
})
23+
24+
await deploy({
25+
name: { name: 'Fibonacci', mappedTo: 'FibonacciRust' },
26+
args: [],
27+
bytecode: readBytecode('./rust/contracts/fibonacci.polkavm'),
28+
})
29+
await deploy({
30+
name: { name: 'Fibonacci', mappedTo: 'FibonacciRustU128' },
31+
args: [],
32+
bytecode: readBytecode('./rust/contracts/fibonacci_u128.polkavm'),
33+
})
34+
await deploy({
35+
name: { name: 'Fibonacci', mappedTo: 'FibonacciRustU256' },
36+
args: [],
37+
bytecode: readBytecode('./rust/contracts/fibonacci_u256.polkavm'),
38+
})
39+
40+
// MyToken
41+
42+
await deploy({
43+
name: { name: 'MyToken', mappedTo: 'MyTokenEvm' },
44+
args: [],
45+
})
46+
47+
await deploy({
48+
name: { name: 'MyToken', mappedTo: 'MyTokenPvm' },
49+
args: [],
50+
bytecodeType: 'polkavm',
51+
})
52+
53+
await deploy({
54+
name: { name: 'MyToken', mappedTo: 'MyTokenInk' },
55+
args: [],
56+
bytecode: readBytecode('./ink/ink_erc20/target/ink/ink_erc20.polkavm'),
57+
})
58+
59+
await deploy({
60+
name: { name: 'MyToken', mappedTo: 'MyTokenRustWithAlloc' },
61+
args: [],
62+
bytecode: readBytecode('./rust/contracts/erc20_with_alloc.polkavm'),
63+
})
64+
65+
await deploy({
66+
name: { name: 'MyToken', mappedTo: 'MyTokenRustNoAlloc' },
67+
args: [],
68+
bytecode: readBytecode('./rust/contracts/erc20_no_alloc.polkavm'),
69+
})
Lines changed: 122 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import { env } from '../tools/lib/index.ts'
33
import { abis } from '../codegen/abis.ts'
44
import {
5+
FibonacciEvm,
6+
FibonacciInk,
7+
FibonacciPvm,
8+
FibonacciRust,
9+
FibonacciRustU128,
10+
FibonacciRustU256,
511
MyTokenEvm,
612
MyTokenInk,
713
MyTokenPvm,
@@ -28,12 +34,21 @@ const recipient: Hex = '0x3d26c9637dFaB74141bA3C466224C0DBFDfF4A63'
2834
}
2935
}
3036

31-
const addresses = [
32-
{ address: MyTokenEvm, name: 'EVM - solidity' },
33-
{ address: MyTokenPvm, name: 'PVM - solidity' },
34-
{ address: MyTokenInk, name: 'PVM - Ink!' },
35-
{ address: MyTokenRustWithAlloc, name: 'PVM - Rust with alloc' },
36-
{ address: MyTokenRustNoAlloc, name: 'PVM - Rust no alloc' },
37+
const tokenAddresses = [
38+
{ address: MyTokenEvm, name: 'Token - EVM - solidity' },
39+
{ address: MyTokenPvm, name: 'Token - PVM - solidity' },
40+
{ address: MyTokenInk, name: 'Token - PVM - Ink!' },
41+
{ address: MyTokenRustWithAlloc, name: 'Token - PVM - Rust with alloc' },
42+
{ address: MyTokenRustNoAlloc, name: 'Token - PVM - Rust no alloc' },
43+
] as const
44+
45+
const fibAddresses = [
46+
{ address: FibonacciEvm, name: 'Fibonacci - EVM - solidity' },
47+
{ address: FibonacciPvm, name: 'Fibonacci - PVM - solidity' },
48+
{ address: FibonacciRust, name: 'Fibonacci - PVM - Rust' },
49+
{ address: FibonacciRustU128, name: 'Fibonacci u128 - PVM - Rust' },
50+
{ address: FibonacciRustU256, name: 'Fibonacci u256 - PVM - Rust' },
51+
{ address: FibonacciInk, name: 'Fibonacci - PVM - Ink!' },
3752
] as const
3853

3954
type CodeSizeEntry = {
@@ -45,25 +60,39 @@ type StatEntry = {
4560
operation: string
4661
gas: bigint
4762
weight: { ref_time: bigint; proof_size: bigint }
63+
status: 'success' | 'reverted'
4864
}
4965

50-
const codeSizes: CodeSizeEntry[] = []
66+
const tokenCodeSizes: CodeSizeEntry[] = []
67+
const fibCodeSizes: CodeSizeEntry[] = []
5168
const stats: StatEntry[] = []
5269

53-
for (const { name, address } of addresses) {
70+
for (const { name, address } of tokenAddresses) {
71+
const code = await env.wallet.getCode({ address })
72+
if (!code) {
73+
console.error(`Deploy contract first with "deno task deploy"`)
74+
Deno.exit(1)
75+
}
76+
tokenCodeSizes.push({
77+
name,
78+
size: code.length / 2 - 1,
79+
})
80+
}
81+
82+
for (const { name, address } of fibAddresses) {
5483
const code = await env.wallet.getCode({ address })
5584
if (!code) {
5685
console.error(`Deploy contract first with "deno task deploy"`)
5786
Deno.exit(1)
5887
}
59-
codeSizes.push({
88+
fibCodeSizes.push({
6089
name,
6190
size: code.length / 2 - 1,
6291
})
6392
}
6493

6594
// mint token
66-
for (const { name, address } of addresses) {
95+
for (const { name, address } of tokenAddresses) {
6796
const { request } = await env.wallet.simulateContract({
6897
address,
6998
abi: abis.MyToken,
@@ -82,11 +111,12 @@ for (const { name, address } of addresses) {
82111
operation: `mint (${name})`,
83112
gas: receipt.gasUsed,
84113
weight,
114+
status: receipt.status,
85115
})
86116
}
87117

88118
// transfer token
89-
for (const { name, address } of addresses) {
119+
for (const { name, address } of tokenAddresses) {
90120
const { request } = await env.wallet.simulateContract({
91121
address,
92122
abi: abis.MyToken,
@@ -102,43 +132,72 @@ for (const { name, address } of addresses) {
102132
operation: `transfer (${name})`,
103133
gas: receipt.gasUsed,
104134
weight,
135+
status: receipt.status,
105136
})
106137
}
107138

108-
// Separate mint and transfer operations
139+
// fibonacci calls
140+
for (const { name, address } of fibAddresses) {
141+
const { request } = await env.wallet.simulateContract({
142+
address,
143+
abi: abis.Fibonacci,
144+
functionName: 'fibonacci',
145+
args: [20],
146+
})
147+
148+
const hash = await env.wallet.writeContract(request)
149+
const receipt = await env.wallet.waitForTransactionReceipt({ hash })
150+
const weight = await env.debugClient.postDispatchWeight(hash)
151+
152+
stats.push({
153+
operation: `fibonacci (${name})`,
154+
gas: receipt.gasUsed,
155+
weight,
156+
status: receipt.status,
157+
})
158+
}
159+
160+
// Separate mint, transfer, and fibonacci operations
109161
const mintStats = stats.filter((s) => s.operation.includes('mint'))
110162
const transferStats = stats.filter((s) => s.operation.includes('transfer'))
163+
const fibonacciStats = stats.filter((s) => s.operation.includes('fibonacci'))
111164

112165
function createOperationTable(operationStats: StatEntry[], title: string) {
113-
const minGas = operationStats.reduce(
166+
// Sort by ref_time (ascending)
167+
const sortedStats = [...operationStats].sort((a, b) =>
168+
Number(a.weight.ref_time - b.weight.ref_time)
169+
)
170+
171+
const minGas = sortedStats.reduce(
114172
(min, s) => s.gas < min ? s.gas : min,
115-
operationStats[0].gas,
173+
sortedStats[0].gas,
116174
)
117-
const minRefTime = operationStats.reduce(
175+
const minRefTime = sortedStats.reduce(
118176
(min, s) => s.weight.ref_time < min ? s.weight.ref_time : min,
119-
operationStats[0].weight.ref_time,
177+
sortedStats[0].weight.ref_time,
120178
)
121-
const minProofSize = operationStats.reduce(
179+
const minProofSize = sortedStats.reduce(
122180
(min, s) => s.weight.proof_size < min ? s.weight.proof_size : min,
123-
operationStats[0].weight.proof_size,
181+
sortedStats[0].weight.proof_size,
124182
)
125183

126184
const table = new Table({
127185
head: [
128186
'Implementation',
129-
'Gas Used',
130-
'Gas %',
187+
'Success',
131188
'Ref Time',
132189
'Ref Time %',
133190
'Proof Size',
134191
'Proof Size %',
192+
'Gas Used',
193+
'Gas %',
135194
],
136195
style: {
137196
head: ['cyan'],
138197
},
139198
})
140199

141-
for (const stat of operationStats) {
200+
for (const stat of sortedStats) {
142201
const gasPercent = ((Number(stat.gas) / Number(minGas)) * 100).toFixed(
143202
1,
144203
)
@@ -156,12 +215,13 @@ function createOperationTable(operationStats: StatEntry[], title: string) {
156215

157216
table.push([
158217
implName,
159-
stat.gas.toString(),
160-
`${gasPercent}%`,
218+
stat.status === 'success' ? '✓' : '✗',
161219
stat.weight.ref_time.toString(),
162220
`${refTimePercent}%`,
163221
stat.weight.proof_size.toString(),
164222
`${proofSizePercent}%`,
223+
stat.gas.toString(),
224+
`${gasPercent}%`,
165225
])
166226
}
167227

@@ -171,27 +231,58 @@ function createOperationTable(operationStats: StatEntry[], title: string) {
171231

172232
createOperationTable(mintStats, 'Mint Operation')
173233
createOperationTable(transferStats, 'Transfer Operation')
234+
createOperationTable(fibonacciStats, 'Fibonacci(20) Operation')
235+
236+
// Output token code size table
237+
const sortedTokenCodeSizes = [...tokenCodeSizes].sort((a, b) => a.size - b.size)
238+
239+
const minTokenCodeSize = sortedTokenCodeSizes.reduce(
240+
(min, c) => c.size < min ? c.size : min,
241+
sortedTokenCodeSizes[0].size,
242+
)
243+
244+
const tokenCodeSizeTable = new Table({
245+
head: ['Implementation', 'Code Size (bytes)', 'Size %'],
246+
style: {
247+
head: ['cyan'],
248+
},
249+
})
250+
251+
for (const entry of sortedTokenCodeSizes) {
252+
const sizePercent = ((entry.size / minTokenCodeSize) * 100).toFixed(1)
253+
tokenCodeSizeTable.push([
254+
entry.name,
255+
entry.size.toString(),
256+
`${sizePercent}%`,
257+
])
258+
}
259+
260+
console.log('\n## Token Code Size\n')
261+
console.log(tokenCodeSizeTable.toString())
262+
263+
// Output fibonacci code size table
264+
const sortedFibCodeSizes = [...fibCodeSizes].sort((a, b) => a.size - b.size)
174265

175-
// Output code size table
176-
const minCodeSize = codeSizes.reduce(
266+
const minFibCodeSize = sortedFibCodeSizes.reduce(
177267
(min, c) => c.size < min ? c.size : min,
178-
codeSizes[0].size,
268+
sortedFibCodeSizes[0].size,
179269
)
180270

181-
const codeSizeTable = new Table({
271+
const fibCodeSizeTable = new Table({
182272
head: ['Implementation', 'Code Size (bytes)', 'Size %'],
183273
style: {
184274
head: ['cyan'],
185275
},
186276
})
187277

188-
for (const entry of codeSizes) {
189-
const sizePercent = ((entry.size / minCodeSize) * 100).toFixed(1)
190-
codeSizeTable.push([
278+
for (const entry of sortedFibCodeSizes) {
279+
const sizePercent = ((entry.size / minFibCodeSize) * 100).toFixed(1)
280+
fibCodeSizeTable.push([
191281
entry.name,
192282
entry.size.toString(),
193283
`${sizePercent}%`,
194284
])
195285
}
196286

197-
console.log('\n' + codeSizeTable.toString())
287+
console.log('\n## Fibonacci Code Size\n')
288+
console.log(fibCodeSizeTable.toString())

cli/token_deploy.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)