Skip to content

Commit d20459e

Browse files
authored
🔊 common benchmark log (#4)
1 parent f5b2866 commit d20459e

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

.changeset/sour-deers-beam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vsit": patch
3+
---
4+
5+
common debug log

packages/vsit/src/common/log.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Debug from 'debug'
2+
3+
export const debug = {
4+
benchmark: Debug('vit:benchmark'),
5+
plugin: Debug('vsit:plugin'),
6+
store: Debug('vsit:store'),
7+
}

packages/vsit/src/common/store/index.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
import { createHash } from 'node:crypto'
2-
import { performance } from 'node:perf_hooks'
32

4-
import Debug from 'debug'
53
import { fetch } from 'ofetch'
64

7-
const debug = Debug('vsit:store')
5+
import { debug } from '@/common/log'
86

97
export const createStore = () => {
108
const pool = new Map<string, Promise<string>>()
119
const globalCache = new Map<string, string>()
1210
const createInstance = (id: string, url: string, options?: RequestInit) => {
1311
const promise = (async () => {
1412
try {
15-
const now = performance.now()
13+
debug.store('start fetch %s', url)
1614
return fetch(url, options)
1715
.then(async (res) => {
1816
const content = await res.text()
1917
globalCache.set(id, content)
2018
pool.delete(id)
21-
debug('load url %s took', url, `${(performance.now() - now) / 1000}ms`)
2219
return content
2320
})
2421
} catch (e) {
@@ -36,11 +33,10 @@ export const createStore = () => {
3633
pool.delete(hash)
3734
},
3835
async fetch(url: string, options?: RequestInit) {
39-
debug('start fetch %s', url)
4036
const hash = createHash('sha256').update(url).digest('hex')
4137
const cache = globalCache.get(hash)
4238
if (cache) {
43-
debug('load cache %s', url)
39+
debug.store('load cache %s', url)
4440
return Promise.resolve(cache)
4541
}
4642
let instance = pool.get(hash)

packages/vsit/src/plugins/vsit/index.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { performance } from 'node:perf_hooks'
2+
13
import bodyparser from 'body-parser'
24
import Hook from 'console-feed/lib/Hook'
35
import { Encode } from 'console-feed/lib/Transform'
4-
import Debug from 'debug'
56
import { parseURL, withoutLeadingSlash } from 'ufo'
67

8+
import { debug } from '@/common/log'
79
import { RESOLVED_NODE_ID, VIRUTAL_NODE_ID } from '@/common/resolver/constants'
810
import { isEsmSh } from '@/common/resolver/is'
911
import {
@@ -16,8 +18,6 @@ import { createStore } from '@/common/store'
1618

1719
import type { Plugin } from 'vite'
1820

19-
const debug = Debug('vsit:plugin')
20-
2121
export const PluginVit = (): Plugin[] => {
2222
let content = ''
2323
const store = createStore()
@@ -35,12 +35,12 @@ export const PluginVit = (): Plugin[] => {
3535
if (url.pathname === '/update-fake-node-file' && req.method === 'POST') {
3636
const body = (req as any).body as { content: string }
3737
content = injectConsoleHook(body.content)
38-
debug('update fake node file %s', content)
38+
debug.plugin('update fake node file %s', content)
3939
res.end('ok')
4040
return
4141
}
4242
if (url.pathname === '/fake-node-file' && req.method === 'GET') {
43-
debug('request latest fake node file')
43+
debug.plugin('request latest fake node file')
4444
try {
4545
// console.log('request', req.url)
4646
// /fake-node-file?t=<timestamp>
@@ -89,9 +89,11 @@ export const PluginVit = (): Plugin[] => {
8989
// vite will remove duplicate slash if id starts with 'https://'
9090
if (isEsmSh(id)) {
9191
// un wrap
92+
const now = performance.now()
9293
const url = unWrapId(id)
9394
const code = await store.fetch(url)
9495
const resolvedCode = wrapCode(code)
96+
debug.benchmark('load url %s took', url, `${(performance.now() - now) / 1000}ms`)
9597
return {
9698
code: resolvedCode,
9799
moduleSideEffects: false,

0 commit comments

Comments
 (0)