Skip to content

Commit 72c3574

Browse files
committed
chore: upgrade odin
1 parent 7b7a95f commit 72c3574

File tree

4 files changed

+124
-57
lines changed

4 files changed

+124
-57
lines changed

.github/odin.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/sh
22

3-
VERSION=dev-2022-08
3+
VERSION=dev-2022-09
44
FILE_NAME=odin-ubuntu-amd64-$VERSION.zip
55

66
mkdir /tmp/odin
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const host = "localhost";
2+
3+
async function send(api, value) {
4+
let r = await fetch(api, {
5+
method: "POST",
6+
body: JSON.stringify({ value: value }),
7+
});
8+
let text = await r.text();
9+
console.log(`response: ${text}`);
10+
return parseInt(text);
11+
}
12+
13+
async function sendAsync(api, value) {
14+
while (true) {
15+
try {
16+
let v = await send(api, value);
17+
return v;
18+
} catch (e) {}
19+
}
20+
}
21+
22+
async function calculateSum(port, n) {
23+
const api = `http://${host}:${port}/`;
24+
var sum = +0;
25+
var tasks = [];
26+
for (var i = 1; i <= n; i++) {
27+
tasks.push(sendAsync(api, i));
28+
}
29+
for (var i = 0; i < n; i++) {
30+
sum += await tasks[i];
31+
}
32+
console.log(sum);
33+
}
34+
35+
function runServer(port) {
36+
return Bun.serve({
37+
async fetch(req) {
38+
if (req.method === "POST") {
39+
const payload = await req.json();
40+
console.log(`request: ${payload.value}`);
41+
return new Response(payload.value.toString());
42+
} else {
43+
return new Response(`${req.method} not supported`);
44+
}
45+
},
46+
error(err) {
47+
return new Response("uh oh! :(\n" + err.toString(), { status: 500 });
48+
},
49+
development: false,
50+
port: port,
51+
});
52+
}
53+
54+
async function main() {
55+
const args = process.argv.slice(2);
56+
const n = +args[0] || 10;
57+
const port = 20000 + Math.floor(Math.random() * 30000);
58+
const server = runServer(port);
59+
await calculateSum(port, n);
60+
server.stop();
61+
}
62+
63+
main();

bench/algorithm/http-server/1.js

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,77 @@
11
const http = require("http");
22

3-
const host = 'localhost';
3+
const host = "localhost";
44

55
function requestListener(req, res) {
6-
var body = "";
7-
req.on('data', function (chunk) {
8-
body += chunk;
9-
});
10-
req.on('end', function () {
11-
const payload = JSON.parse(body);
12-
res.writeHead(200);
13-
res.end(payload.value.toString());
14-
});
15-
};
6+
var body = "";
7+
req.on("data", function (chunk) {
8+
body += chunk;
9+
});
10+
req.on("end", function () {
11+
const payload = JSON.parse(body);
12+
res.writeHead(200);
13+
res.end(payload.value.toString());
14+
});
15+
}
1616

1717
function send(api, value) {
18-
return new Promise(function (onFulfilled, onRejected) {
19-
const req = http.request(api, {
20-
method: 'POST',
21-
headers: {
22-
'content-type': 'application/json',
23-
},
24-
}, res => {
25-
var content = "";
26-
res.on('data', (chunk) => {
27-
content += chunk;
28-
});
29-
res.on('end', () => {
30-
onFulfilled(+content);
31-
});
32-
});
33-
req.on('error', (err) => {
34-
onRejected(err);
35-
});
36-
req.write(JSON.stringify({ "value": value }));
37-
req.end();
38-
});
18+
return new Promise(function (onFulfilled, onRejected) {
19+
const req = http.request(
20+
api,
21+
{
22+
method: "POST",
23+
headers: {
24+
"content-type": "application/json",
25+
},
26+
},
27+
(res) => {
28+
var content = "";
29+
res.on("data", (chunk) => {
30+
content += chunk;
31+
});
32+
res.on("end", () => {
33+
onFulfilled(+content);
34+
});
35+
},
36+
);
37+
req.on("error", (err) => {
38+
onRejected(err);
39+
});
40+
req.write(JSON.stringify({ value: value }));
41+
req.end();
42+
});
3943
}
4044

4145
async function sendAsync(api, value) {
42-
while (true) {
43-
try {
44-
return await send(api, value);
45-
} catch (e) { }
46-
}
46+
while (true) {
47+
try {
48+
return await send(api, value);
49+
} catch (e) {}
50+
}
4751
}
4852

4953
async function calculateSum(port, n) {
50-
var api = `http://${host}:${port}/`;
51-
var sum = +0;
52-
var tasks = [];
53-
for (var i = 1; i <= n; i++) {
54-
tasks.push(sendAsync(api, i));
55-
}
56-
for (var i = 0; i < n; i++) {
57-
sum += await tasks[i];
58-
}
59-
console.log(sum);
54+
const api = `http://${host}:${port}/`;
55+
var sum = +0;
56+
var tasks = [];
57+
for (var i = 1; i <= n; i++) {
58+
tasks.push(sendAsync(api, i));
59+
}
60+
for (var i = 0; i < n; i++) {
61+
sum += await tasks[i];
62+
}
63+
console.log(sum);
6064
}
6165

6266
function main() {
63-
const args = process.argv.slice(2)
64-
const n = +args[0] || 10;
65-
const port = 30000 + Math.floor(Math.random() * 10000);
66-
const server = http.createServer(requestListener);
67-
server.listen(port, host, async () => {
68-
await calculateSum(port, n);
69-
server.close();
70-
});
67+
const args = process.argv.slice(2);
68+
const n = +args[0] || 10;
69+
const port = 20000 + Math.floor(Math.random() * 30000);
70+
const server = http.createServer(requestListener);
71+
server.listen(port, host, async () => {
72+
await calculateSum(port, n);
73+
server.close();
74+
});
7175
}
7276

7377
main();

bench/bench_javascript_bun.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ problems:
2929
- 1.js
3030
# - name: http-server
3131
# source:
32-
# - 1.js
32+
# - 1.bun.js
3333
- name: lru
3434
source:
3535
- 1.js

0 commit comments

Comments
 (0)