-h, --help show this dialog
--debug enable additional logging output
--benchmark <value> generate work for specified number of random hashes
+ --score <value> used with --benchmark to run specified number of times and calculate work-per-second
If validating a nonce, it must be a 16-character hexadecimal value.
Effort must be a decimal number between 1-32.
let isBatch = false
let isBenchmark = false
+let runs = 1
const body: { [key: string]: any } = {
action: 'work_generate'
}
process.env.NANO_POW_EFFORT = e
break
}
+ case ('--score'): {
+ const s = args[i + 1]
+ if (s == null) throw new Error('Missing argument for score')
+ const count = +s
+ if (count < 1) throw new Error('Invalid score runs count')
+ runs = count
+ break
+ }
case ('--validate'):
case ('-v'): {
const v = args[i + 1]
if (msg.data === 'ipc') {
logger.log(`CLI connected to server over IPC`)
try {
- await execute()
+ if (isBenchmark) {
+ if (runs > 1) {
+ await score()
+ } else {
+ await benchmark()
+ }
+ } else {
+ await execute()
+ }
} catch {
logger.log(`Error executing ${body.action}`)
} finally {
process.exit(code)
})
-async function execute (): Promise<void> {
- const results: (WorkErrorResponse | WorkGenerateResponse | WorkValidateResponse)[] = []
- if (isBenchmark) {
- console.log('Running benchmark...')
- }
- let start = 0
+async function benchmark () {
+ console.log('Running benchmark...')
+ let start = 0, end = 0
const times: number[] = []
for (const hash of hashes) {
+ const kill = setTimeout(() => {
+ throw new Error('cli execution timed out')
+ }, 60_000)
+ body.hash = hash
try {
- const aborter = new AbortController()
- const kill = setTimeout(() => aborter.abort(), 60_000)
- body.hash = hash
start = performance.now()
- const result: WorkGenerateResponse | WorkValidateResponse | WorkErrorResponse = await new Promise((resolve, reject): void => {
- const listener = async (msg: Serializable): Promise<void> => {
- if (typeof msg === 'object' && msg != null
- && 'type' in msg && typeof msg.type === 'string'
- && 'data' in msg && typeof msg.data === 'string'
- ) {
- if (msg.type === 'ipc') {
- resolve(JSON.parse(msg.data))
- server.off('message', listener)
- }
- }
- }
- server.on('message', listener)
- server.send({ type: 'ipc', data: JSON.stringify(body) }, cb => cb ? reject(cb) : logger.log('cli ipc request to server'))
- })
+ await request(body)
+ end = performance.now()
+ times.push(end - start)
+ } catch (err) {
+ logger.log(err)
+ } finally {
clearTimeout(kill)
- if (isBatch || isBenchmark) {
- results.push(result)
- times.push(performance.now() - start)
- } else {
- console.log(result)
+ }
+ }
+ console.log(stats(times))
+ return stats(times)
+}
+
+async function execute (): Promise<void> {
+ const results: (WorkErrorResponse | WorkGenerateResponse | WorkValidateResponse)[] = []
+ for (const hash of hashes) {
+ const kill = setTimeout(() => {
+ throw new Error('cli execution timed out')
+ }, 60_000)
+ body.hash = hash
+ try {
+ const result = await request(body)
+ isBatch ? results.push(result) : console.log(result)
+ } catch (err) {
+ logger.log(err)
+ } finally {
+ clearTimeout(kill)
+ }
+ }
+ if (isBatch) console.log(results)
+}
+
+async function request (body: { [key: string]: any }): Promise<WorkGenerateResponse | WorkValidateResponse | WorkErrorResponse> {
+ return new Promise((resolve, reject): void => {
+ const listener = async (msg: Serializable): Promise<void> => {
+ if (typeof msg === 'object' && msg != null
+ && 'type' in msg && typeof msg.type === 'string'
+ && 'data' in msg && typeof msg.data === 'string'
+ ) {
+ if (msg.type === 'ipc') {
+ resolve(JSON.parse(msg.data))
+ server.off('message', listener)
+ }
}
+ }
+ server.on('message', listener)
+ server.send({ type: 'ipc', data: JSON.stringify(body) }, cb => cb ? reject(cb) : logger.log('cli ipc request to server'))
+ })
+}
+
+async function score (): Promise<void> {
+ console.log('Calculating work per second...')
+ const rates: number[] = []
+ for (let i = 0; i < runs; i++) {
+ try {
+ const result = await benchmark()
+ logger.log(result)
+ if (result != null) rates.push(result.truncatedArithmetic)
} catch (err) {
logger.log(err)
}
}
- if (isBatch && !isBenchmark) console.log(results)
- if (process.env.NANO_POW_DEBUG || isBenchmark) console.log(stats(times))
+ console.log(stats(rates)?.truncatedHarmonic, 'wps')
}