Skip to content

Instantly share code, notes, and snippets.

@scorredoira
Last active March 7, 2021 23:13
Show Gist options
  • Save scorredoira/3540888b71ecd239a23a1e85e8062c34 to your computer and use it in GitHub Desktop.
Save scorredoira/3540888b71ecd239a23a1e85e8062c34 to your computer and use it in GitHub Desktop.
Hello world in Dune, Go and C

HTTP Hello world in Dune, Go and C

Dune

let server = http.newServer()
server.address = ":8081"
server.handler = (w, r) => w.write("Hello, World!")
server.start()

Go

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, world!")
	})

	http.ListenAndServe(":8082", nil)
}

c

Using lwan webserver

#include "lwan.h"

LWAN_HANDLER(hello_world)
{
    static const char message[] = "Hello, World!";

    response->mime_type = "text/plain";
    lwan_strbuf_set_static(response->buffer, message, sizeof(message) - 1);

    return HTTP_OK;
}

int
main(void)
{
    const struct lwan_url_map default_map[] = {
        { .prefix = "/", .handler = LWAN_HANDLER_REF(hello_world) },
        { .prefix = NULL }
    };
    struct lwan l;

    lwan_init(&l);

    lwan_set_url_map(&l, default_map);
    lwan_main_loop(&l);

    lwan_shutdown(&l);

    return 0;
}

Results

# dune
$ wrk -d2 http://localhost:8082
Running 2s test @ http://localhost:8082
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    42.70us  109.53us   8.64ms   98.35%
    Req/Sec   120.04k     4.71k  144.42k    85.37%
  489385 requests in 2.10s, 60.67MB read
Requests/sec: 233108.97
Transfer/sec:     28.90MB

# go
$ wrk -d2 http://localhost:8082
Running 2s test @ http://localhost:8082
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    39.74us   48.04us   2.54ms   96.61%
    Req/Sec   123.54k     4.68k  147.08k    87.80%
  503408 requests in 2.10s, 62.41MB read
Requests/sec: 239800.20
Transfer/sec:     29.73MB

# c
$ wrk -d2 http://localhost:8081
Running 2s test @ http://localhost:8081
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    47.89us   67.63us   2.69ms   94.83%
    Req/Sec   112.09k     3.09k  118.21k    78.57%
  467756 requests in 2.10s, 57.99MB read
Requests/sec: 222742.86
Transfer/sec:     27.62MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment