This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
132 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// adapted from https://github.com/netlify/edge-bundler/blob/main/node/logger.ts | ||
type LogFunction = (...args: unknown[]) => void | ||
|
||
const noopLogger: LogFunction = () => { | ||
// no-op | ||
} | ||
|
||
interface Logger { | ||
system: LogFunction | ||
user: LogFunction | ||
} | ||
|
||
const getLogger = (systemLogger?: LogFunction, debug = false): Logger => { | ||
// If there is a system logger configured, we'll use that. If there isn't, | ||
// we'll pipe system logs to stdout if `debug` is enabled and swallow them | ||
// otherwise. | ||
const system = systemLogger ?? (debug ? console.log : noopLogger) | ||
|
||
return { | ||
system, | ||
user: console.log, | ||
} | ||
} | ||
|
||
export { getLogger } | ||
export type { LogFunction, Logger } |
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,15 @@ | ||
import { hrtime } from 'process' | ||
|
||
const NANOSECS_TO_SECS = 1e9 | ||
|
||
export const startTimer = function () { | ||
return hrtime() | ||
} | ||
|
||
// returns the time in nanoseconds | ||
export const endTimer = function ([startSecs, startNsecs]: [number, number]) { | ||
const [endSecs, endNsecs] = hrtime() | ||
const durationNs = (endSecs - startSecs) * NANOSECS_TO_SECS + endNsecs - startNsecs | ||
|
||
return durationNs | ||
} |
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,60 @@ | ||
import { afterEach, test, expect, vi } from 'vitest' | ||
|
||
import { getLogger } from '../../../src/utils/logger.js' | ||
|
||
const consoleLog = console.log | ||
|
||
const noopLogger = () => { | ||
// no-op | ||
} | ||
|
||
afterEach(() => { | ||
// Restoring global `console.log`. | ||
console.log = consoleLog | ||
}) | ||
|
||
test('Prints user logs to stdout', () => { | ||
const mockConsoleLog = vi.fn() | ||
console.log = mockConsoleLog | ||
|
||
const logger1 = getLogger(noopLogger, true) | ||
const logger2 = getLogger(noopLogger, false) | ||
|
||
logger1.user('Hello with `debug: true`') | ||
logger2.user('Hello with `debug: false`') | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(2) | ||
expect(mockConsoleLog).toHaveBeenNthCalledWith(1, 'Hello with `debug: true`') | ||
expect(mockConsoleLog).toHaveBeenNthCalledWith(2, 'Hello with `debug: false`') | ||
}) | ||
|
||
test('Prints system logs to the system logger provided', () => { | ||
const mockSystemLog = vi.fn() | ||
const mockConsoleLog = vi.fn() | ||
console.log = mockSystemLog | ||
|
||
const logger1 = getLogger(mockSystemLog, true) | ||
const logger2 = getLogger(mockSystemLog, false) | ||
|
||
logger1.system('Hello with `debug: true`') | ||
logger2.system('Hello with `debug: false`') | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(0) | ||
expect(mockSystemLog).toHaveBeenCalledTimes(2) | ||
expect(mockSystemLog).toHaveBeenNthCalledWith(1, 'Hello with `debug: true`') | ||
expect(mockSystemLog).toHaveBeenNthCalledWith(2, 'Hello with `debug: false`') | ||
}) | ||
|
||
test('Prints system logs to stdout if there is no system logger provided and `debug` is enabled', () => { | ||
const mockConsoleLog = vi.fn() | ||
console.log = mockConsoleLog | ||
|
||
const logger1 = getLogger(undefined, true) | ||
const logger2 = getLogger(undefined, false) | ||
|
||
logger1.system('Hello with `debug: true`') | ||
logger2.system('Hello with `debug: false`') | ||
|
||
expect(mockConsoleLog).toHaveBeenCalledTimes(1) | ||
expect(mockConsoleLog).toHaveBeenNthCalledWith(1, 'Hello with `debug: true`') | ||
}) |
bdc8f84
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⏱ Benchmark results
largeDepsEsbuild: 2.1s
largeDepsNft: 9.5s
largeDepsZisi: 14.7s