-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.js
More file actions
98 lines (86 loc) · 2.6 KB
/
benchmark.js
File metadata and controls
98 lines (86 loc) · 2.6 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
const { performance } = require('perf_hooks');
async function mockFetch(url, options) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
ok: true,
json: async () => ({ duplicate: Math.random() > 0.8 })
});
}, 50); // mock network delay
});
}
const activities = Array.from({ length: 50 }, (_, i) => ({
name: `Activity ${i}`,
startDate: new Date(),
type: 'RUN',
distance: 5000,
duration: 1800,
averageHr: 150,
hrZones: {}
}));
async function sequentialSync() {
let synced = 0;
let skipped = 0;
let errors = 0;
for (const activity of activities) {
try {
const response = await mockFetch('/api/activities', {});
if (response.ok) {
const data = await response.json();
if (data.duplicate) {
skipped++;
} else {
synced++;
}
} else {
errors++;
}
} catch (error) {
errors++;
}
}
return { synced, errors, skipped };
}
async function parallelSync() {
let synced = 0;
let skipped = 0;
let errors = 0;
const promises = activities.map(async (activity) => {
try {
const response = await mockFetch('/api/activities', {});
if (response.ok) {
const data = await response.json();
if (data.duplicate) {
return { type: 'skipped' };
} else {
return { type: 'synced' };
}
} else {
return { type: 'error' };
}
} catch (error) {
return { type: 'error' };
}
});
const results = await Promise.all(promises);
for (const result of results) {
if (result.type === 'synced') synced++;
else if (result.type === 'skipped') skipped++;
else if (result.type === 'error') errors++;
}
return { synced, errors, skipped };
}
async function run() {
console.log('Running sequential...');
const startSeq = performance.now();
await sequentialSync();
const endSeq = performance.now();
console.log(`Sequential took ${(endSeq - startSeq).toFixed(2)} ms`);
console.log('Running parallel...');
const startPar = performance.now();
await parallelSync();
const endPar = performance.now();
console.log(`Parallel took ${(endPar - startPar).toFixed(2)} ms`);
console.log(`Speedup: ${((endSeq - startSeq) / (endPar - startPar)).toFixed(2)}x`);
}
run();