Skip to content

Commit

Permalink
refactor(bindings/nodejs): Polish benchmark to make it more readable (#…
Browse files Browse the repository at this point in the history
…1810)

Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo committed Mar 31, 2023
1 parent db36bfd commit 85c3785
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 174 deletions.
17 changes: 17 additions & 0 deletions bindings/nodejs/benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OpenDAL Node.js Bindings Benchmark

This benchmark is test against the opendal and aws js sdk.

To run the benchmark, please make sure the following env have been set correctly.

- AWS_S3_ENDPOINT: the endpoint of the s3 service
- AWS_S3_REGION: the region of the s3 service
- AWS_ACCESS_KEY_ID: the access key of the s3 service
- AWS_SECRET_ACCESS_KEY: the secret key of the s3 service
- AWS_BUCKET: the bucket name of the s3 service

To run the benchmark:

```shell
yarn run bench
```
95 changes: 91 additions & 4 deletions bindings/nodejs/benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,98 @@
* under the License.
*/

const read = require('./read.js')
const write = require('./write.js')
const { Operator } = require('../index.js')
const { S3Client, PutObjectCommand, GetObjectCommand } = require('@aws-sdk/client-s3')
const { suite, add, cycle, complete } = require('benny')
const crypto = require('node:crypto')

const endpoint = process.env.AWS_S3_ENDPOINT
const region = process.env.AWS_S3_REGION
const accessKeyId = process.env.AWS_ACCESS_KEY_ID
const secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY
const bucket = process.env.AWS_BUCKET

const opendalClient = new Operator('s3', {
root: '/',
bucket,
endpoint,
})

const s3Client = new S3Client({
endpoint,
region,
credentials: {
accessKeyId,
secretAccessKey,
},
})

const testCases = [
{ name: '4kb', content: Buffer.alloc(4 * 1024, 'opendal', 'utf8') },
{ name: '256kb', content: Buffer.alloc(256 * 1024, 'opendal', 'utf8') },
{ name: '4mb', content: Buffer.alloc(4 * 1024 * 1024, 'opendal', 'utf8') },
{ name: '16mb', content: Buffer.alloc(16 * 1024 * 1024, 'opendal', 'utf8') },
]

async function benchRead() {
const uuid = crypto.randomUUID()
await testCases
.map((v) => async () => {
const filename = `${uuid}_${v.name}_read_bench.txt`
await opendalClient.write(filename, v.content)

return suite(
`read (${v.name})`,
add(`opendal read (${v.name})`, async () => {
await opendalClient.read(filename).then((v) => v.toString('utf-8'))
}),
add(`s3 read (${v.name})`, async () => {
const command = new GetObjectCommand({
Key: filename,
Bucket: bucket,
})
await s3Client.send(command).then((v) => v.Body.transformToString('utf-8'))
}),
cycle(),
complete(),
)
})
.reduce((p, v) => p.then(() => v()), Promise.resolve())
}

async function benchWrite() {
const uuid = crypto.randomUUID()
await testCases
.map(
(v) => () =>
suite(
`write (${v.name})`,
add(`opendal write (${v.name})`, async () => {
let count = 0
return async () => opendalClient.write(`${uuid}_${count++}_${v.name}_opendal.txt`, v.content)
}),
add(`s3 write (${v.name})`, async () => {
let count = 0

return async () => {
const command = new PutObjectCommand({
Bucket: bucket,
Key: `${uuid}_${count++}_${v.name}_s3.txt`,
Body: v.content,
})
await s3Client.send(command)
}
}),
cycle(),
complete(),
),
)
.reduce((p, v) => p.then(() => v()), Promise.resolve())
}

async function bench() {
await write()
await read()
await benchRead()
await benchWrite()
}

bench()
79 changes: 0 additions & 79 deletions bindings/nodejs/benchmark/lib.js

This file was deleted.

45 changes: 0 additions & 45 deletions bindings/nodejs/benchmark/read.js

This file was deleted.

46 changes: 0 additions & 46 deletions bindings/nodejs/benchmark/write.js

This file was deleted.

1 change: 1 addition & 0 deletions bindings/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"format": "prettier --write .",
"prepublishOnly": "napi prepublish -t npm",
"test": "cucumber-js",
"bench": "node ./benchmark/index.js",
"version": "napi version"
},
"prettier": {
Expand Down

0 comments on commit 85c3785

Please sign in to comment.