Skip to content

Commit f3d8ba7

Browse files
authored
chore: add memory usage benchmark (#297)
Adds a simple benchmark for making graphs of memory usage during an import
1 parent 31980be commit f3d8ba7

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

benchmarks/memory/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Memory Benchmark
2+
3+
How much memory does the importer use while importing files?
4+
5+
It should be relatively flat to enable importing files larger than physical memory.
6+
7+
## Usage
8+
9+
```console
10+
$ npm i
11+
$ npm start
12+
13+
14+
> npm run build && node dist/src/index.js
15+
16+
17+
18+
> aegir build --bundle false
19+
20+
[14:51:28] tsc [started]
21+
[14:51:33] tsc [completed]
22+
generating Ed25519 keypair...
23+
┌─────────┬────────────────┬─────────┬───────────┬──────┐
24+
│ (index) │ Implementation │ ops/s │ ms/op │ runs │
25+
├─────────┼────────────────┼─────────┼───────────┼──────┤
26+
//... results here
27+
```

benchmarks/memory/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "ipfs-unixfs-memory-benchmark",
3+
"version": "0.0.0",
4+
"description": "Memory benchmarks for ipfs-unixfs-importer",
5+
"license": "Apache-2.0 OR MIT",
6+
"private": true,
7+
"type": "module",
8+
"types": "./dist/src/index.d.ts",
9+
"files": [
10+
"src",
11+
"dist",
12+
"!dist/test",
13+
"!**/*.tsbuildinfo"
14+
],
15+
"exports": {
16+
".": {
17+
"types": "./dist/src/index.d.ts",
18+
"import": "./dist/src/index.js"
19+
}
20+
},
21+
"eslintConfig": {
22+
"extends": "ipfs",
23+
"parserOptions": {
24+
"sourceType": "module"
25+
}
26+
},
27+
"scripts": {
28+
"build": "aegir build --bundle false",
29+
"clean": "aegir clean",
30+
"lint": "aegir lint",
31+
"dep-check": "aegir dep-check",
32+
"start": "npm run build && node --expose-gc ./dist/test/index.spec.js"
33+
},
34+
"devDependencies": {
35+
"aegir": "^38.1.2",
36+
"blockstore-fs": "^1.0.0",
37+
"ipfs-unixfs-importer": "../../packages/ipfs-unixfs-importer",
38+
"it-drain": "^2.0.1"
39+
}
40+
}

benchmarks/memory/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

benchmarks/memory/test/index.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable no-console */
2+
3+
import { importer } from 'ipfs-unixfs-importer'
4+
import path from 'node:path'
5+
import os from 'node:os'
6+
import fs from 'node:fs'
7+
import drain from 'it-drain'
8+
import { FsBlockstore } from 'blockstore-fs'
9+
10+
const ONE_MEG = 1024 * 1024
11+
12+
const FILE_SIZE = ONE_MEG * 1000
13+
const CHUNK_SIZE = ONE_MEG
14+
15+
async function main (): Promise<void> {
16+
const dir = path.join(os.tmpdir(), `test-${Date.now()}`)
17+
const blocks = new FsBlockstore(dir)
18+
await blocks.open()
19+
20+
console.info('bytes imported (mb), heap total (mb), heap used (mb), rss (mb)')
21+
22+
try {
23+
await drain(importer([{
24+
content: (async function * (): AsyncIterable<Uint8Array> {
25+
for (let i = 0; i < FILE_SIZE; i += CHUNK_SIZE) {
26+
yield new Uint8Array(CHUNK_SIZE)
27+
28+
// @ts-expect-error only present when node is run with --expose-gc
29+
global.gc()
30+
31+
console.info(`${i / ONE_MEG}, ${process.memoryUsage().heapTotal / ONE_MEG}, ${process.memoryUsage().heapUsed / ONE_MEG}, ${process.memoryUsage().rss / ONE_MEG}`)
32+
}
33+
})()
34+
}], blocks))
35+
} catch (err) {
36+
console.error(err)
37+
} finally {
38+
await blocks.close()
39+
fs.rmSync(dir, {
40+
recursive: true
41+
})
42+
}
43+
}
44+
45+
main().catch(err => {
46+
console.error(err) // eslint-disable-line no-console
47+
process.exit(1)
48+
})

benchmarks/memory/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "aegir/src/config/tsconfig.aegir.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
},
6+
"include": [
7+
"src",
8+
"test"
9+
]
10+
}

0 commit comments

Comments
 (0)