1
+ import { performance } from 'node:perf_hooks'
2
+ import { readFile } from 'node:fs/promises'
3
+ import { join } from 'node:path'
4
+ import { extractDeclarations } from './src/extractor'
5
+
6
+ async function benchmark ( ) {
7
+ console . log ( '🚀 DTS Extractor Performance Benchmark\n' )
8
+
9
+ // Test files of different complexities
10
+ const testFiles = [
11
+ 'test/fixtures/input/example/0001.ts' , // Simple
12
+ 'test/fixtures/input/example/0002.ts' , // Medium
13
+ 'test/fixtures/input/example/0003.ts' , // Complex
14
+ 'test/fixtures/input/example/0005.ts' , // Very complex
15
+ ]
16
+
17
+ for ( const filePath of testFiles ) {
18
+ try {
19
+ const sourceCode = await readFile ( filePath , 'utf-8' )
20
+ const fileSize = sourceCode . length
21
+ const lineCount = sourceCode . split ( '\n' ) . length
22
+
23
+ console . log ( `📁 ${ filePath } ` )
24
+ console . log ( ` Size: ${ fileSize } chars, ${ lineCount } lines` )
25
+
26
+ // Warm up
27
+ for ( let i = 0 ; i < 3 ; i ++ ) {
28
+ extractDeclarations ( sourceCode , filePath )
29
+ }
30
+
31
+ // Benchmark
32
+ const iterations = 100
33
+ const start = performance . now ( )
34
+
35
+ for ( let i = 0 ; i < iterations ; i ++ ) {
36
+ extractDeclarations ( sourceCode , filePath )
37
+ }
38
+
39
+ const end = performance . now ( )
40
+ const avgTime = ( end - start ) / iterations
41
+ const throughput = fileSize / avgTime * 1000 // chars per second
42
+
43
+ console . log ( ` ⚡ Avg time: ${ avgTime . toFixed ( 2 ) } ms` )
44
+ console . log ( ` 📊 Throughput: ${ ( throughput / 1000 ) . toFixed ( 1 ) } k chars/sec` )
45
+ console . log ( )
46
+
47
+ } catch ( error ) {
48
+ console . error ( `❌ Error processing ${ filePath } :` , error instanceof Error ? error . message : String ( error ) )
49
+ }
50
+ }
51
+
52
+ // Test with a large synthetic file
53
+ console . log ( '🧪 Synthetic Large File Test' )
54
+ const largeFile = generateLargeTypeScriptFile ( 10000 ) // 10k lines
55
+
56
+ const start = performance . now ( )
57
+ const declarations = extractDeclarations ( largeFile , 'synthetic.ts' )
58
+ const end = performance . now ( )
59
+
60
+ console . log ( ` 📏 Generated ${ largeFile . split ( '\n' ) . length } lines` )
61
+ console . log ( ` 🔍 Found ${ declarations . length } declarations` )
62
+ console . log ( ` ⚡ Time: ${ ( end - start ) . toFixed ( 2 ) } ms` )
63
+ console . log ( ` 📊 Throughput: ${ ( largeFile . length / ( end - start ) * 1000 / 1000 ) . toFixed ( 1 ) } k chars/sec` )
64
+ }
65
+
66
+ function generateLargeTypeScriptFile ( lines : number ) : string {
67
+ const content : string [ ] = [ ]
68
+
69
+ // Add imports
70
+ content . push ( "import { SomeType } from 'some-module'" )
71
+ content . push ( "import type { AnotherType } from 'another-module'" )
72
+ content . push ( "" )
73
+
74
+ // Add interfaces
75
+ for ( let i = 0 ; i < lines * 0.1 ; i ++ ) {
76
+ content . push ( `export interface Interface${ i } {` )
77
+ content . push ( ` prop${ i } : string` )
78
+ content . push ( ` method${ i } (): void` )
79
+ content . push ( `}` )
80
+ content . push ( "" )
81
+ }
82
+
83
+ // Add types
84
+ for ( let i = 0 ; i < lines * 0.1 ; i ++ ) {
85
+ content . push ( `export type Type${ i } = string | number | Interface${ i } ` )
86
+ }
87
+
88
+ // Add functions
89
+ for ( let i = 0 ; i < lines * 0.2 ; i ++ ) {
90
+ content . push ( `export function func${ i } (param: Type${ i % 100 } ): Interface${ i % 100 } {` )
91
+ content . push ( ` return {} as Interface${ i % 100 } ` )
92
+ content . push ( `}` )
93
+ content . push ( "" )
94
+ }
95
+
96
+ // Add variables
97
+ for ( let i = 0 ; i < lines * 0.1 ; i ++ ) {
98
+ content . push ( `export const var${ i } : Type${ i % 100 } = 'value${ i } '` )
99
+ }
100
+
101
+ // Add classes
102
+ for ( let i = 0 ; i < lines * 0.1 ; i ++ ) {
103
+ content . push ( `export class Class${ i } implements Interface${ i % 100 } {` )
104
+ content . push ( ` prop${ i } = 'value'` )
105
+ content . push ( ` method${ i } () {}` )
106
+ content . push ( `}` )
107
+ content . push ( "" )
108
+ }
109
+
110
+ return content . join ( '\n' )
111
+ }
112
+
113
+ // Run benchmark
114
+ benchmark ( ) . catch ( console . error )
0 commit comments