-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
40 changed files
with
1,451 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/node_modules | ||
/benchmark/node_modules | ||
.TODO | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
npm test | ||
npm run build | ||
npm run denoify | ||
git add deno_dist | ||
git add dist | ||
git add cjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { benchmark } from './helpers.js'; | ||
import { | ||
createChallenge, | ||
solveChallenge, | ||
solveChallengeWorkers, | ||
} from '../lib/index.js'; | ||
|
||
const hmacKey = 'test'; | ||
const workers = 8; | ||
const workerScript = await import.meta.resolve!('../lib/worker.ts'); | ||
|
||
const challenge1 = await createChallenge({ | ||
hmacKey, | ||
maxNumber: 1000, | ||
number: 1000, | ||
}); | ||
|
||
const challenge2 = await createChallenge({ | ||
hmacKey, | ||
maxNumber: 10000, | ||
number: 10000, | ||
}); | ||
|
||
const challenge3 = await createChallenge({ | ||
hmacKey, | ||
maxNumber: 100000, | ||
number: 100000, | ||
}); | ||
|
||
const challenge4 = await createChallenge({ | ||
hmacKey, | ||
maxNumber: 500000, | ||
number: 500000, | ||
}); | ||
|
||
await benchmark('solveChallenge()', (bench) => { | ||
bench | ||
.add('n = 1,000', async () => { | ||
await solveChallenge(challenge1.challenge, challenge1.salt).promise; | ||
}) | ||
.add('n = 10,000', async () => { | ||
await solveChallenge(challenge2.challenge, challenge2.salt).promise; | ||
}) | ||
.add('n = 100,000', async () => { | ||
await solveChallenge(challenge3.challenge, challenge3.salt).promise; | ||
}) | ||
.add('n = 500,000', async () => { | ||
await solveChallenge(challenge4.challenge, challenge4.salt).promise; | ||
}) | ||
}); | ||
|
||
await benchmark(`solveChallengeWorkers() (${workers} workers)`, (bench) => { | ||
bench | ||
.add('n = 1,000', async () => { | ||
await solveChallengeWorkers( | ||
workerScript, | ||
workers, | ||
challenge1.challenge, | ||
challenge1.salt, | ||
challenge1.algorithm, | ||
challenge1.max, | ||
); | ||
}) | ||
.add('n = 10,000', async () => { | ||
await solveChallengeWorkers( | ||
workerScript, | ||
workers, | ||
challenge2.challenge, | ||
challenge2.salt, | ||
challenge2.algorithm, | ||
challenge2.max, | ||
); | ||
}) | ||
.add('n = 100,000', async () => { | ||
await solveChallengeWorkers( | ||
workerScript, | ||
workers, | ||
challenge3.challenge, | ||
challenge3.salt, | ||
challenge3.algorithm, | ||
challenge3.max, | ||
); | ||
}) | ||
.add('n = 500,000', async () => { | ||
await solveChallengeWorkers( | ||
workerScript, | ||
workers, | ||
challenge4.challenge, | ||
challenge4.salt, | ||
challenge4.algorithm, | ||
challenge4.max, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Bench } from 'tinybench'; | ||
|
||
const NAME_MAX_LEN = 40; | ||
|
||
export async function benchmark(name: string, initFn: (bench: Bench) => void, duration: number = 500) { | ||
const bench = new Bench({ | ||
time: duration, | ||
throws: true, | ||
warmupTime: 2000, | ||
warmupIterations: 100, | ||
}); | ||
initFn(bench); | ||
await bench.run(); | ||
console.log('>', name); | ||
for (let row of bench.table()) { | ||
if (row) { | ||
console.log( | ||
'-', | ||
row['Task Name'].slice(0, NAME_MAX_LEN).padEnd(NAME_MAX_LEN, '.'), | ||
row['ops/sec'].padStart(10, ' '), | ||
'ops/s', | ||
row['Margin'], | ||
); | ||
} | ||
} | ||
console.log(''); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"type": "module", | ||
"scripts": { | ||
"bench": "npx tsx bench.ts" | ||
}, | ||
"devDependencies": { | ||
"tinybench": "^2.6.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
if (!('crypto' in globalThis)) { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
globalThis.crypto = require('node:crypto').webcrypto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import type { Challenge, ChallengeOptions, Payload } from './types.js'; | ||
import type { Challenge, ChallengeOptions, Payload, Solution } from './types.js'; | ||
export declare function createChallenge(options: ChallengeOptions): Promise<Challenge>; | ||
export declare function verifySolution(payload: string | Payload, hmacKey: string): Promise<boolean>; | ||
export declare function solveChallenge(challenge: string, salt: string, algorithm?: string, max?: number, start?: number): { | ||
promise: Promise<Solution | null>; | ||
controller: AbortController; | ||
}; | ||
export declare function solveChallengeWorkers(workerScript: string | URL, concurrency: number, challenge: string, salt: string, algorithm?: string, max?: number, startNumber?: number): Promise<Solution | null>; |
Oops, something went wrong.