| 1 | //go:build perf |
| 2 | |
| 3 | package todotest |
| 4 | |
| 5 | import ( |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/rastrillo/rastrillo" |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | "amadan.net/rastrillo/rastrillo/perf/perftest" |
| 15 | |
| 16 | todo "todo/internal/todo" |
| 17 | ) |
| 18 | |
| 19 | // The perf lane: make perf, serial and uncached, failing only on CI. |
| 20 | // Every GET screen the router serves is held to 150ms to first byte; a |
| 21 | // new route fails here until it is measured or has a record in |
| 22 | // .rastrillo/budgets.txt. Never t.Parallel() in this file: a perf test |
| 23 | // sharing the machine measures its neighbours. |
| 24 | func TestPerfScreens(t *testing.T) { |
| 25 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 26 | d, err := db.Open(schema.Path(t), logger) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | defer d.Close() |
| 31 | r, err := todo.Router(d, testOrigin, logger) |
| 32 | if err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | opts := rastrillo.Options{Logger: logger} |
| 36 | todo.Configure(&opts, todo.Mux(r), time.Now()) |
| 37 | h, closeAll, err := rastrillo.Handler(opts) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | defer closeAll() |
| 42 | // Screens behind sign-in need Signin; a {param} route needs a Paths |
| 43 | // entry whose row this test seeds first. |
| 44 | perftest.Screens(t, perftest.ScreenConfig{Routes: r, Handler: h}) |
| 45 | } |
| 46 | |
| 47 | // A cold start, from exec to the first byte of "/", held to 500ms. |
| 48 | func TestPerfBoot(t *testing.T) { |
| 49 | perftest.Boot(t, perftest.BootConfig{ |
| 50 | Prepare: schema.Path, |
| 51 | Build: func(path string) (http.Handler, func(), error) { |
| 52 | started := time.Now() |
| 53 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 54 | d, err := db.Open(path, logger) |
| 55 | if err != nil { |
| 56 | return nil, nil, err |
| 57 | } |
| 58 | mux, err := todo.App(d, testOrigin, logger) |
| 59 | if err != nil { |
| 60 | d.Close() |
| 61 | return nil, nil, err |
| 62 | } |
| 63 | opts := rastrillo.Options{Logger: logger} |
| 64 | todo.Configure(&opts, mux, started) |
| 65 | h, closeAll, err := rastrillo.Handler(opts) |
| 66 | if err != nil { |
| 67 | d.Close() |
| 68 | return nil, nil, err |
| 69 | } |
| 70 | return h, func() { closeAll(); d.Close() }, nil |
| 71 | }, |
| 72 | Path: "/", |
| 73 | }) |
| 74 | } |
| 75 | |