Skip to content

Commit

Permalink
chore: Updated dependencies and toolchain.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Oct 23, 2023
1 parent b57d652 commit a85f833
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 162 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
version: latest
- name: Install dependencies
run: pnpm install
run: pnpm install --shamefully-hoist
- name: Run Tests
run: pnpm run ci
- name: Upload coverage to Codecov
Expand Down
40 changes: 21 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,41 @@
"types": "./dist/index.d.ts",
"scripts": {
"dev": "swc --delete-dir-on-start -s -w -d dist src",
"prebuild": "rm -rf dist && npm run lint",
"build": "swc -d dist src",
"postbuild": "tsc -p . --emitDeclarationOnly",
"build": "swc --delete-dir-on-start -d dist src",
"postbuild": "concurrently npm:lint npm:typecheck",
"format": "prettier -w src test",
"lint": "eslint src test",
"test": "c8 -c test/config/c8-local.json tap --rcfile=test/config/tap.yml test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json tap --rcfile=test/config/tap.yml --no-color test/*.test.ts",
"lint": "eslint --cache --ext .js,.jsx,.ts,.tsx src test",
"typecheck": "tsc -p . --emitDeclarationOnly",
"test": "c8 -c test/config/c8-local.json tap test/*.test.ts",
"test:ci": "c8 -c test/config/c8-ci.json tap --no-color test/*.test.ts",
"ci": "npm run build && npm run test:ci",
"prepublishOnly": "npm run ci",
"postpublish": "git push origin && git push origin -f --tags"
},
"dependencies": {
"acquerello": "^1.0.12",
"acquerello": "^1.1.2",
"hdr-histogram-js": "^3.0.0",
"table": "^6.8.1"
},
"devDependencies": {
"@cowtech/eslint-config": "^8.8.0",
"@swc/cli": "^0.1.57",
"@swc/core": "^1.3.19",
"@types/node": "^18.11.9",
"@types/sinon": "^10.0.13",
"@types/tap": "^15.0.7",
"c8": "^7.12.0",
"@cowtech/eslint-config": "^8.10.0",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.94",
"@types/node": "^20.8.7",
"@types/tap": "^15.0.10",
"c8": "^8.0.1",
"chokidar": "^3.5.3",
"prettier": "^2.8.0",
"concurrently": "^8.2.2",
"prettier": "^3.0.3",
"proxyquire": "^2.1.3",
"sinon": "^14.0.2",
"tap": "^16.3.2",
"tap": "^18.5.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"typescript": "^5.2.2"
},
"engines": {
"node": ">=14.15.0"
"node": ">= 18.18.0"
},
"tap": {
"extends": "./test/config/tap.yml"
}
}
33 changes: 27 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { isMainThread, Worker, workerData } from 'node:worker_threads'
import { Callback, Context, defaultOptions, Options, PrintOptions, Results, runnerPath, Tests } from './models.js'
import {
defaultOptions,
runnerPath,
type Callback,
type Context,
type Options,
type PrintOptions,
type Results,
type Tests
} from './models.js'
import { printResults } from './print.js'

