ic-mops 0.31.1 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.ts +14 -0
- package/commands/bench/bench-canister.mo +87 -0
- package/commands/bench/user-bench.mo +14 -0
- package/commands/bench.ts +335 -0
- package/commands/publish.ts +4 -0
- package/commands/sources.ts +4 -4
- package/declarations/bench/bench.did +26 -0
- package/declarations/bench/bench.did.d.ts +26 -0
- package/declarations/bench/bench.did.js +26 -0
- package/declarations/bench/index.d.ts +50 -0
- package/declarations/bench/index.js +41 -0
- package/dist/cli.js +13 -0
- package/dist/commands/bench/bench-canister.mo +87 -0
- package/dist/commands/bench/user-bench.mo +14 -0
- package/dist/commands/bench.d.ts +11 -0
- package/dist/commands/bench.js +275 -0
- package/dist/commands/publish.js +4 -0
- package/dist/commands/sources.d.ts +2 -1
- package/dist/commands/sources.js +4 -4
- package/dist/declarations/bench/bench.did +26 -0
- package/dist/declarations/bench/bench.did.d.ts +26 -0
- package/dist/declarations/bench/bench.did.js +26 -0
- package/dist/declarations/bench/index.d.ts +50 -0
- package/dist/declarations/bench/index.js +41 -0
- package/dist/helpers/get-dfx-version.d.ts +1 -0
- package/dist/helpers/get-dfx-version.js +9 -0
- package/dist/helpers/get-moc-path.d.ts +1 -0
- package/dist/helpers/get-moc-path.js +11 -0
- package/dist/helpers/get-moc-version.d.ts +1 -0
- package/dist/helpers/get-moc-version.js +7 -0
- package/dist/package.json +81 -80
- package/dist/resolve-packages.js +1 -1
- package/dist/vessel.d.ts +2 -1
- package/dist/vessel.js +9 -7
- package/helpers/get-dfx-version.ts +10 -0
- package/helpers/get-moc-path.ts +12 -0
- package/helpers/get-moc-version.ts +8 -0
- package/package.json +81 -80
- package/resolve-packages.ts +1 -1
- package/tsconfig.json +2 -0
- package/vessel.ts +10 -8
package/cli.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {bump} from './commands/bump.js';
|
|
|
24
24
|
import {sync} from './commands/sync.js';
|
|
25
25
|
import {outdated} from './commands/outdated.js';
|
|
26
26
|
import {update} from './commands/update.js';
|
|
27
|
+
import {bench} from './commands/bench.js';
|
|
27
28
|
import {transferOwnership} from './commands/transfer-ownership.js';
|
|
28
29
|
// import {docs} from './commands/docs.js';
|
|
29
30
|
|
|
@@ -198,6 +199,19 @@ program
|
|
|
198
199
|
await test(filter, options);
|
|
199
200
|
});
|
|
200
201
|
|
|
202
|
+
// bench
|
|
203
|
+
program
|
|
204
|
+
.command('bench [filter]')
|
|
205
|
+
.description('Run benchmarks')
|
|
206
|
+
.addOption(new Option('--save', 'Save benchmark results to .bench/<filename>.json'))
|
|
207
|
+
.addOption(new Option('--compare', 'Run benchmark and compare results with .bench/<filename>.json'))
|
|
208
|
+
.addOption(new Option('--gc <gc>', 'Garbage collector').choices(['copying', 'compacting', 'generational', 'incremental']).default('incremental'))
|
|
209
|
+
// .addOption(new Option('--force-gc', 'Force GC'))
|
|
210
|
+
.addOption(new Option('--verbose', 'Show more information'))
|
|
211
|
+
.action(async (filter, options) => {
|
|
212
|
+
await bench(filter, options);
|
|
213
|
+
});
|
|
214
|
+
|
|
201
215
|
// template
|
|
202
216
|
program
|
|
203
217
|
.command('template')
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import Nat64 "mo:base/Nat64";
|
|
2
|
+
import Nat "mo:base/Nat";
|
|
3
|
+
import Debug "mo:base/Debug";
|
|
4
|
+
import ExperimentalInternetComputer "mo:base/ExperimentalInternetComputer";
|
|
5
|
+
import Prim "mo:prim";
|
|
6
|
+
import Bench "mo:bench";
|
|
7
|
+
|
|
8
|
+
import UserBench "./user-bench";
|
|
9
|
+
|
|
10
|
+
actor class() {
|
|
11
|
+
var benchOpt : ?Bench.Bench = null;
|
|
12
|
+
|
|
13
|
+
public func init() : async Bench.BenchSchema {
|
|
14
|
+
let bench = UserBench.init();
|
|
15
|
+
benchOpt := ?bench;
|
|
16
|
+
bench.getSchema();
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
public query func getSchema() : async Bench.BenchSchema {
|
|
20
|
+
let ?bench = benchOpt else Debug.trap("bench not initialized");
|
|
21
|
+
bench.getSchema();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
func _getStats() : Bench.BenchResult {
|
|
25
|
+
{
|
|
26
|
+
instructions = 0;
|
|
27
|
+
rts_heap_size = Prim.rts_heap_size();
|
|
28
|
+
rts_memory_size = Prim.rts_memory_size();
|
|
29
|
+
rts_total_allocation = Prim.rts_total_allocation();
|
|
30
|
+
rts_mutator_instructions = Prim.rts_mutator_instructions();
|
|
31
|
+
rts_collector_instructions = Prim.rts_collector_instructions();
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
func _diffStats(before : Bench.BenchResult, after : Bench.BenchResult) : Bench.BenchResult {
|
|
36
|
+
{
|
|
37
|
+
instructions = after.instructions - before.instructions;
|
|
38
|
+
rts_heap_size = after.rts_heap_size - before.rts_heap_size;
|
|
39
|
+
rts_memory_size = after.rts_memory_size - before.rts_memory_size;
|
|
40
|
+
rts_total_allocation = after.rts_total_allocation - before.rts_total_allocation;
|
|
41
|
+
rts_mutator_instructions = after.rts_mutator_instructions - before.rts_mutator_instructions;
|
|
42
|
+
rts_collector_instructions = after.rts_collector_instructions - before.rts_collector_instructions;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
func _runCell(rowIndex : Nat, colIndex : Nat) : Bench.BenchResult {
|
|
47
|
+
let ?bench = benchOpt else Debug.trap("bench not initialized");
|
|
48
|
+
let statsBefore = _getStats();
|
|
49
|
+
|
|
50
|
+
let instructions = Nat64.toNat(ExperimentalInternetComputer.countInstructions(func() {
|
|
51
|
+
bench.runCell(rowIndex, colIndex);
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
let statsAfter = _getStats();
|
|
55
|
+
_diffStats(statsBefore, { statsAfter with instructions });
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
func _runCellAwait(rowIndex : Nat, colIndex : Nat) : async Bench.BenchResult {
|
|
59
|
+
let ?bench = benchOpt else Debug.trap("bench not initialized");
|
|
60
|
+
let statsBefore = _getStats();
|
|
61
|
+
|
|
62
|
+
let instructions = Nat64.toNat(ExperimentalInternetComputer.countInstructions(func() {
|
|
63
|
+
bench.runCell(rowIndex, colIndex);
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
await (func() : async () {})();
|
|
67
|
+
|
|
68
|
+
let statsAfter = _getStats();
|
|
69
|
+
_diffStats(statsBefore, { statsAfter with instructions });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
public query func getStats() : async Bench.BenchResult {
|
|
73
|
+
_getStats();
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
public query func runCellQuery(rowIndex : Nat, colIndex : Nat) : async Bench.BenchResult {
|
|
77
|
+
_runCell(rowIndex, colIndex);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
public func runCellUpdate(rowIndex : Nat, colIndex : Nat) : async Bench.BenchResult {
|
|
81
|
+
_runCell(rowIndex, colIndex);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
public func runCellUpdateAwait(rowIndex : Nat, colIndex : Nat) : async Bench.BenchResult {
|
|
85
|
+
await _runCellAwait(rowIndex, colIndex);
|
|
86
|
+
};
|
|
87
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Nat "mo:base/Nat";
|
|
2
|
+
import Iter "mo:base/Iter";
|
|
3
|
+
import Buffer "mo:base/Buffer";
|
|
4
|
+
import Vector "mo:vector/Class";
|
|
5
|
+
import Bench "mo:bench";
|
|
6
|
+
|
|
7
|
+
// placeholder file that will be replaced with the *.bench.mo file
|
|
8
|
+
module {
|
|
9
|
+
public func init() : Bench.Bench {
|
|
10
|
+
let bench = Bench.Bench();
|
|
11
|
+
// benchmark code goes here...
|
|
12
|
+
bench;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import {execSync} from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
import {globSync} from 'glob';
|
|
7
|
+
import {markdownTable} from 'markdown-table';
|
|
8
|
+
import logUpdate from 'log-update';
|
|
9
|
+
|
|
10
|
+
import {getRootDir} from '../mops.js';
|
|
11
|
+
import {parallel} from '../parallel.js';
|
|
12
|
+
import {createActor} from '../declarations/bench/index.js';
|
|
13
|
+
import {BenchResult, BenchSchema, _SERVICE} from '../declarations/bench/bench.did.js';
|
|
14
|
+
import {absToRel} from './test/utils.js';
|
|
15
|
+
import {getMocVersion} from '../helpers/get-moc-version.js';
|
|
16
|
+
import {getDfxVersion} from '../helpers/get-dfx-version.js';
|
|
17
|
+
import {getMocPath} from '../helpers/get-moc-path.js';
|
|
18
|
+
import {sources} from './sources.js';
|
|
19
|
+
import {execaCommand} from 'execa';
|
|
20
|
+
|
|
21
|
+
let ignore = [
|
|
22
|
+
'**/node_modules/**',
|
|
23
|
+
'**/.mops/**',
|
|
24
|
+
'**/.vessel/**',
|
|
25
|
+
'**/.git/**',
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
let globConfig = {
|
|
29
|
+
nocase: true,
|
|
30
|
+
ignore: ignore,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type BenchOptions = {
|
|
34
|
+
dfx?: string,
|
|
35
|
+
moc?: string,
|
|
36
|
+
gc?: 'copying' | 'compacting' | 'generational' | 'incremental',
|
|
37
|
+
forceGc?: boolean,
|
|
38
|
+
save?: boolean,
|
|
39
|
+
compare?: boolean,
|
|
40
|
+
verbose?: boolean,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export async function bench(filter = '', options: BenchOptions = {}): Promise<boolean> {
|
|
44
|
+
let defaultOptions: BenchOptions = {
|
|
45
|
+
moc: getMocVersion(),
|
|
46
|
+
dfx: getDfxVersion(),
|
|
47
|
+
gc: 'incremental',
|
|
48
|
+
forceGc: true,
|
|
49
|
+
save: false,
|
|
50
|
+
compare: false,
|
|
51
|
+
verbose: false,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
options = {...defaultOptions, ...options};
|
|
55
|
+
|
|
56
|
+
options.verbose && console.log(options);
|
|
57
|
+
|
|
58
|
+
let rootDir = getRootDir();
|
|
59
|
+
let globStr = '**/bench?(mark)/**/*.bench.mo';
|
|
60
|
+
if (filter) {
|
|
61
|
+
globStr = `**/bench?(mark)/**/*${filter}*.mo`;
|
|
62
|
+
}
|
|
63
|
+
let files = globSync(path.join(rootDir, globStr), globConfig);
|
|
64
|
+
if (!files.length) {
|
|
65
|
+
if (filter) {
|
|
66
|
+
console.log(`No benchmark files found for filter '${filter}'`);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
console.log('No *.bench.mo files found');
|
|
70
|
+
console.log('Put your benchmark code in \'bench\' directory in *.bench.mo files');
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
files.sort();
|
|
75
|
+
|
|
76
|
+
let benchDir = `${getRootDir()}/.mops/.bench/`;
|
|
77
|
+
fs.rmSync(benchDir, {recursive: true, force: true});
|
|
78
|
+
fs.mkdirSync(benchDir, {recursive: true});
|
|
79
|
+
|
|
80
|
+
console.log('Benchmark files:');
|
|
81
|
+
for (let file of files) {
|
|
82
|
+
console.log(chalk.gray(`• ${absToRel(file)}`));
|
|
83
|
+
}
|
|
84
|
+
console.log('');
|
|
85
|
+
console.log('='.repeat(50));
|
|
86
|
+
console.log('');
|
|
87
|
+
|
|
88
|
+
console.log('Starting dfx replica...');
|
|
89
|
+
startDfx(options.verbose);
|
|
90
|
+
|
|
91
|
+
console.log('Deploying canisters...');
|
|
92
|
+
await parallel(os.cpus().length, files, async (file: string) => {
|
|
93
|
+
try {
|
|
94
|
+
await deployBenchFile(file, options);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
console.error('Unexpected error. Stopping dfx replica...');
|
|
98
|
+
stopDfx(options.verbose);
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
await parallel(1, files, async (file: string) => {
|
|
104
|
+
console.log('\n' + '—'.repeat(50));
|
|
105
|
+
console.log(`\nRunning ${chalk.gray(absToRel(file))}...`);
|
|
106
|
+
console.log('');
|
|
107
|
+
try {
|
|
108
|
+
await runBenchFile(file, options);
|
|
109
|
+
}
|
|
110
|
+
catch (err) {
|
|
111
|
+
console.error('Unexpected error. Stopping dfx replica...');
|
|
112
|
+
stopDfx(options.verbose);
|
|
113
|
+
throw err;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
console.log('Stopping dfx replica...');
|
|
118
|
+
stopDfx(options.verbose);
|
|
119
|
+
|
|
120
|
+
fs.rmSync(benchDir, {recursive: true, force: true});
|
|
121
|
+
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function getMocArgs(options: BenchOptions): string {
|
|
126
|
+
let args = '';
|
|
127
|
+
if (options.forceGc) {
|
|
128
|
+
args += ' --force-gc';
|
|
129
|
+
}
|
|
130
|
+
if (options.gc) {
|
|
131
|
+
args += ` --${options.gc}-gc`;
|
|
132
|
+
}
|
|
133
|
+
return args;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function dfxJson(canisterName: string, options: BenchOptions = {}) {
|
|
137
|
+
options || console.log(options);
|
|
138
|
+
|
|
139
|
+
let canisters: Record<string, any> = {};
|
|
140
|
+
if (canisterName) {
|
|
141
|
+
canisters[canisterName] = {
|
|
142
|
+
type: 'custom',
|
|
143
|
+
wasm: 'canister.wasm',
|
|
144
|
+
candid: 'canister.did',
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
version: 1,
|
|
150
|
+
canisters,
|
|
151
|
+
defaults: {
|
|
152
|
+
build: {
|
|
153
|
+
packtool: 'mops sources',
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
networks: {
|
|
157
|
+
local: {
|
|
158
|
+
type: 'ephemeral',
|
|
159
|
+
bind: '127.0.0.1:4944',
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function startDfx(verbose = false) {
|
|
166
|
+
stopDfx(verbose);
|
|
167
|
+
let dir = path.join(getRootDir(), '.mops/.bench');
|
|
168
|
+
fs.writeFileSync(path.join(dir, 'dfx.json'), JSON.stringify(dfxJson(''), null, 2));
|
|
169
|
+
execSync('dfx start --background --clean' + (verbose ? '' : ' -qqqq'), {cwd: dir, stdio: ['inherit', verbose ? 'inherit' : 'ignore', 'inherit']});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function stopDfx(verbose = false) {
|
|
173
|
+
let dir = path.join(getRootDir(), '.mops/.bench');
|
|
174
|
+
execSync('dfx stop' + (verbose ? '' : ' -qqqq'), {cwd: dir, stdio: ['pipe', verbose ? 'inherit' : 'ignore', 'pipe']});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function deployBenchFile(file: string, options: BenchOptions = {}): Promise<void> {
|
|
178
|
+
let rootDir = getRootDir();
|
|
179
|
+
let tempDir = path.join(rootDir, '.mops/.bench/', path.parse(file).name);
|
|
180
|
+
let canisterName = path.parse(file).name;
|
|
181
|
+
|
|
182
|
+
// prepare temp files
|
|
183
|
+
fs.mkdirSync(tempDir, {recursive: true});
|
|
184
|
+
fs.writeFileSync(path.join(tempDir, 'dfx.json'), JSON.stringify(dfxJson(canisterName, options), null, 2));
|
|
185
|
+
fs.cpSync(new URL('./bench/bench-canister.mo', import.meta.url), path.join(tempDir, 'canister.mo'));
|
|
186
|
+
fs.cpSync(file, path.join(tempDir, 'user-bench.mo'));
|
|
187
|
+
|
|
188
|
+
// build canister
|
|
189
|
+
let mocPath = getMocPath();
|
|
190
|
+
let mocArgs = getMocArgs(options);
|
|
191
|
+
options.verbose && console.time(`build ${canisterName}`);
|
|
192
|
+
await execaCommand(`${mocPath} -c --idl canister.mo ${mocArgs} ${(await sources({cwd: tempDir})).join(' ')}`, {cwd: tempDir, stdio: options.verbose ? 'pipe' : ['pipe', 'ignore', 'pipe']});
|
|
193
|
+
options.verbose && console.timeEnd(`build ${canisterName}`);
|
|
194
|
+
|
|
195
|
+
// deploy canister
|
|
196
|
+
options.verbose && console.time(`deploy ${canisterName}`);
|
|
197
|
+
await execaCommand(`dfx deploy ${canisterName} --mode reinstall --yes --identity anonymous`, {cwd: tempDir, stdio: options.verbose ? 'pipe' : ['pipe', 'ignore', 'pipe']});
|
|
198
|
+
options.verbose && console.timeEnd(`deploy ${canisterName}`);
|
|
199
|
+
|
|
200
|
+
// init bench
|
|
201
|
+
options.verbose && console.time(`init ${canisterName}`);
|
|
202
|
+
let canisterId = execSync(`dfx canister id ${canisterName}`, {cwd: tempDir}).toString().trim();
|
|
203
|
+
let actor: _SERVICE = await createActor(canisterId, {
|
|
204
|
+
agentOptions: {
|
|
205
|
+
host: 'http://127.0.0.1:4944',
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
await actor.init();
|
|
209
|
+
options.verbose && console.timeEnd(`init ${canisterName}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
type RunBenchFileResult = {
|
|
213
|
+
schema: BenchSchema,
|
|
214
|
+
results: Map<string, BenchResult>,
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
async function runBenchFile(file: string, options: BenchOptions = {}): Promise<RunBenchFileResult> {
|
|
218
|
+
let rootDir = getRootDir();
|
|
219
|
+
let tempDir = path.join(rootDir, '.mops/.bench/', path.parse(file).name);
|
|
220
|
+
let canisterName = path.parse(file).name;
|
|
221
|
+
|
|
222
|
+
let canisterId = execSync(`dfx canister id ${canisterName}`, {cwd: tempDir}).toString().trim();
|
|
223
|
+
let actor: _SERVICE = await createActor(canisterId, {
|
|
224
|
+
agentOptions: {
|
|
225
|
+
host: 'http://127.0.0.1:4944',
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
let schema = await actor.getSchema();
|
|
230
|
+
|
|
231
|
+
// load previous results
|
|
232
|
+
let prevResults: Map<string, BenchResult> | undefined;
|
|
233
|
+
let resultsJsonFile = path.join(rootDir, '.bench', `${path.parse(file).name}.json`);
|
|
234
|
+
if (options.compare) {
|
|
235
|
+
if (fs.existsSync(resultsJsonFile)) {
|
|
236
|
+
let prevResultsJson = JSON.parse(fs.readFileSync(resultsJsonFile).toString());
|
|
237
|
+
prevResults = new Map(prevResultsJson.results);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
console.log(chalk.yellow(`No previous results found "${resultsJsonFile}"`));
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
let results = new Map<string, BenchResult>();
|
|
245
|
+
|
|
246
|
+
let formatNumber = (n: bigint | number): string => {
|
|
247
|
+
return n.toLocaleString('en-US').replaceAll(',', '_');
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
let getTable = (prop: keyof BenchResult): string => {
|
|
251
|
+
let resArr = [['', ...schema.cols]];
|
|
252
|
+
|
|
253
|
+
for (let [_rowIndex, row] of schema.rows.entries()) {
|
|
254
|
+
let curRow = [row];
|
|
255
|
+
|
|
256
|
+
for (let [_colIndex, col] of schema.cols.entries()) {
|
|
257
|
+
let res = results.get(`${row}:${col}`);
|
|
258
|
+
if (res) {
|
|
259
|
+
|
|
260
|
+
// compare with previous results
|
|
261
|
+
let diff = '';
|
|
262
|
+
if (options.compare && prevResults) {
|
|
263
|
+
let prevRes = prevResults.get(`${row}:${col}`);
|
|
264
|
+
if (prevRes) {
|
|
265
|
+
let percent = (Number(res[prop]) - Number(prevRes[prop])) / Number(prevRes[prop]) * 100;
|
|
266
|
+
let sign = percent > 0 ? '+' : '';
|
|
267
|
+
let percentText = percent == 0 ? '0%' : sign + percent.toFixed(2) + '%';
|
|
268
|
+
// diff = ' (' + (percent > 0 ? chalk.red(percentText) : chalk.green(percentText)) + ')'; // alignment is broken
|
|
269
|
+
diff = ' (' + percentText + ')';
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
diff = chalk.yellow(' (no previous results)');
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// add to table
|
|
277
|
+
curRow.push(formatNumber(res[prop]) + diff);
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
curRow.push('');
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
resArr.push(curRow);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return markdownTable(resArr, {align: ['l', ...'r'.repeat(schema.cols.length)]});
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
let printResults = () => {
|
|
290
|
+
logUpdate(`
|
|
291
|
+
\n${chalk.bold(schema.name)}
|
|
292
|
+
${schema.description ? '\n' + chalk.gray(schema.description) : ''}
|
|
293
|
+
\n\n${chalk.blue('Instructions')}\n\n${getTable('instructions')}
|
|
294
|
+
\n\n${chalk.blue('Heap')}\n\n${getTable('rts_heap_size')}
|
|
295
|
+
`);
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
printResults();
|
|
299
|
+
|
|
300
|
+
// run all cells
|
|
301
|
+
for (let [rowIndex, row] of schema.rows.entries()) {
|
|
302
|
+
for (let [colIndex, col] of schema.cols.entries()) {
|
|
303
|
+
// let res = await actor.runCellQuery(BigInt(rowIndex), BigInt(colIndex));
|
|
304
|
+
// let res = await actor.runCellUpdate(BigInt(rowIndex), BigInt(colIndex));
|
|
305
|
+
let res = await actor.runCellUpdateAwait(BigInt(rowIndex), BigInt(colIndex));
|
|
306
|
+
results.set(`${row}:${col}`, res);
|
|
307
|
+
printResults();
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
logUpdate.done();
|
|
311
|
+
|
|
312
|
+
// save results
|
|
313
|
+
if (options.save) {
|
|
314
|
+
console.log(`Saving results to ${chalk.gray(absToRel(resultsJsonFile))}`);
|
|
315
|
+
let json: Record<any, any> = {
|
|
316
|
+
version: 1,
|
|
317
|
+
moc: options.moc,
|
|
318
|
+
dfx: options.dfx,
|
|
319
|
+
gc: options.gc,
|
|
320
|
+
forceGc: options.forceGc,
|
|
321
|
+
results: Array.from(results.entries()),
|
|
322
|
+
};
|
|
323
|
+
fs.mkdirSync(path.dirname(resultsJsonFile), {recursive: true});
|
|
324
|
+
fs.writeFileSync(resultsJsonFile, JSON.stringify(json, (_, val) => {
|
|
325
|
+
if (typeof val === 'bigint') {
|
|
326
|
+
return Number(val);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
return val;
|
|
330
|
+
}
|
|
331
|
+
}, 2));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return {schema, results};
|
|
335
|
+
}
|
package/commands/publish.ts
CHANGED
|
@@ -197,6 +197,10 @@ export async function publish(options: {docs?: boolean, test?: boolean} = {}) {
|
|
|
197
197
|
'!tests/**',
|
|
198
198
|
'!**/*.test.mo',
|
|
199
199
|
'!**/*.Test.mo',
|
|
200
|
+
'!bench/**',
|
|
201
|
+
'!benchmark/**',
|
|
202
|
+
'!**/*.bench.mo',
|
|
203
|
+
'!**/*.Bench.mo',
|
|
200
204
|
];
|
|
201
205
|
let files = config.package.files || ['**/*.mo'];
|
|
202
206
|
files = [...files, ...defaultFiles];
|
package/commands/sources.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {checkConfigFile, formatDir, formatGithubDir, getDependencyType, readConf
|
|
|
4
4
|
import {resolvePackages} from '../resolve-packages.js';
|
|
5
5
|
|
|
6
6
|
// TODO: resolve conflicts
|
|
7
|
-
export async function sources({verbose = false} = {}) {
|
|
7
|
+
export async function sources({verbose = false, cwd = process.cwd()} = {}) {
|
|
8
8
|
if (!checkConfigFile()) {
|
|
9
9
|
return [];
|
|
10
10
|
}
|
|
@@ -17,13 +17,13 @@ export async function sources({verbose = false} = {}) {
|
|
|
17
17
|
|
|
18
18
|
let pkgDir;
|
|
19
19
|
if (depType === 'local') {
|
|
20
|
-
pkgDir = path.relative(
|
|
20
|
+
pkgDir = path.relative(cwd, version);
|
|
21
21
|
}
|
|
22
22
|
else if (depType === 'github') {
|
|
23
|
-
pkgDir = path.relative(
|
|
23
|
+
pkgDir = path.relative(cwd, formatGithubDir(name, version));
|
|
24
24
|
}
|
|
25
25
|
else if (depType === 'mops') {
|
|
26
|
-
pkgDir = path.relative(
|
|
26
|
+
pkgDir = path.relative(cwd, formatDir(name, version));
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
29
|
return;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
type anon_class_10_1 =
|
|
2
|
+
service {
|
|
3
|
+
getSchema: () -> (BenchSchema) query;
|
|
4
|
+
getStats: () -> (BenchResult) query;
|
|
5
|
+
init: () -> (BenchSchema);
|
|
6
|
+
runCellQuery: (nat, nat) -> (BenchResult) query;
|
|
7
|
+
runCellUpdate: (nat, nat) -> (BenchResult);
|
|
8
|
+
runCellUpdateAwait: (nat, nat) -> (BenchResult);
|
|
9
|
+
};
|
|
10
|
+
type BenchSchema =
|
|
11
|
+
record {
|
|
12
|
+
cols: vec text;
|
|
13
|
+
description: text;
|
|
14
|
+
name: text;
|
|
15
|
+
rows: vec text;
|
|
16
|
+
};
|
|
17
|
+
type BenchResult =
|
|
18
|
+
record {
|
|
19
|
+
instructions: int;
|
|
20
|
+
rts_collector_instructions: int;
|
|
21
|
+
rts_heap_size: int;
|
|
22
|
+
rts_memory_size: int;
|
|
23
|
+
rts_mutator_instructions: int;
|
|
24
|
+
rts_total_allocation: int;
|
|
25
|
+
};
|
|
26
|
+
service : () -> anon_class_10_1
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Principal } from '@dfinity/principal';
|
|
2
|
+
import type { ActorMethod } from '@dfinity/agent';
|
|
3
|
+
|
|
4
|
+
export interface BenchResult {
|
|
5
|
+
'instructions' : bigint,
|
|
6
|
+
'rts_memory_size' : bigint,
|
|
7
|
+
'rts_total_allocation' : bigint,
|
|
8
|
+
'rts_collector_instructions' : bigint,
|
|
9
|
+
'rts_mutator_instructions' : bigint,
|
|
10
|
+
'rts_heap_size' : bigint,
|
|
11
|
+
}
|
|
12
|
+
export interface BenchSchema {
|
|
13
|
+
'cols' : Array<string>,
|
|
14
|
+
'name' : string,
|
|
15
|
+
'rows' : Array<string>,
|
|
16
|
+
'description' : string,
|
|
17
|
+
}
|
|
18
|
+
export interface anon_class_10_1 {
|
|
19
|
+
'getSchema' : ActorMethod<[], BenchSchema>,
|
|
20
|
+
'getStats' : ActorMethod<[], BenchResult>,
|
|
21
|
+
'init' : ActorMethod<[], BenchSchema>,
|
|
22
|
+
'runCellQuery' : ActorMethod<[bigint, bigint], BenchResult>,
|
|
23
|
+
'runCellUpdate' : ActorMethod<[bigint, bigint], BenchResult>,
|
|
24
|
+
'runCellUpdateAwait' : ActorMethod<[bigint, bigint], BenchResult>,
|
|
25
|
+
}
|
|
26
|
+
export interface _SERVICE extends anon_class_10_1 {}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const idlFactory = ({ IDL }) => {
|
|
2
|
+
const BenchSchema = IDL.Record({
|
|
3
|
+
'cols' : IDL.Vec(IDL.Text),
|
|
4
|
+
'name' : IDL.Text,
|
|
5
|
+
'rows' : IDL.Vec(IDL.Text),
|
|
6
|
+
'description' : IDL.Text,
|
|
7
|
+
});
|
|
8
|
+
const BenchResult = IDL.Record({
|
|
9
|
+
'instructions' : IDL.Int,
|
|
10
|
+
'rts_memory_size' : IDL.Int,
|
|
11
|
+
'rts_total_allocation' : IDL.Int,
|
|
12
|
+
'rts_collector_instructions' : IDL.Int,
|
|
13
|
+
'rts_mutator_instructions' : IDL.Int,
|
|
14
|
+
'rts_heap_size' : IDL.Int,
|
|
15
|
+
});
|
|
16
|
+
const anon_class_10_1 = IDL.Service({
|
|
17
|
+
'getSchema' : IDL.Func([], [BenchSchema], ['query']),
|
|
18
|
+
'getStats' : IDL.Func([], [BenchResult], ['query']),
|
|
19
|
+
'init' : IDL.Func([], [BenchSchema], []),
|
|
20
|
+
'runCellQuery' : IDL.Func([IDL.Nat, IDL.Nat], [BenchResult], ['query']),
|
|
21
|
+
'runCellUpdate' : IDL.Func([IDL.Nat, IDL.Nat], [BenchResult], []),
|
|
22
|
+
'runCellUpdateAwait' : IDL.Func([IDL.Nat, IDL.Nat], [BenchResult], []),
|
|
23
|
+
});
|
|
24
|
+
return anon_class_10_1;
|
|
25
|
+
};
|
|
26
|
+
export const init = ({ IDL }) => { return []; };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ActorSubclass,
|
|
3
|
+
HttpAgentOptions,
|
|
4
|
+
ActorConfig,
|
|
5
|
+
Agent,
|
|
6
|
+
} from "@dfinity/agent";
|
|
7
|
+
import type { Principal } from "@dfinity/principal";
|
|
8
|
+
import type { IDL } from "@dfinity/candid";
|
|
9
|
+
|
|
10
|
+
import { _SERVICE } from './bench.did';
|
|
11
|
+
|
|
12
|
+
export declare const idlFactory: IDL.InterfaceFactory;
|
|
13
|
+
export declare const canisterId: string;
|
|
14
|
+
|
|
15
|
+
export declare interface CreateActorOptions {
|
|
16
|
+
/**
|
|
17
|
+
* @see {@link Agent}
|
|
18
|
+
*/
|
|
19
|
+
agent?: Agent;
|
|
20
|
+
/**
|
|
21
|
+
* @see {@link HttpAgentOptions}
|
|
22
|
+
*/
|
|
23
|
+
agentOptions?: HttpAgentOptions;
|
|
24
|
+
/**
|
|
25
|
+
* @see {@link ActorConfig}
|
|
26
|
+
*/
|
|
27
|
+
actorOptions?: ActorConfig;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
|
|
32
|
+
* @constructs {@link ActorSubClass}
|
|
33
|
+
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
|
|
34
|
+
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
|
|
35
|
+
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
|
|
36
|
+
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
|
|
37
|
+
* @see {@link HttpAgentOptions}
|
|
38
|
+
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
|
|
39
|
+
* @see {@link ActorConfig}
|
|
40
|
+
*/
|
|
41
|
+
export declare const createActor: (
|
|
42
|
+
canisterId: string | Principal,
|
|
43
|
+
options?: CreateActorOptions
|
|
44
|
+
) => ActorSubclass<_SERVICE>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
|
|
48
|
+
* @constructs {@link ActorSubClass}
|
|
49
|
+
*/
|
|
50
|
+
export declare const bench: ActorSubclass<_SERVICE>;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Actor, HttpAgent } from "@dfinity/agent";
|
|
2
|
+
|
|
3
|
+
// Imports and re-exports candid interface
|
|
4
|
+
import { idlFactory } from "./bench.did.js";
|
|
5
|
+
export { idlFactory } from "./bench.did.js";
|
|
6
|
+
|
|
7
|
+
/* CANISTER_ID is replaced by webpack based on node environment
|
|
8
|
+
* Note: canister environment variable will be standardized as
|
|
9
|
+
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
|
|
10
|
+
* beginning in dfx 0.15.0
|
|
11
|
+
*/
|
|
12
|
+
export const canisterId =
|
|
13
|
+
process.env.CANISTER_ID_BENCH ||
|
|
14
|
+
process.env.BENCH_CANISTER_ID;
|
|
15
|
+
|
|
16
|
+
export const createActor = (canisterId, options = {}) => {
|
|
17
|
+
const agent = options.agent || new HttpAgent({ ...options.agentOptions });
|
|
18
|
+
|
|
19
|
+
if (options.agent && options.agentOptions) {
|
|
20
|
+
console.warn(
|
|
21
|
+
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Fetch root key for certificate validation during development
|
|
26
|
+
if (process.env.DFX_NETWORK !== "ic") {
|
|
27
|
+
agent.fetchRootKey().catch((err) => {
|
|
28
|
+
console.warn(
|
|
29
|
+
"Unable to fetch root key. Check to ensure that your local replica is running"
|
|
30
|
+
);
|
|
31
|
+
console.error(err);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Creates an actor with using the candid interface and the HttpAgent
|
|
36
|
+
return Actor.createActor(idlFactory, {
|
|
37
|
+
agent,
|
|
38
|
+
canisterId,
|
|
39
|
+
...options.actorOptions,
|
|
40
|
+
});
|
|
41
|
+
};
|