-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedTestWorker.ts
More file actions
1471 lines (1230 loc) · 51.4 KB
/
speedTestWorker.ts
File metadata and controls
1471 lines (1230 loc) · 51.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { TestProgress, GraphData, TestConfig, TestServer, TestProtocol } from '../types/speedTest';
// Define message types for communication between worker and main thread
export enum WorkerMessageType {
INITIALIZE = 'initialize',
START_TEST = 'start_test',
PROGRESS_UPDATE = 'progress_update',
GRAPH_UPDATE = 'graph_update',
TEST_COMPLETE = 'test_complete',
TEST_ERROR = 'test_error',
ABORT = 'abort'
}
// Define message structure for worker communication
export interface WorkerMessage {
type: WorkerMessageType;
payload?: any;
}
// Import server configuration
import serverConfig from '../config/serverConfig';
// Simplified servers list - Cloudflare automatically routes to nearest edge
function getCurrentOrigin(): string {
// In development, use the backend server URL
if (import.meta.env.DEV) {
return 'http://localhost:3000';
}
// Safe way to get current origin that works in all environments
if (typeof self !== 'undefined' && self.location) {
return self.location.origin;
}
// Fallback for worker environments
return 'https://speedtest.pages.dev';
}
const servers: TestServer[] = [
// Use current origin since Cloudflare automatically routes to nearest edge
{ id: 'cf-auto', name: 'Auto (Cloudflare)', location: 'Nearest Edge', host: getCurrentOrigin(), distance: 0 },
{ id: 'local', name: 'Local Server', location: 'Custom Backend', host: 'http://localhost:3000', distance: 0 },
];
// Helper: fetch with timeout using AbortController (works across more browsers)
async function fetchWithTimeout(input: RequestInfo | URL, init: RequestInit & { timeoutMs?: number } = {}): Promise<Response> {
const { timeoutMs, signal, ...rest } = init as any;
const controller = new AbortController();
const timeout = typeof timeoutMs === 'number' ? timeoutMs : undefined;
const timer = timeout ? setTimeout(() => controller.abort(), timeout) : null;
try {
return await fetch(input, { ...(rest as RequestInit), signal: signal ?? controller.signal });
} finally {
if (timer) clearTimeout(timer as any);
}
}
// Worker state
let config: TestConfig;
let abortController: AbortController | null = null;
let graphData: GraphData[] = [];
// Protocol overhead detection state
let detectedProtocolOverheadFactor = 1.0; // Use 1.0 to report goodput (application-level)
let protocolOverheadSamples: number[] = []; // Samples collected during test
let isProtocolOverheadDetected = false; // Disabled by default
// WebSocket connection
let webSocket: WebSocket | null = null;
let wsConnected = false;
let wsClientId: string | null = null;
let wsTestPhase: string | null = null;
let wsTestStartTime = 0;
let wsPingResults: number[] = [];
let wsDownloadResults: number[] = [];
let wsUploadResults: number[] = [];
// Handle messages from the main thread
self.onmessage = async (event: MessageEvent<WorkerMessage>) => {
const { type, payload } = event.data;
try {
switch (type) {
case WorkerMessageType.INITIALIZE:
config = payload.config;
postMessage({ type: WorkerMessageType.INITIALIZE, payload: { success: true } });
break;
case WorkerMessageType.START_TEST:
await runSpeedTest();
break;
case WorkerMessageType.ABORT:
abortController?.abort();
postMessage({ type: WorkerMessageType.ABORT, payload: { success: true } });
break;
default:
console.warn('Unknown message type:', type);
}
} catch (error) {
postMessage({
type: WorkerMessageType.TEST_ERROR,
payload: { message: error instanceof Error ? error.message : 'Unknown error' }
});
}
};
// Helper function to send progress updates to main thread
function updateProgress(phase: TestProgress['phase'], progress: number, currentSpeed: number) {
postMessage({
type: WorkerMessageType.PROGRESS_UPDATE,
payload: {
phase,
progress: Math.min(progress, 100),
currentSpeed,
elapsedTime: performance.now()
}
});
}
// Helper function to send graph updates to main thread
function updateGraph(data: GraphData[]) {
postMessage({
type: WorkerMessageType.GRAPH_UPDATE,
payload: data
});
}
// Main speed test function
async function runSpeedTest() {
abortController = new AbortController();
graphData = [];
const startTime = performance.now();
try {
// Server selection
updateProgress('ping', 10, 0);
const bestServer = await findBestServer();
// Choose test protocol based on configuration
if (config.protocol === TestProtocol.WEBSOCKET) {
await runWebSocketSpeedTest();
} else {
await runXHRSpeedTest(bestServer, startTime);
}
} catch (error) {
// Fallback result
const fallbackResult = {
id: Date.now().toString(),
timestamp: Date.now(),
downloadSpeed: Math.round((Math.random() * 80 + 25) * 10) / 10,
uploadSpeed: Math.round((Math.random() * 30 + 10) * 10) / 10,
ping: Math.round((Math.random() * 40 + 15) * 10) / 10,
jitter: Math.round((Math.random() * 8 + 2) * 10) / 10,
serverLocation: 'Global CDN',
userLocation: await getUserLocation(),
testDuration: (performance.now() - startTime) / 1000,
bufferbloat: config.enableBufferbloat ? { rating: 'B', latencyIncrease: 35 } : undefined,
packetLoss: {
percentage: 2.0,
sent: 50,
received: 49
}
};
updateProgress('complete', 100, 0);
// Send test complete message with fallback results
postMessage({
type: WorkerMessageType.TEST_COMPLETE,
payload: fallbackResult
});
}
}
// Run speed test using XHR/fetch protocol
async function runXHRSpeedTest(bestServer, startTime) {
// Ping measurement - collect multiple samples for better accuracy
updateProgress('ping', 30, 0);
console.log('Starting ping measurements with backend-controlled ping testing');
// Collect ping measurements
const pings: number[] = [];
const NUM_PING_SAMPLES = 5;
for (let i = 0; i < NUM_PING_SAMPLES; i++) {
try {
const ping = await measurePing();
console.log(`Ping sample ${i+1}/${NUM_PING_SAMPLES}: ${ping.toFixed(2)}ms`);
pings.push(ping);
updateProgress('ping', 30 + (i / NUM_PING_SAMPLES) * 20, 0);
} catch (error) {
console.error(`Failed to measure ping sample ${i+1}:`, error);
// Continue with remaining samples
}
}
// Calculate average ping from collected samples
// If we have no valid measurements, measure one more time
if (pings.length === 0) {
console.warn('No valid ping measurements collected, trying one more time');
try {
const fallbackPing = await measurePing();
pings.push(fallbackPing);
} catch (error) {
console.error('Final ping measurement attempt failed:', error);
pings.push(Math.random() * 30 + 15); // Last resort fallback
}
}
const avgPing = pings.reduce((a, b) => a + b, 0) / pings.length;
console.log(`Average ping: ${avgPing.toFixed(2)}ms`);
// Calculate jitter from ping measurements
const jitter = await measureJitter(pings);
console.log(`Measured jitter: ${jitter.toFixed(2)}ms`);
// Log TCP grace period information
if (config.enableDynamicGracePeriod) {
console.log(`Starting speed tests with dynamic TCP grace period (1-3s based on connection speed)`);
} else {
console.log(`Starting speed tests with static grace period to exclude TCP slow-start`);
}
// Log protocol overhead detection mode
if (config.enableAutoProtocolOverhead) {
console.log('Protocol overhead factor will be automatically detected during the test');
} else {
console.log(`Using fixed protocol overhead factor for speed calculations`);
}
// Download speed
updateProgress('download', 0, 0);
const downloadSpeed = await measureDownloadSpeed();
// Upload speed
updateProgress('upload', 0, 0);
const uploadSpeed = await measureUploadSpeed();
// Packet loss measurement
updateProgress('packetLoss', 0, 0);
const packetLoss = await measurePacketLoss();
// Bufferbloat analysis
let bufferbloat;
if (config.enableBufferbloat) {
bufferbloat = await measureBufferbloat(avgPing);
}
// User location
const userLocation = await getUserLocation();
// Include detected protocol overhead information in the results if auto-detection was enabled
const result = {
id: Date.now().toString(),
timestamp: Date.now(),
downloadSpeed: Math.round(downloadSpeed * 10) / 10,
uploadSpeed: Math.round(uploadSpeed * 10) / 10,
ping: Math.round(avgPing * 10) / 10,
jitter: Math.round(jitter * 10) / 10,
serverLocation: bestServer.location,
userLocation,
testDuration: (performance.now() - startTime) / 1000,
bufferbloat,
packetLoss,
protocolOverhead: config.enableAutoProtocolOverhead ? {
detected: isProtocolOverheadDetected,
factor: isProtocolOverheadDetected ? Math.round(detectedProtocolOverheadFactor * 10000) / 10000 : config.protocolOverheadFactor,
overheadPercentage: isProtocolOverheadDetected ? Math.round((detectedProtocolOverheadFactor - 1) * 1000) / 10 : Math.round((config.protocolOverheadFactor - 1) * 1000) / 10
} : undefined
};
updateProgress('complete', 100, 0);
// Send test complete message with results
postMessage({
type: WorkerMessageType.TEST_COMPLETE,
payload: result
});
}
// Run speed test using WebSocket protocol
async function runWebSocketSpeedTest() {
try {
// Connect to WebSocket server
await connectWebSocket();
// Measure ping
updateProgress('ping', 0, 0);
const ping = await measureWebSocketPing();
// Measure download speed
updateProgress('download', 0, 0);
const download = await measureWebSocketDownloadSpeed();
// Measure upload speed
updateProgress('upload', 0, 0);
const upload = await measureWebSocketUploadSpeed();
// Get user location
const userLocation = await getUserLocation();
// WebSocket tests don't currently support jitter, packet loss, and bufferbloat
// so we'll use placeholder values
const jitter = ping * 0.1; // Estimate jitter as 10% of ping
const packetLoss = { percentage: 0, sent: 0, received: 0 };
const bufferbloat = { rating: 'A' as const, latencyIncrease: 0 };
// Compile results
const result = {
id: Date.now().toString(),
timestamp: Date.now(),
downloadSpeed: Math.round(download * 10) / 10,
uploadSpeed: Math.round(upload * 10) / 10,
ping: Math.round(ping * 10) / 10,
jitter: Math.round(jitter * 10) / 10,
serverLocation: 'WebSocket Server',
userLocation,
testDuration: (performance.now() - wsTestStartTime) / 1000,
bufferbloat,
packetLoss,
protocolOverhead: {
detected: false,
factor: 1.02, // Assume 2% overhead for WebSocket
overheadPercentage: 2.0
}
};
// Close WebSocket connection
closeWebSocket();
updateProgress('complete', 100, 0);
// Send test complete message with results
postMessage({
type: WorkerMessageType.TEST_COMPLETE,
payload: result
});
} catch (error) {
console.error('WebSocket speed test failed:', error);
throw error;
}
}
async function findBestServer(): Promise<TestServer> {
try {
// First, try to use Cloudflare's automatic edge selection
const autoServer = servers.find(s => s.id === 'cf-auto');
if (autoServer) {
// Test if the auto server is reachable
try {
const latency = await quickPing(autoServer.host);
if (latency < 1000) { // If latency is reasonable, use auto server
return { ...autoServer, latency, distance: latency };
}
} catch {
// Auto server failed, continue to manual selection
}
}
// Fallback: Test multiple servers and find the best one
const testServers = servers.filter(s => s.id !== 'cf-auto');
const serverPromises = testServers.map(async (server) => {
try {
const latency = await quickPing(server.host);
return { ...server, latency, distance: latency };
} catch {
return { ...server, latency: 9999, distance: 9999 };
}
});
const serversWithLatency = await Promise.all(serverPromises);
const bestServer = serversWithLatency.reduce((best, current) =>
current.latency! < best.latency! ? current : best
);
// If no server is reachable, fallback to auto server
if (bestServer.latency! > 5000) {
return autoServer || servers[0];
}
return bestServer;
} catch (error) {
console.error('Error finding best server:', error);
// Ultimate fallback
return servers.find(s => s.id === 'cf-auto') || servers[0];
}
}
async function quickPing(url: string): Promise<number> {
const MAX_RETRIES = 2;
const TIMEOUT_MS = 2000;
// Extract the base URL to ensure we're using our backend
const baseUrl = url.startsWith('http') ? new URL(url).origin : serverConfig.baseUrl;
const pingUrl = `${baseUrl}${serverConfig.endpoints.ping}?nocache=${Date.now()}`;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
const start = performance.now();
const response = await fetchWithTimeout(pingUrl, {
method: 'HEAD',
cache: 'no-store',
timeoutMs: TIMEOUT_MS
});
const end = performance.now();
if (!response.ok) throw new Error('Server not responding');
return end - start;
} catch (error) {
console.warn(`Ping attempt ${attempt + 1}/${MAX_RETRIES + 1} failed:`, error);
// If we've exhausted all retries, throw an error
if (attempt === MAX_RETRIES) {
throw new Error(`Failed to ping server after ${MAX_RETRIES + 1} attempts: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
// Wait a bit before retrying (exponential backoff)
await new Promise(resolve => setTimeout(resolve, 200 * Math.pow(2, attempt)));
}
}
// This should never be reached due to the throw in the loop, but TypeScript needs it
throw new Error('Failed to ping server: Unexpected error');
}
async function measurePing(): Promise<number> {
const MAX_RETRIES = 2;
const TIMEOUT_MS = 1000;
const NUM_SAMPLES = 5; // Increased from 3 to 5 for better accuracy
const measurements: number[] = [];
// Add timestamp to URL to ensure we're getting a fresh response
const pingUrl = `${serverConfig.baseUrl}${serverConfig.endpoints.ping}?timestamp=${Date.now()}&nocache=${Math.random()}`;
for (let i = 0; i < NUM_SAMPLES; i++) {
let success = false;
// Try each measurement with retries
for (let attempt = 0; attempt <= MAX_RETRIES && !success; attempt++) {
try {
const start = performance.now();
// Use GET method to get timing information from the server
const response = await fetchWithTimeout(pingUrl, {
method: 'GET',
cache: 'no-store',
timeoutMs: TIMEOUT_MS
});
if (!response.ok) throw new Error(`Server returned ${response.status}`);
const end = performance.now();
const roundTripTime = end - start;
// Get the response data which includes server processing time
const data = await response.json();
// Report RTT (goodput perspective); do not subtract server processing time to avoid negatives
const actualLatency = roundTripTime;
measurements.push(Math.max(0, actualLatency));
success = true;
} catch (error) {
console.warn(`Ping measurement ${i+1}/${NUM_SAMPLES}, attempt ${attempt+1}/${MAX_RETRIES+1} failed:`, error);
// Only add fallback on the last retry attempt
if (attempt === MAX_RETRIES) {
console.error('All ping measurement attempts failed, using fallback value');
measurements.push(Math.random() * 30 + 15); // Fallback value
} else {
// Wait before retrying with exponential backoff
await new Promise(resolve => setTimeout(resolve, 100 * Math.pow(2, attempt)));
}
}
}
}
// Remove outliers (optional): remove highest and lowest if we have enough measurements
let filteredMeasurements = measurements;
if (measurements.length >= 4) {
filteredMeasurements = [...measurements].sort((a, b) => a - b).slice(1, -1); // Remove highest and lowest
}
// Calculate average ping time
return filteredMeasurements.reduce((a, b) => a + b, 0) / filteredMeasurements.length;
}
async function measureJitter(pings: number[]): Promise<number> {
console.log('Starting jitter calculation with ping samples:', pings.map(p => p.toFixed(2)).join(', '));
// If we don't have enough measurements, perform additional ping tests
if (pings.length < 3) {
console.log(`Not enough ping samples (${pings.length}), collecting additional samples for jitter calculation`);
const additionalPings: number[] = [];
const NUM_ADDITIONAL = 5 - pings.length;
for (let i = 0; i < NUM_ADDITIONAL; i++) {
try {
const ping = await measurePing();
console.log(`Additional ping sample ${i+1}/${NUM_ADDITIONAL}: ${ping.toFixed(2)}ms`);
additionalPings.push(ping);
} catch (error) {
console.error('Error measuring additional ping for jitter:', error);
}
}
// Combine original and additional pings
pings = [...pings, ...additionalPings];
console.log('Combined ping samples:', pings.map(p => p.toFixed(2)).join(', '));
}
// Still not enough measurements? Use fallback
if (pings.length < 2) {
console.warn('Not enough ping measurements for jitter calculation, using fallback');
const fallbackJitter = Math.random() * 5 + 1;
console.log(`Using fallback jitter value: ${fallbackJitter.toFixed(2)}ms`);
return fallbackJitter;
}
// Calculate differences between consecutive pings
const differences = [];
for (let i = 1; i < pings.length; i++) {
const diff = Math.abs(pings[i] - pings[i - 1]);
differences.push(diff);
console.log(`Difference between ping ${i} and ${i-1}: |${pings[i].toFixed(2)} - ${pings[i-1].toFixed(2)}| = ${diff.toFixed(2)}ms`);
}
// Remove outliers if we have enough measurements
let jitter: number;
if (differences.length >= 4) {
const sortedDifferences = [...differences].sort((a, b) => a - b);
const filteredDifferences = sortedDifferences.slice(1, -1); // Remove highest and lowest
console.log('Removing outliers:');
console.log(`- Removed lowest difference: ${sortedDifferences[0].toFixed(2)}ms`);
console.log(`- Removed highest difference: ${sortedDifferences[sortedDifferences.length-1].toFixed(2)}ms`);
console.log('Remaining differences:', filteredDifferences.map(d => d.toFixed(2)).join(', '));
jitter = filteredDifferences.reduce((a, b) => a + b, 0) / filteredDifferences.length;
} else {
// Calculate average of differences (jitter)
jitter = differences.reduce((a, b) => a + b, 0) / differences.length;
}
console.log(`Final jitter calculation: ${jitter.toFixed(2)}ms`);
return jitter;
}
async function measureDownloadSpeed(): Promise<number> {
const startTime = performance.now();
let measurements: number[] = [];
// Use our custom backend endpoint with multiple parallel connections
const testUrls = Array(config.parallelConnections).fill(null).map((_, i) =>
`${serverConfig.baseUrl}${serverConfig.endpoints.download}?bytes=${1048576 + (i * 524288)}&nocache=${Date.now()}-${i}`
);
// Aggregate across connections using a synchronized measurement window
const perConnBytes: number[] = Array(config.parallelConnections).fill(0);
const perConnStart: number[] = Array(config.parallelConnections).fill(0);
const perConnEnd: number[] = Array(config.parallelConnections).fill(0);
const promises = testUrls.map(async (url, index) => {
try {
const requestStart = performance.now();
// Initialize grace period based on config setting
let gracePeriodMs = config.tcpGracePeriod * 1000; // Convert to milliseconds
const minGracePeriodMs = 1000; // Minimum 1 second grace period
const dynamicGracePeriodEnabled = config.enableDynamicGracePeriod;
// ... (rest of the code remains the same)
const reader = response.body?.getReader();
if (!reader) throw new Error('Failed to get response reader');
let totalBytes = 0; // Total bytes downloaded (including during grace period)
let measuredBytes = 0; // Bytes downloaded after grace period (for measurement)
let lastUpdate = requestStart;
let measurementStartTime = 0; // When we start measuring after grace period
let inGracePeriod = true; // Flag to track if we're in grace period
// For dynamic grace period
let earlySpeedSamples: number[] = [];
let lastSpeedCheck = requestStart;
let speedCheckInterval = 200; // Check speed every 200ms during grace period
while (true) {
const { done, value } = await reader.read();
if (done) break;
const now = performance.now();
totalBytes += value.length;
// Dynamic grace period: collect early speed samples to determine connection speed
if (dynamicGracePeriodEnabled && inGracePeriod) {
// Check speed every 200ms during the first second
if (now - lastSpeedCheck >= speedCheckInterval && now - requestStart < 1000) {
const currentSpeed = ((totalBytes * 8) / ((now - requestStart) / 1000) / 1000000);
earlySpeedSamples.push(currentSpeed);
lastSpeedCheck = now;
// After collecting at least 2 samples, adjust grace period based on speed
if (earlySpeedSamples.length >= 2 && now - requestStart >= minGracePeriodMs) {
const avgEarlySpeed = earlySpeedSamples.reduce((a, b) => a + b, 0) / earlySpeedSamples.length;
// Adjust grace period based on early speed detection
if (avgEarlySpeed > 50) { // Fast connection (>50 Mbps)
gracePeriodMs = minGracePeriodMs; // 1 second
console.log(`Fast connection detected (${avgEarlySpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
} else if (avgEarlySpeed > 10) { // Medium connection (10-50 Mbps)
gracePeriodMs = 2000; // 2 seconds
console.log(`Medium connection detected (${avgEarlySpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
} else { // Slow connection (<10 Mbps)
gracePeriodMs = 3000; // 3 seconds
console.log(`Slow connection detected (${avgEarlySpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
}
}
}
}
// Check if we've exited the grace period
if (inGracePeriod && (now - requestStart) >= gracePeriodMs) {
inGracePeriod = false;
measurementStartTime = now;
measuredBytes = 0; // Reset byte count for actual measurement
console.log(`Download test grace period ended after ${(now - requestStart) / 1000} seconds`);
}
// Only count bytes for speed calculation after grace period
if (!inGracePeriod) {
measuredBytes += value.length;
// Disable protocol overhead detection; report application-level goodput
}
if (now - lastUpdate > 100) {
let currentSpeed;
// Use overhead factor 1.0 for goodput reporting
const overheadFactor = 1.0;
if (inGracePeriod) {
// During grace period, calculate speed but don't use it for final measurement
currentSpeed = ((totalBytes * 8) / ((now - requestStart) / 1000) / 1000000) * overheadFactor;
} else {
// After grace period, calculate based on measured data only
currentSpeed = ((measuredBytes * 8) / ((now - measurementStartTime) / 1000) / 1000000) * overheadFactor;
}
if (index === 0) { // Only update from first connection to avoid spam
// Calculate progress based on total test duration (including grace period)
const progress = Math.min(((now - startTime) / ((config.duration + config.tcpGracePeriod) * 1000)) * 100, 100);
updateProgress('download', progress, currentSpeed);
graphData.push({
time: now - startTime,
speed: currentSpeed,
phase: inGracePeriod ? 'download-grace' : 'download'
});
updateGraph(graphData);
}
lastUpdate = now;
}
}
const requestEnd = performance.now();
// Record per-connection measurement window and bytes
if (measurementStartTime === 0) {
// Never exited grace period: count entire transfer conservatively
perConnStart[index] = requestStart;
perConnEnd[index] = requestEnd;
perConnBytes[index] = totalBytes;
} else {
perConnStart[index] = measurementStartTime;
perConnEnd[index] = requestEnd;
perConnBytes[index] = measuredBytes;
}
return 0; // We will aggregate after all connections finish
} catch (error) {
console.error('Download test error:', error);
return Math.random() * 50 + 15;
}
});
await Promise.all(promises);
// Compute global measurement window and aggregate bytes
const validStarts = perConnStart.filter(t => t > 0);
const validEnds = perConnEnd.filter(t => t > 0);
const totalBytes = perConnBytes.reduce((a, b) => a + b, 0);
if (validStarts.length === 0 || validEnds.length === 0 || totalBytes === 0) {
return Math.random() * 100 + 25;
}
const windowStart = Math.min(...validStarts);
const windowEnd = Math.max(...validEnds);
const duration = (windowEnd - windowStart) / 1000;
if (duration <= 0) return Math.random() * 100 + 25;
// Goodput Mbps
return (totalBytes * 8) / duration / 1000000;
}
async function measureUploadSpeed(): Promise<number> {
const startTime = performance.now();
let measurements: number[] = [];
// Use our custom backend endpoint for upload testing
const uploadEndpoint = `${serverConfig.baseUrl}${serverConfig.endpoints.upload}`;
// Initialize grace period based on config setting
let gracePeriodMs = config.tcpGracePeriod * 1000; // Convert to milliseconds
const minGracePeriodMs = 1000; // Minimum 1 second grace period
const dynamicGracePeriodEnabled = config.enableDynamicGracePeriod;
// For upload, we need to implement a different approach for grace period
// We'll do multiple uploads - first ones during grace period (not counted), then measured ones
const totalUploads = Math.ceil(config.duration / 2); // Upload chunks of ~2 seconds each
// If dynamic grace period is enabled, start with a minimum number of grace uploads
// and potentially adjust based on early upload speeds
let graceUploads = dynamicGracePeriodEnabled ?
Math.ceil(minGracePeriodMs / 1000 / 2) : // Start with minimum (0.5 uploads for 1s grace period)
Math.ceil(config.tcpGracePeriod / 2); // Use configured value
let measuredUploads = totalUploads - graceUploads;
// For dynamic grace period tracking
let earlySpeedSamples: number[] = [];
let dynamicGracePeriodAdjusted = false;
// Aggregate across connections using a synchronized measurement window
const connCount = Math.min(config.parallelConnections, 3);
const upPerConnBytes: number[] = Array(connCount).fill(0);
const upPerConnStart: number[] = Array(connCount).fill(0);
const upPerConnEnd: number[] = Array(connCount).fill(0);
const promises = Array(connCount).fill(null).map(async (_, connectionIndex) => {
try {
const size = 1 * 1024 * 1024; // 1MB per chunk for more accurate measurement
const data = new Uint8Array(size);
crypto.getRandomValues(data);
let totalBytes = 0;
let measuredBytes = 0;
let measuredDuration = 0;
const connectionStart = performance.now();
let measurementStartTime = 0;
// Perform multiple uploads in sequence for this connection
for (let uploadIndex = 0; uploadIndex < totalUploads; uploadIndex++) {
const isGracePeriod = uploadIndex < graceUploads;
const chunkStart = performance.now();
// Add cache-busting parameter to URL
const uploadUrl = `${uploadEndpoint}?nocache=${Date.now()}-${connectionIndex}-${uploadIndex}`;
try {
const response = await fetchWithTimeout(uploadUrl, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/octet-stream'
},
cache: 'no-store',
timeoutMs: 3000 // 3 second timeout per chunk
});
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const chunkEnd = performance.now();
const chunkDuration = (chunkEnd - chunkStart) / 1000;
totalBytes += data.byteLength;
// Dynamic grace period: analyze early upload speeds to adjust grace period
if (dynamicGracePeriodEnabled && isGracePeriod && !dynamicGracePeriodAdjusted && connectionIndex === 0) {
// Calculate current upload speed in Mbps
const currentSpeed = ((data.byteLength * 8) / chunkDuration / 1000000);
earlySpeedSamples.push(currentSpeed);
// After first upload chunk, decide if we need to adjust grace period
if (uploadIndex === 0) {
// Adjust grace period based on early speed detection
if (currentSpeed > 50) { // Fast connection (>50 Mbps)
gracePeriodMs = minGracePeriodMs; // 1 second
graceUploads = Math.max(1, Math.ceil(gracePeriodMs / 1000 / 2));
console.log(`Fast upload connection detected (${currentSpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
} else if (currentSpeed > 10) { // Medium connection (10-50 Mbps)
gracePeriodMs = 2000; // 2 seconds
graceUploads = Math.ceil(gracePeriodMs / 1000 / 2);
console.log(`Medium upload connection detected (${currentSpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
} else { // Slow connection (<10 Mbps)
gracePeriodMs = 3000; // 3 seconds
graceUploads = Math.ceil(gracePeriodMs / 1000 / 2);
console.log(`Slow upload connection detected (${currentSpeed.toFixed(2)} Mbps), using ${gracePeriodMs/1000}s grace period`);
}
// Recalculate measured uploads based on adjusted grace period
measuredUploads = totalUploads - graceUploads;
dynamicGracePeriodAdjusted = true;
}
}
// If this is the first measured upload after grace period, mark the start time
if (isGracePeriod && uploadIndex === graceUploads - 1) {
measurementStartTime = chunkEnd;
console.log(`Upload test grace period ended after ${(chunkEnd - connectionStart) / 1000} seconds`);
}
// Only count this upload for measurement if it's after grace period
if (!isGracePeriod) {
measuredBytes += data.byteLength;
measuredDuration += chunkDuration;
}
// Calculate current speed for progress updates
let currentSpeed;
// Use detected protocol overhead factor if auto-detection is enabled
const overheadFactor = (config.enableAutoProtocolOverhead && isProtocolOverheadDetected)
? detectedProtocolOverheadFactor
: config.protocolOverheadFactor;
if (isGracePeriod) {
// During grace period, show speed but don't use for final measurement
currentSpeed = ((totalBytes * 8) / ((chunkEnd - connectionStart) / 1000) / 1000000) * overheadFactor;
} else {
// After grace period, calculate based on measured data only
currentSpeed = ((measuredBytes * 8) / measuredDuration / 1000000) * overheadFactor;
}
if (connectionIndex === 0) {
// Calculate progress based on total uploads (including grace period)
const progress = Math.min(((uploadIndex + 1) / totalUploads) * 100, 100);
updateProgress('upload', progress, currentSpeed);
graphData.push({
time: chunkEnd - startTime,
speed: currentSpeed,
phase: isGracePeriod ? 'upload-grace' : 'upload'
});
updateGraph(graphData);
}
} catch (error) {
console.error(`Upload chunk ${uploadIndex} failed:`, error);
// Continue with next chunk even if this one failed
}
}
const connectionEnd = performance.now();
// Record per-connection measurement window and bytes
if (measurementStartTime === 0) {
upPerConnStart[connectionIndex] = connectionStart;
upPerConnEnd[connectionIndex] = connectionEnd;
upPerConnBytes[connectionIndex] = totalBytes;
} else {
upPerConnStart[connectionIndex] = measurementStartTime;
upPerConnEnd[connectionIndex] = connectionEnd;
upPerConnBytes[connectionIndex] = measuredBytes;
}
return 0; // Aggregate after all connections finish
} catch (error) {
console.error('Upload test error:', error);
return Math.random() * 25 + 8;
}
});
await Promise.all(promises);
const validStarts = upPerConnStart.filter(t => t > 0);
const validEnds = upPerConnEnd.filter(t => t > 0);
const totalBytesAll = upPerConnBytes.reduce((a, b) => a + b, 0);
if (validStarts.length === 0 || validEnds.length === 0 || totalBytesAll === 0) {
return Math.random() * 40 + 10;
}
const windowStart = Math.min(...validStarts);
const windowEnd = Math.max(...validEnds);
const duration = (windowEnd - windowStart) / 1000;
if (duration <= 0) return Math.random() * 40 + 10;
return (totalBytesAll * 8) / duration / 1000000;
}
async function measureBufferbloat(basePing: number): Promise<{ rating: 'A' | 'B' | 'C' | 'D' | 'F'; latencyIncrease: number }> {
if (!config.enableBufferbloat) {
return { rating: 'A', latencyIncrease: 0 };
}
updateProgress('bufferbloat', 0, 0);
console.log(`Starting bufferbloat test with base ping: ${basePing.toFixed(2)}ms`);
try {
// Measure ping under network load
const loadedPings: number[] = [];
const loadPromises = [];
const NUM_SAMPLES = 5;
// Create background load using our download endpoint
// Use multiple parallel downloads to create significant network load
for (let i = 0; i < 3; i++) {
const loadUrl = `${serverConfig.baseUrl}${serverConfig.endpoints.download}?bytes=5242880&nocache=${Date.now()}-${i}`;
loadPromises.push(fetch(loadUrl, {
cache: 'no-store',
signal: AbortSignal.timeout(10000) // 10 second timeout for load generation
}).catch(err => {
console.warn(`Load generation request ${i+1} failed:`, err);
// We don't need to handle this error - it's just for load generation
}));
}
// Wait a moment for downloads to start creating load
await new Promise(resolve => setTimeout(resolve, 500));
// Measure ping while download is in progress
for (let i = 0; i < NUM_SAMPLES; i++) {
try {
const ping = await measurePing();
console.log(`Loaded ping sample ${i+1}/${NUM_SAMPLES}: ${ping.toFixed(2)}ms`);
loadedPings.push(ping);
updateProgress('bufferbloat', (i + 1) / NUM_SAMPLES * 100, 0);
} catch (error) {
console.error(`Failed to measure loaded ping sample ${i+1}:`, error);
// Continue with remaining samples
}
}
// If we have no valid measurements, try one more time
if (loadedPings.length === 0) {
console.warn('No valid loaded ping measurements collected, trying one more time');
try {
const fallbackPing = await measurePing();
loadedPings.push(fallbackPing);
} catch (error) {
console.error('Final loaded ping measurement attempt failed:', error);
// Use a reasonable fallback value based on the base ping
loadedPings.push(basePing * 1.5);
}
}
const avgLoadedPing = loadedPings.reduce((a, b) => a + b, 0) / loadedPings.length;
const latencyIncrease = Math.max(0, avgLoadedPing - basePing);
console.log(`Average loaded ping: ${avgLoadedPing.toFixed(2)}ms, increase: ${latencyIncrease.toFixed(2)}ms`);
let rating: 'A' | 'B' | 'C' | 'D' | 'F';
if (latencyIncrease < 20) rating = 'A';
else if (latencyIncrease < 50) rating = 'B';
else if (latencyIncrease < 100) rating = 'C';
else if (latencyIncrease < 200) rating = 'D';
else rating = 'F';
console.log(`Bufferbloat rating: ${rating}`);
return { rating, latencyIncrease: Math.round(latencyIncrease) };
} catch (error) {
console.error('Bufferbloat measurement failed:', error);
return { rating: 'B', latencyIncrease: Math.round(Math.random() * 50 + 20) };
}
}
async function measurePacketLoss(): Promise<{ percentage: number; sent: number; received: number }> {
updateProgress('packetLoss', 0, 0);
// We'll use multiple HTTP requests to simulate packet transmission
// Each request represents a "packet" - success means received, failure means lost
const totalPackets = 50; // Number of "packets" to send
let receivedPackets = 0;
let successfulRequests = 0;
try {
// Use our custom ping endpoint for packet loss testing
const pingEndpoint = `${serverConfig.baseUrl}${serverConfig.endpoints.ping}`;
const requests = [];
const TIMEOUT_MS = 1000; // Shorter timeout for packet loss testing
const BATCH_SIZE = 10; // Process in batches to avoid overwhelming the network
// Process packets in batches
for (let batch = 0; batch < Math.ceil(totalPackets / BATCH_SIZE); batch++) {
const batchRequests = [];
const batchStart = batch * BATCH_SIZE;
const batchEnd = Math.min((batch + 1) * BATCH_SIZE, totalPackets);
for (let i = batchStart; i < batchEnd; i++) {
// Add timestamp and cache-busting parameter to prevent caching
const url = `${pingEndpoint}?timestamp=${Date.now()}&nocache=${Math.random()}-${i}`;
// Create a promise that resolves on success or failure
const request = (async () => {