Open4

denoweb

lfzlfz

インストール

環境
wsl2

dockerはポート合わせるのが面倒なので使わない
※書いとかないと忘れてうっかりdockerで試しそうなので

インストール

asdf plugin-add deno
asdf list all deno
asdf install deno 1.15.3
asdf global deno 1.15.3

main.ts

async function handle(conn: Deno.Conn) {
    const httpConn = Deno.serveHttp(conn);
    for await (const requestEvent of httpConn) {
        const url = new URL(requestEvent.request.url);
        console.log(`path: ${url.pathname}`);
        await requestEvent.respondWith(
            new Response("hello world", {
            status: 200,
            }),
        );
    }
}
 
const server = Deno.listen({ port:11111});
 
for await (const conn of server) {
    console.log('listen');
    handle(conn);
}

実行

deno run --allow-net main.ts

lfzlfz
const server = Deno.listen({ port: 11111 });
 
let cnt=0;
 
while (true) {
  try {
    const conn = await server.accept();
    (async () => {
      const httpConn = Deno.serveHttp(conn);
      while (true) {
        try {
          const requestEvent = await httpConn.nextRequest();
          if(requestEvent === null){
            break;
          }
          const url = new URL(requestEvent.request.url);
          console.log(`path: ${url.pathname} : ${cnt++}`);
          await requestEvent.respondWith(
            new Response("hello world", {
            status: 200,
            }),
          );
 
        } catch (err) {
          // the connection has finished
          console.log("the connection has finished");
          break;
        }
      }
    })();
  } catch (err) {
    // The listener has closed
    console.log("The listener has closed")
    break;
  }
}

ab -n 40000 -c 4000 http://localhost:11111/
29000で死ぬ

lfzlfz

速度比較

ab -n 40000 -c 4000

` deno go rust rust --release
real 17.90s 5.62s 36000requestで死ぬ 13.41
user 1.10s 0.52s なぜ 0.76s
sys 6.23s 3.82s なぜ 4.37s
  • rustは飛び飛びで1ヶ月ぐらいはやってるけど、まだよく分からない
  • goは公式見ながら数時間試しただけ
  • denoはjavascriptなので慣れたもん

※36000にしたら32400で死んだ
※32000にしたら28800で死んだ
なんで最後との一塊で死ぬのか謎

denoは素のDeno.listen
goはgin
rustはactix-web

ソースを貼り付けるのは面倒なので省略

  • deno
    は上に書いてあるwhileのコード
  • go
    公式にあるginのalbumsへのアクセス
  • rust
    自分のgithub参照(actix-webのテキトーなルーティング群のうちトップのみ)
    mutableな共有メモリ使ったりとか色々テストしてて無駄な処理突っ込んであるからか分からないが、糞遅い

dockerいらないと思ったけどそれだとrustのテストが面倒かったのでクロスコンパイル考えるならdocker載せた方が良い

crossはdocker入れないと動かなかった

windows上のdockerは不具合起こしやすいので軽く動作確認する時以外使いたくない

wsl2上でdocker使うなら最初からwsl2直でいいじゃん