1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Simple benchmark comparing local built language server vs npm latest
5
+ * Run with: node benchmarks/language-server-comparison.js
6
+ */
7
+
8
+ const { spawn } = require ( 'child_process' ) ;
9
+ const { performance } = require ( 'perf_hooks' ) ;
10
+ const fs = require ( 'fs' ) ;
11
+ const path = require ( 'path' ) ;
12
+
13
+ async function runCommand ( command , args , cwd ) {
14
+ return new Promise ( ( resolve , reject ) => {
15
+ const proc = spawn ( command , args , { cwd, stdio : 'pipe' } ) ;
16
+ let stdout = '' ;
17
+ let stderr = '' ;
18
+
19
+ proc . stdout . on ( 'data' , ( data ) => stdout += data ) ;
20
+ proc . stderr . on ( 'data' , ( data ) => stderr += data ) ;
21
+
22
+ proc . on ( 'close' , ( code ) => {
23
+ if ( code === 0 ) {
24
+ resolve ( { stdout, stderr } ) ;
25
+ } else {
26
+ reject ( new Error ( `Command failed: ${ stderr } ` ) ) ;
27
+ }
28
+ } ) ;
29
+ } ) ;
30
+ }
31
+
32
+ async function installNpmLatest ( ) {
33
+ console . log ( '📦 Installing svelte-language-server@latest...' ) ;
34
+ try {
35
+ await runCommand ( 'npm' , [ 'install' , 'svelte-language-server@latest' ] ,
36
+ path . join ( __dirname , 'temp' ) ) ;
37
+ } catch ( error ) {
38
+ // Create temp dir and try again
39
+ fs . mkdirSync ( path . join ( __dirname , 'temp' ) , { recursive : true } ) ;
40
+ fs . writeFileSync ( path . join ( __dirname , 'temp' , 'package.json' ) , '{}' ) ;
41
+ await runCommand ( 'npm' , [ 'install' , 'svelte-language-server@latest' ] ,
42
+ path . join ( __dirname , 'temp' ) ) ;
43
+ }
44
+ }
45
+
46
+ async function benchmarkLanguageServer ( serverPath , name ) {
47
+ console . log ( `\n🔥 Benchmarking ${ name } ...` ) ;
48
+
49
+ const testFile = path . join ( __dirname , 'test-file.svelte' ) ;
50
+ if ( ! fs . existsSync ( testFile ) ) {
51
+ fs . writeFileSync ( testFile , `<script lang="ts">
52
+ let count: number = 0;
53
+ let name: string = 'world';
54
+
55
+ function increment() {
56
+ count += 1;
57
+ }
58
+
59
+ $: doubled = count * 2;
60
+ </script>
61
+
62
+ <h1>Hello {name}!</h1>
63
+ <button on:click={increment}>
64
+ Count: {count} (doubled: {doubled})
65
+ </button>
66
+
67
+ <style>
68
+ h1 { color: blue; }
69
+ button { padding: 10px; }
70
+ </style>` ) ;
71
+ }
72
+
73
+ const results = { } ;
74
+
75
+ // Test basic operations by sending LSP requests
76
+ // This is simplified - in reality you'd send proper LSP initialize/requests
77
+
78
+ try {
79
+ // Measure startup time
80
+ const startTime = performance . now ( ) ;
81
+ const proc = spawn ( 'node' , [ serverPath , '--stdio' ] ) ;
82
+
83
+ // Give it a moment to start
84
+ await new Promise ( resolve => setTimeout ( resolve , 100 ) ) ;
85
+
86
+ const initTime = performance . now ( ) - startTime ;
87
+ results . startup = `${ initTime . toFixed ( 1 ) } ms` ;
88
+
89
+ proc . kill ( ) ;
90
+
91
+ // For now, just measure the module require time as a proxy
92
+ const requireStart = performance . now ( ) ;
93
+ delete require . cache [ require . resolve ( serverPath ) ] ;
94
+ require ( serverPath ) ;
95
+ const requireTime = performance . now ( ) - requireStart ;
96
+ results . moduleLoad = `${ requireTime . toFixed ( 1 ) } ms` ;
97
+
98
+ } catch ( error ) {
99
+ results . error = error . message ;
100
+ }
101
+
102
+ return results ;
103
+ }
104
+
105
+ async function main ( ) {
106
+ console . log ( '🏁 Language Server Version Comparison' ) ;
107
+ console . log ( '=====================================\n' ) ;
108
+
109
+ // Setup
110
+ await installNpmLatest ( ) ;
111
+
112
+ // Build local version
113
+ console . log ( '🔨 Building local version...' ) ;
114
+ await runCommand ( 'npm' , [ 'run' , 'build' ] , path . join ( __dirname , '..' , 'packages' , 'language-server' ) ) ;
115
+
116
+ // Benchmark paths
117
+ const localPath = path . join ( __dirname , '..' , 'packages' , 'language-server' , 'dist' , 'src' , 'index.js' ) ;
118
+ const npmPath = path . join ( __dirname , 'temp' , 'node_modules' , 'svelte-language-server' , 'dist' , 'src' , 'index.js' ) ;
119
+
120
+ // Check if files exist
121
+ if ( ! fs . existsSync ( localPath ) ) {
122
+ console . error ( '❌ Local build not found. Run `npm run build` first.' ) ;
123
+ return ;
124
+ }
125
+
126
+ if ( ! fs . existsSync ( npmPath ) ) {
127
+ console . error ( '❌ npm version not found. Installation failed.' ) ;
128
+ return ;
129
+ }
130
+
131
+ // Get versions
132
+ const localPkg = JSON . parse ( fs . readFileSync ( path . join ( __dirname , '..' , 'packages' , 'language-server' , 'package.json' ) ) ) ;
133
+ const npmPkg = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'temp' , 'node_modules' , 'svelte-language-server' , 'package.json' ) ) ) ;
134
+
135
+ console . log ( `📊 Comparing versions:` ) ;
136
+ console . log ( ` Local: ${ localPkg . version } ` ) ;
137
+ console . log ( ` npm: ${ npmPkg . version } ` ) ;
138
+
139
+ // Run benchmarks
140
+ const localResults = await benchmarkLanguageServer ( localPath , `Local v${ localPkg . version } ` ) ;
141
+ const npmResults = await benchmarkLanguageServer ( npmPath , `npm v${ npmPkg . version } ` ) ;
142
+
143
+ // Display results
144
+ console . log ( '\n📈 Results:' ) ;
145
+ console . log ( '===========' ) ;
146
+ console . log ( '| Metric | Local | npm | Comparison |' ) ;
147
+ console . log ( '|--------|--------|--------|------------|' ) ;
148
+
149
+ for ( const [ metric , localValue ] of Object . entries ( localResults ) ) {
150
+ const npmValue = npmResults [ metric ] || 'N/A' ;
151
+ const localMs = parseFloat ( localValue ) ;
152
+ const npmMs = parseFloat ( npmValue ) ;
153
+
154
+ let comparison = 'N/A' ;
155
+ if ( ! isNaN ( localMs ) && ! isNaN ( npmMs ) ) {
156
+ if ( localMs < npmMs ) {
157
+ const improvement = ( ( npmMs - localMs ) / npmMs * 100 ) . toFixed ( 1 ) ;
158
+ comparison = `🚀 ${ improvement } % faster` ;
159
+ } else if ( localMs > npmMs ) {
160
+ const regression = ( ( localMs - npmMs ) / npmMs * 100 ) . toFixed ( 1 ) ;
161
+ comparison = `🐌 ${ regression } % slower` ;
162
+ } else {
163
+ comparison = '⚖️ same' ;
164
+ }
165
+ }
166
+
167
+ console . log ( `| ${ metric } | ${ localValue } | ${ npmValue } | ${ comparison } |` ) ;
168
+ }
169
+
170
+ console . log ( '\n✅ Benchmark complete!' ) ;
171
+ }
172
+
173
+ main ( ) . catch ( console . error ) ;
0 commit comments