Skip to content

Commit

Permalink
make it simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Feb 26, 2024
1 parent 966f8f5 commit ae5f73b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 46 deletions.
37 changes: 14 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ console.log("Server running at http://localhost:3000/");
Another interesting use case is running benchmarks within different browsers using tools like Tinybench. For example:

```ts
import assert from 'node:assert'
import { test } from 'node:test'
import { run } from 'bx'

async function benchmarkTest() {
Expand All @@ -107,33 +105,26 @@ async function benchmarkTest() {
console.log('I am slower')
})

await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
await bench.run();

return bench.results;
}

test('benchmark test in Chrome', async () => {
const results = await run(benchmarkTest, {
browserName: 'chrome'
})
assert.equal(results.length, 2)
assert.ok(results[0].mean < results[1].mean)
assert.ok(results[0].mean < 1)
// mean is around 4.45
assert.ok(results[1].mean > 4)
assert.ok(results[1].mean < 5)
const [fasterTaskChrome, slowerTaskChrome] = await run(benchmarkTest, {
browserName: 'chrome'
})

test('benchmark test in Firefox', async () => {
const results = await run(benchmarkTest, {
browserName: 'firefox'
})
assert.ok(results[0].mean < 1)
// mean is around 5
assert.ok(results[1].mean > 4.5)
assert.ok(results[1].mean < 5.55)
const [fasterTaskFirefox, slowerTaskFirefox] = await run(benchmarkTest, {
browserName: 'firefox'
})

console.log(`Chrome: faster task ${fasterTaskChrome.mean}ms, slower task ${slowerTaskChrome.mean}ms`)
console.log(`Firefox: faster task ${fasterTaskFirefox.mean}ms, slower task ${slowerTaskFirefox.mean}ms`)
```

which prints out:

```
Chrome: faster task 0.015639662257872562ms, slower task 4.015384614467621ms
Firefox: faster task 0.007285443683520326ms, slower task 4.761904761904762ms
```

# Session Management
Expand Down
30 changes: 7 additions & 23 deletions examples/benchmark.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import assert from 'node:assert'
import { test } from 'node:test'
import { run } from '../dist/index.js'

async function benchmarkTest() {
Expand All @@ -15,30 +13,16 @@ async function benchmarkTest() {
console.log('I am slower')
})

await bench.warmup(); // make results more reliable, ref: https://github.com/tinylibs/tinybench/pull/50
await bench.run();

return bench.results;
}

test('benchmark test in Chrome', async () => {
const results = await run(benchmarkTest, {
browserName: 'chrome'
})
assert.equal(results.length, 2)
assert.ok(results[0].mean < results[1].mean)
assert.ok(results[0].mean < 1)
// mean is around 4.45
assert.ok(results[1].mean > 4)
assert.ok(results[1].mean < 5)
const [fasterTaskChrome, slowerTaskChrome] = await run(benchmarkTest, {
browserName: 'chrome'
})

test('benchmark test in Firefox', async () => {
const results = await run(benchmarkTest, {
browserName: 'firefox'
})
assert.ok(results[0].mean < 1)
// mean is around 5
assert.ok(results[1].mean > 5)
assert.ok(results[1].mean < 6)
const [fasterTaskFirefox, slowerTaskFirefox] = await run(benchmarkTest, {
browserName: 'firefox'
})

console.log(`Chrome: faster task ${fasterTaskChrome.mean}ms, slower task ${slowerTaskChrome.mean}ms`)
console.log(`Firefox: faster task ${fasterTaskFirefox.mean}ms, slower task ${slowerTaskFirefox.mean}ms`)

0 comments on commit ae5f73b

Please sign in to comment.