forked from DreamLab-AI/origin-logseq-AR
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbenchmark.js
More file actions
182 lines (154 loc) · 5.07 KB
/
benchmark.js
File metadata and controls
182 lines (154 loc) · 5.07 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
#!/usr/bin/env node
/**
* Benchmark script for JavaScript Solid Server
*
* Measures throughput and latency for common operations.
* Run: node benchmark.js
*/
import autocannon from 'autocannon';
import { createServer } from './src/server.js';
import fs from 'fs-extra';
const PORT = 3030;
const DURATION = 10; // seconds per test
const CONNECTIONS = 10;
let server;
let token;
async function setup() {
// Clean data directory
await fs.emptyDir('./data');
// Start server (no logging for clean benchmark)
server = createServer({ logger: false });
await server.listen({ port: PORT, host: '127.0.0.1' });
// Create a test pod
const res = await fetch(`http://127.0.0.1:${PORT}/.pods`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'bench' })
});
const data = await res.json();
token = data.token;
// Create some test resources
for (let i = 0; i < 100; i++) {
await fetch(`http://127.0.0.1:${PORT}/bench/public/item${i}.json`, {
method: 'PUT',
headers: {
'Content-Type': 'application/ld+json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ '@id': `#item${i}`, 'http://example.org/value': i })
});
}
console.log('Setup complete: created pod with 100 resources\n');
}
async function teardown() {
await server.close();
await fs.emptyDir('./data');
}
function runBenchmark(opts) {
return new Promise((resolve) => {
const instance = autocannon({
...opts,
duration: DURATION,
connections: CONNECTIONS,
}, (err, result) => {
resolve(result);
});
autocannon.track(instance, { renderProgressBar: false });
});
}
function formatResult(result) {
return {
'Requests/sec': Math.round(result.requests.average),
'Latency avg': `${result.latency.average.toFixed(2)}ms`,
'Latency p99': `${result.latency.p99.toFixed(2)}ms`,
'Throughput': `${(result.throughput.average / 1024 / 1024).toFixed(2)} MB/s`
};
}
async function benchmarkGET() {
console.log('📖 Benchmarking GET (read resource)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/item0.json`,
method: 'GET'
});
return formatResult(result);
}
async function benchmarkGETContainer() {
console.log('📂 Benchmarking GET (container listing)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/`,
method: 'GET'
});
return formatResult(result);
}
let putCounter = 1000;
async function benchmarkPUT() {
console.log('✏️ Benchmarking PUT (create/update resource)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/new`,
method: 'PUT',
headers: {
'Content-Type': 'application/ld+json',
'Authorization': `Bearer ${token}`
},
setupClient: (client) => {
client.setBody(JSON.stringify({ '@id': '#test', 'http://example.org/v': putCounter++ }));
}
});
return formatResult(result);
}
async function benchmarkPOST() {
console.log('📝 Benchmarking POST (create in container)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/`,
method: 'POST',
headers: {
'Content-Type': 'application/ld+json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ '@id': '#new', 'http://example.org/created': true })
});
return formatResult(result);
}
async function benchmarkOPTIONS() {
console.log('🔍 Benchmarking OPTIONS (discovery)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/item0.json`,
method: 'OPTIONS'
});
return formatResult(result);
}
async function benchmarkHEAD() {
console.log('📋 Benchmarking HEAD (metadata only)...');
const result = await runBenchmark({
url: `http://127.0.0.1:${PORT}/bench/public/item0.json`,
method: 'HEAD'
});
return formatResult(result);
}
async function main() {
console.log('🚀 JavaScript Solid Server Benchmark');
console.log('=====================================');
console.log(`Duration: ${DURATION}s per test, ${CONNECTIONS} concurrent connections\n`);
await setup();
const results = {};
results['GET resource'] = await benchmarkGET();
results['GET container'] = await benchmarkGETContainer();
results['HEAD'] = await benchmarkHEAD();
results['OPTIONS'] = await benchmarkOPTIONS();
results['PUT'] = await benchmarkPUT();
results['POST'] = await benchmarkPOST();
console.log('\n📊 Results Summary');
console.log('==================\n');
// Print as table
console.log('| Operation | Req/sec | Avg Latency | p99 Latency |');
console.log('|-----------|---------|-------------|-------------|');
for (const [op, data] of Object.entries(results)) {
console.log(`| ${op.padEnd(13)} | ${String(data['Requests/sec']).padStart(7)} | ${data['Latency avg'].padStart(11)} | ${data['Latency p99'].padStart(11)} |`);
}
console.log('\n');
await teardown();
// Output JSON for README
console.log('JSON results:');
console.log(JSON.stringify(results, null, 2));
}
main().catch(console.error);