← tests

middleware

Per-layer dispatch cost at zero, four and sixteen no-op layers.

Comparable across every framework. Every layer calls the next and does nothing else, so the slope between the three is the framework's per-layer cost and nothing else.

testheftrequestbasewhat differs from the base
middleware.none1GET /middleware/nonejson.smalla different route carrying the same handler, which should cost nothing
middleware.four1GET /middleware/fourmiddleware.nonemore no-op middleware layers in front of the handler
middleware.sixteen1GET /middleware/sixteenmiddleware.nonemore no-op middleware layers in front of the handler

middleware.none

heft 1

base json.smallbase of middleware.four, middleware.sixteen

No layers in front of a handler that serialises the small payload. A different route carrying the same handler, which should cost nothing. It is the zero point the other two are read against, and a framework where this differs from json.small is paying for the route rather than the layers.

Request

GET /middleware/none

No body.

Expected response

HTTP 200

payload items.small

{"size":"small","count":1,"items":[{"id":1,"name":"slate-lamp-6647","category":"tools","priceCents":18928,"inStock":true}]}

Compared as parsed JSON, so key order and how a number is written do not matter.

Test source, tests/middleware/none.ts
tests/middleware/none.tsopen on GitHub →
import { performanceTest } from "#kit";
import { items } from "#payloads";

const path = "/middleware/none";

export default performanceTest({
  id: { family: "middleware", name: "none" },
  path,
  base: "json.small",
  varies: "route",
  heft: 1,
  about:
    "No layers in front of a handler that serialises the small payload. A " +
    "different route carrying the same handler, which should cost nothing. It " +
    "is the zero point the other two are read against, and a framework where " +
    "this differs from json.small is paying for the route rather than the " +
    "layers.",

  request: (c) => c.get(path).okWith(items.small),
});

middleware.four

heft 1

base middleware.none

Four no-op layers in front of a handler that serialises the small payload. Four layers in front of the handler, each calling the next and doing nothing else. Read against middleware.none, the difference divided by four is the framework's per-layer cost.

Request

GET /middleware/four

No body.

Expected response

HTTP 200

payload items.small

{"size":"small","count":1,"items":[{"id":1,"name":"slate-lamp-6647","category":"tools","priceCents":18928,"inStock":true}]}

Compared as parsed JSON, so key order and how a number is written do not matter.

Test source, tests/middleware/four.ts
tests/middleware/four.tsopen on GitHub →
import { performanceTest } from "#kit";
import { items } from "#payloads";

const path = "/middleware/four";

export default performanceTest({
  id: { family: "middleware", name: "four" },
  path,
  base: "middleware.none",
  varies: "layers",
  heft: 1,
  about:
    "Four no-op layers in front of a handler that serialises the small " +
    "payload. Four layers in front of the handler, each calling the next and " +
    "doing nothing else. Read against middleware.none, the difference divided " +
    "by four is the framework's per-layer cost.",

  request: (c) => c.get(path).okWith(items.small),
});

middleware.sixteen

heft 1

base middleware.none

Sixteen no-op layers in front of a handler that serialises the small payload. Sixteen layers, which is where a per-layer cost that looked like noise at four becomes readable. Read against middleware.none, and against middleware.four to see whether the cost is linear.

Request

GET /middleware/sixteen

No body.

Expected response

HTTP 200

payload items.small

{"size":"small","count":1,"items":[{"id":1,"name":"slate-lamp-6647","category":"tools","priceCents":18928,"inStock":true}]}

Compared as parsed JSON, so key order and how a number is written do not matter.

Test source, tests/middleware/sixteen.ts
tests/middleware/sixteen.tsopen on GitHub →
import { performanceTest } from "#kit";
import { items } from "#payloads";

const path = "/middleware/sixteen";

export default performanceTest({
  id: { family: "middleware", name: "sixteen" },
  path,
  base: "middleware.none",
  varies: "layers",
  heft: 1,
  about:
    "Sixteen no-op layers in front of a handler that serialises the small " +
    "payload. Sixteen layers, which is where a per-layer cost that looked " +
    "like noise at four becomes readable. Read against middleware.none, and " +
    "against middleware.four to see whether the cost is linear.",

  request: (c) => c.get(path).okWith(items.small),
});