Skip to content

Detecting temp directory performed on every request #101

@makindotcc

Description

@makindotcc

Hello.
I was benchmarking wisp and mist, I saw that wisp is significantly slower.
Found that applying following diff to make_connection and wisp_mist.handler is making wisp 20x faster on linux (wsl).

pub fn make_connection(
  body_reader: Reader,
  secret_key_base: String,
) -> Connection {
  // Fallback to current working directory when no valid tmp directory exists
-  let prefix = case directories.tmp_dir() {
-    Ok(tmp_dir) -> tmp_dir <> "/gleam-wisp/"
-    Error(_) -> "./tmp/"
-  }
-  let temporary_directory = join_path(prefix, random_slug())
+  let temporary_directory = "test"
  Connection(
    reader: body_reader,
    max_body_size: 8_000_000,
    max_files_size: 32_000_000,
    read_chunk_size: 1_000_000,
    temporary_directory: temporary_directory,
    secret_key_base: secret_key_base,
  )
}
pub fn handler(
  handler: fn(wisp.Request) -> wisp.Response,
  secret_key_base: String,
) -> fn(HttpRequest(mist.Connection)) -> HttpResponse(mist.ResponseData) {
  fn(request: HttpRequest(_)) {
    let connection =
      internal.make_connection(mist_body_reader(request), secret_key_base)
    let request = request.set_body(request, connection)
+    // todo: try to delete temporary files only if they have been allocated for this request?
-    use <- exception.defer(fn() {
-      let assert Ok(_) = wisp.delete_temporary_files(request)
-    })

    let response =
      request
      |> handler
      |> mist_response

    response
  }
}

Current wisp version:

Running 1s test @ http://127.0.0.1:8000
  6 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     3.95ms  404.93us   5.37ms   72.56%
    Req/Sec     4.05k   295.12     4.69k    73.33%
  24198 requests in 1.00s, 2.61MB read
Requests/sec:  24165.91
Transfer/sec:      2.60MB

With following path:

wrk -c 100 -d 1 -t 6 http://127.0.0.1:8000
Running 1s test @ http://127.0.0.1:8000
  6 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   274.09us  507.14us   9.34ms   93.26%
    Req/Sec    83.83k    15.16k  105.48k    63.08%
  541738 requests in 1.10s, 58.38MB read
Requests/sec: 492452.83
Transfer/sec:     53.07MB

I think it would be better to do it lazily (only when needed) since looking up temp directory (directories.tmp_dir()) to check if file exists to never use that in handler is too expensive.
I would be happy to PR laziness, but I don't know really how to make that yet, I'm a new gleamer...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions