A quick vibe-coded site to test response times of PLC.directory mirrors (over 3 attempts)
1import { Mirror } from "@/config/mirrors";
2import { BenchmarkResult, BenchmarkAttempt } from "@/types/benchmark";
3
4const ATTEMPTS_PER_MIRROR = 3;
5
6const singleAttempt = async (
7 url: string,
8 did: string
9): Promise<BenchmarkAttempt> => {
10 const fullUrl = `${url}/${did}`;
11 const startTime = performance.now();
12
13 try {
14 const response = await fetch(fullUrl);
15 const endTime = performance.now();
16 const responseTime = endTime - startTime;
17
18 if (!response.ok) {
19 return {
20 responseTime,
21 status: "error",
22 statusCode: response.status,
23 error: `HTTP ${response.status}`,
24 };
25 }
26
27 await response.json(); // Consume the response
28
29 return {
30 responseTime,
31 status: "success",
32 statusCode: response.status,
33 };
34 } catch (error) {
35 const endTime = performance.now();
36 return {
37 responseTime: endTime - startTime,
38 status: "error",
39 error: error instanceof Error ? error.message : "Unknown error",
40 };
41 }
42};
43
44export const benchmarkMirror = async (
45 mirror: Mirror,
46 did: string
47): Promise<BenchmarkResult> => {
48 const attempts: BenchmarkAttempt[] = [];
49
50 for (let i = 0; i < ATTEMPTS_PER_MIRROR; i++) {
51 const attempt = await singleAttempt(mirror.url, did);
52 attempts.push(attempt);
53 }
54
55 const successfulAttempts = attempts.filter((a) => a.status === "success");
56 const avgResponseTime =
57 successfulAttempts.length > 0
58 ? successfulAttempts.reduce((sum, a) => sum + a.responseTime, 0) /
59 successfulAttempts.length
60 : 0;
61
62 return {
63 mirrorUrl: mirror.url,
64 implementation: mirror.implementation,
65 attempts,
66 avgResponseTime,
67 status: successfulAttempts.length > 0 ? "success" : "error",
68 };
69};
70
71export const benchmarkAllMirrors = async (
72 mirrors: Mirror[],
73 did: string
74): Promise<BenchmarkResult[]> => {
75 const promises = mirrors.map((mirror) => benchmarkMirror(mirror, did));
76 return Promise.all(promises);
77};