type PromiseResolver<T> = (value: T) => void
Expand All @@ -10,7 +19,10 @@ export * from './models.js'
function scheduleNextTest(context: Context): void {
// We still have work to do
if (context.current < context.tests.length) {
return process.nextTick(() => run(context))
process.nextTick(() => {
run(context)
})
return
}

if (context.print) {
Expand Down Expand Up @@ -65,7 +77,13 @@ function run(context: Context): void {
scheduleNextTest(context)
})

worker.on('message', result => {
worker.on('message', message => {
if (message.type !== 'cronometro.result') {
return
}

const result = message.payload

context.results[name] = result
context.current++

Expand Down Expand Up @@ -112,10 +130,11 @@ export function cronometro(

callback = function (err: Error | null, results?: Results): void {
if (err) {
return promiseReject(err)
promiseReject(err)
return
}

return promiseResolve(results!)
promiseResolve(results!)
}
}

Expand Down Expand Up @@ -165,7 +184,9 @@ export function cronometro(
onTestError
}

process.nextTick(() => run(context))
process.nextTick(() => {
run(context)
})
return promise
}

Expand Down
18 changes: 6 additions & 12 deletions src/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Histogram } from 'hdr-histogram-js'
import { type Histogram } from 'hdr-histogram-js'
import { resolve } from 'node:path'
import { Worker } from 'node:worker_threads'
import { type Worker } from 'node:worker_threads'

export interface PrintOptions {
colors?: boolean
Expand All @@ -13,7 +13,7 @@ export type SetupFunction = (cb: (err?: Error | null) => void) => Promise<any> |

export interface Options {
iterations: number
setup: { [key: string]: SetupFunction }
setup: Record<string, SetupFunction>
errorThreshold: number
print: boolean | PrintOptions
warmup: boolean
Expand All @@ -35,9 +35,7 @@ export interface Test {

export type Callback = (err: Error | null, results: Results) => any

export interface Percentiles {
[key: string]: number
}
export type Percentiles = Record<string, number>

export interface Result {
success: boolean
Expand All @@ -51,13 +49,9 @@ export interface Result {
percentiles: Percentiles
}

export interface Tests {
[key: string]: TestFunction | Test
}
export type Tests = Record<string, TestFunction | Test>

export interface Results {
[key: string]: Result
}
export type Results = Record<string, Result>

export interface Context {
warmup: boolean
Expand Down
2 changes: 1 addition & 1 deletion src/print.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clean, colorize } from 'acquerello'
import { table } from 'table'
import { Results } from './models.js'
import { type Results } from './models.js'

const styles = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'gray']

Expand Down
10 changes: 7 additions & 3 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ if (workerData.path.endsWith('.ts')) {
const instance = Symbol.for('ts-node.register.instance')

if (!(instance in process)) {
// eslint-disable-next-line unicorn/prefer-top-level-await
chain = import('ts-node').then(({ register }) => {
register({ project: process.env.TS_NODE_PROJECT })
})
Expand All @@ -34,9 +33,14 @@ chain
})
.then(() => {
// Run the worker
runWorker(workerData, value => parentPort!.postMessage(value), process.exit)
runWorker(
workerData,
value => {
parentPort!.postMessage({ type: 'cronometro.result', payload: value })
},
process.exit
)
})
// eslint-disable-next-line unicorn/prefer-top-level-await
.catch(error => {
process.nextTick(() => {
throw error
Expand Down
36 changes: 27 additions & 9 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { build as buildHistogram } from 'hdr-histogram-js'
import { percentiles, Result, SetupFunction, TestContext, TestFunction, WorkerContext } from './models.js'
import {
percentiles,
type Result,
type SetupFunction,
type TestContext,
type TestFunction,
type WorkerContext
} from './models.js'

function noOp(): void {
// No-op
Expand All @@ -15,7 +22,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {

// Handle error
if (error) {
return context.callback({
context.callback({
success: false,
error,
size: 0,
Expand All @@ -26,6 +33,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
percentiles: {},
standardError: 0
})
return
}

// Get some parameters
Expand Down Expand Up @@ -56,7 +64,7 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
if (stop || executed > total) {
const stdDev = histogram.stdDeviation

return context.callback({
context.callback({
success: true,
size: executed,
min: histogram.minNonZeroValue,
Expand All @@ -68,10 +76,13 @@ function handleTestIteration(context: TestContext, error?: Error | null): void {
),
standardError: stdDev / Math.sqrt(executed)
})
return
}

// Schedule next iteration
process.nextTick(() => runTestIteration(context))
process.nextTick(() => {
runTestIteration(context)
})
}

function runTestIteration(context: TestContext): void {
Expand All @@ -82,15 +93,18 @@ function runTestIteration(context: TestContext): void {

// It is a promise, handle it accordingly
if (callResult && typeof callResult.then === 'function') {
callResult.then(() => context.handler(null), context.handler)
callResult.then(() => {
context.handler(null)
}, context.handler)
} else if (context.test.length === 0) {
// The function is not a promise and has no arguments, so it's sync
context.handler(null)
}
} catch (error) {
// If a error was thrown, only handle if the original function length is 0, which means it's a sync error, otherwise propagate
if (context.test.length === 0) {
return context.handler(error)
context.handler(error)
return
}

throw error
Expand All @@ -99,7 +113,7 @@ function runTestIteration(context: TestContext): void {

function beforeCallback(testContext: TestContext, err?: Error | null): void {
if (err) {
return testContext.callback({
testContext.callback({
success: false,
error: err,
size: 0,
Expand All @@ -110,10 +124,13 @@ function beforeCallback(testContext: TestContext, err?: Error | null): void {
percentiles: {},
standardError: 0
})
return
}

// Schedule the first run
return process.nextTick(() => runTestIteration(testContext))
process.nextTick(() => {
runTestIteration(testContext)
})
}

function afterCallback(
Expand Down Expand Up @@ -190,7 +207,8 @@ export function runWorker(context: WorkerContext, notifier: (value: any) => void
callback(result: Result): void {
if (warmup) {
context.warmup = false
return runWorker(context, notifier, cb)
runWorker(context, notifier, cb)
return
}

const callback = afterCallback.bind(null, result, notifier, cb)
Expand Down
1 change: 0 additions & 1 deletion test/asyncImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ async function main(): Promise<void> {
}
}

// eslint-disable-next-line unicorn/prefer-top-level-await
main()

export default main
2 changes: 1 addition & 1 deletion test/callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { isMainThread, Worker } from 'node:worker_threads'
import t from 'tap'
import { cronometro, Result, TestFunction } from '../src/index.js'
import { cronometro, type Result, type TestFunction } from '../src/index.js'

if (!isMainThread) {
cronometro(
Expand Down
6 changes: 3 additions & 3 deletions test/config/tap.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
jobs: 5
timeout: 120
reporter: spec
coverage: false
timeout: 60
disable-coverage: true
allow-empty-coverage: true
node-arg:
- --loader=ts-node/esm
2 changes: 1 addition & 1 deletion test/fixture/sample.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cronometro from '../../dist/index.js'

const pattern = /[1-3]/g
const replacements: { [key: string]: string } = { 1: 'a', 2: 'b', 3: 'c' }
const replacements: Record<string, string> = { 1: 'a', 2: 'b', 3: 'c' }

const subject =
'123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123'
Expand Down
Loading

0 comments on commit a85f833

Please sign in to comment.