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
const test_concurrencyDoesntWork = (async () => {
const fetchDelayedResponse = async (index: number) => {
const start = Date.now();
const response = await fetch(
"https://api.val.town/v1/express/alp.proxyFetch1",
{
method: "POST",
body: JSON.stringify({
url: "https://hub.dummyapis.com/delay?seconds=1",
}),
}
);
console.log(
`Returned data for ${index}, took:`,
(Date.now() - start) / 1000,
"seconds"
);
};
const fetchDelayedResponseWithLoadBalancer = async (index: number) => {
const start = Date.now();
const response = await fetch(
`https://api.val.town/v1/express/alp.proxyFetch${index}`,
{
method: "POST",
body: JSON.stringify({
url: "https://hub.dummyapis.com/delay?seconds=2",
}),
}
);
console.log(
`Returned data for ${index}, took:`,
(Date.now() - start) / 1000,
"seconds"
);
};
console.log("w/o load balancer");
await Promise.all([
fetchDelayedResponse(1),
fetchDelayedResponse(2),
fetchDelayedResponse(3),
fetchDelayedResponse(4),
fetchDelayedResponse(5),
]);
console.log("w/ load balancer");
await Promise.all([
fetchDelayedResponseWithLoadBalancer(1),
fetchDelayedResponseWithLoadBalancer(2),
fetchDelayedResponseWithLoadBalancer(3),
fetchDelayedResponseWithLoadBalancer(4),
fetchDelayedResponseWithLoadBalancer(5),
]);
})();
"w/o load balancer" "Returned data for 1, took:" 3.006 "seconds" "Returned data for 3, took:" 3.443 "seconds" "Returned data for 4, took:" 4.427 "seconds" "Returned data for 2, took:" 4.429 "seconds" "Returned data for 5, took:" 4.818 "seconds" "w/ load balancer" "Returned data for 4, took:" 4.232 "seconds" "Returned data for 1, took:" 4.462 "seconds" "Returned data for 5, took:" 5.235 "seconds" "Returned data for 3, took:" 5.316 "seconds" "Returned data for 2, took:" 5.591 "seconds"
0
0
πŸ‘† This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data β€” all from the browser.