| 1 | //go:build perf |
| 2 | |
| 3 | package equipotest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "log/slog" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo" |
| 19 | "amadan.net/rastrillo/rastrillo/db" |
| 20 | "amadan.net/rastrillo/rastrillo/mail" |
| 21 | "amadan.net/rastrillo/rastrillo/perf/perftest" |
| 22 | |
| 23 | equipo "amadan.net/oficina/equipo/internal/equipo" |
| 24 | ) |
| 25 | |
| 26 | // The perf lane: make perf, serial and uncached, failing only on CI. |
| 27 | // Every GET screen the router serves is held to 150ms to first byte; a |
| 28 | // new route fails here until it is measured or has a record in |
| 29 | // .rastrillo/budgets.txt. Never t.Parallel() in this file: a perf test |
| 30 | // sharing the machine measures its neighbours. |
| 31 | func TestPerfScreens(t *testing.T) { |
| 32 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 33 | f := runnerFixtureNew(t, false) |
| 34 | d := f.d |
| 35 | mergeTask, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "Performance merge", "perf-merge") |
| 36 | executionMust(t, err) |
| 37 | mergeTask, err = f.s.PrepareBug(connectionContext, f.principal, mergeTask.ID, mergeTask.Revision, bugPrepared(f.connectionFixture)) |
| 38 | executionMust(t, err) |
| 39 | mergeTask, err = f.s.StartBug(connectionContext, f.principal, mergeTask.ID, mergeTask.Revision) |
| 40 | executionMust(t, err) |
| 41 | renderID, renderHash := perfRenderedChange(t, f) |
| 42 | f.claim, err = f.x.Claim(connectionContext, f.principal, equipo.ClaimRequest{Version: 2, AssignmentID: f.a.ID, Revision: f.a.Revision, RequestID: "perf-claim"}) |
| 43 | executionMust(t, err) |
| 44 | f.x.SetRunnerValidators(equipo.RunnerValidators{Question: func(context.Context, equipo.Assignment, equipo.RunnerRequest) error { return nil }}) |
| 45 | q := runnerproto.Question{ID: "perf-question", TaskID: f.a.TaskID, ServerID: "server", AgentID: "agent", SessionID: "session", PermissionID: "permission", Kind: "question", Revision: 1, RawRequest: json.RawMessage(`{"id":"permission","kind":"question","provider":"claude","name":"AskUserQuestion","input":{"questions":[{"question":"Which?","header":"Which?"}]}}`)} |
| 46 | request := f.request() |
| 47 | request.Question = &q |
| 48 | _, err = f.x.Mutate(connectionContext, f.principal, "questions", request) |
| 49 | executionMust(t, err) |
| 50 | mustSQL(t, d, "UPDATE jobs SET state='succeeded' WHERE id=?", mergeTask.JobID) |
| 51 | mustSQL(t, d, "UPDATE tasks SET merge_owner=? WHERE id=?", ownerAddress, mergeTask.ID) |
| 52 | mustSQL(t, d, "UPDATE decision_owners SET address=? WHERE project_id=? AND decision='merge'", ownerAddress, f.project.ID) |
| 53 | mailer := &capturingMailer{} |
| 54 | cfg := testConfig(testOrigin) |
| 55 | // The GitHub section is measured with its flag on, as an owner sees it. |
| 56 | cfg.GitHubEnabled = true |
| 57 | r, err := equipo.RouterWithStore(d, cfg, mailer, logger, f.s) |
| 58 | executionMust(t, err) |
| 59 | defer equipo.CloseLive(r) |
| 60 | opts := rastrillo.Options{Logger: logger} |
| 61 | equipo.Configure(&opts, equipo.Mux(r), time.Now()) |
| 62 | defer opts.Background.Stop() |
| 63 | h, closeAll, err := rastrillo.Handler(opts) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | defer closeAll() |
| 68 | project := f.project |
| 69 | projectPath := fmt.Sprintf("/projects/%d", project.ID) |
| 70 | paths := map[string]string{"/projects/{id}": projectPath, "/projects/{id}/edit": projectPath + "/edit", "/specialists/{key}": "/specialists/engineering", "/team/{specialist}": "/team/review", "/tasks/{id}": "/tasks/" + f.a.TaskID, "/tasks/{id}/questions/{question}": "/tasks/" + f.a.TaskID + "/questions/perf-question", "/tasks/{id}/merge": "/tasks/" + mergeTask.ID + "/merge"} |
| 71 | planChecks := map[string]string{} |
| 72 | inviteURL := perfPlan3Paths(t, f, paths, planChecks, renderID, renderHash) |
| 73 | perftest.Screens(t, perftest.ScreenConfig{ |
| 74 | Routes: r, Handler: h, Paths: paths, |
| 75 | Signin: func(c *http.Client, base string) { |
| 76 | signinEmail(t, c, base, testOrigin, mailer, ownerAddress) |
| 77 | res, err := c.Get(base + inviteURL) |
| 78 | executionMust(t, err) |
| 79 | res.Body.Close() |
| 80 | if res.StatusCode != http.StatusSeeOther { |
| 81 | t.Fatalf("invite bootstrap status %d", res.StatusCode) |
| 82 | } |
| 83 | checks := map[string]string{ |
| 84 | "/work": `rst-shell-sidebar="index"`, |
| 85 | "/needs-you": `id="activity"`, |
| 86 | "/journey": `equipo-steps`, |
| 87 | "/projects": `work.projects_empty`, |
| 88 | "/projects/accounts": `data-project-count="1"`, |
| 89 | "/tasks": `work.tasks_empty`, |
| 90 | "/projects/new": `action="/projects"`, |
| 91 | projectPath: "Shop", |
| 92 | projectPath + "/edit": `action="` + projectPath + `"`, |
| 93 | "/connections": `/connections"`, |
| 94 | "/people": `action="/team/members"`, |
| 95 | "/settings": `action="/team/budgets"`, |
| 96 | } |
| 97 | for _, key := range []string{"product", "design", "copy", "engineering", "review"} { |
| 98 | checks["/specialists/"+key] = `action="/specialists/` + key + `"` |
| 99 | } |
| 100 | for path, want := range planChecks { |
| 101 | checks[path] = want |
| 102 | } |
| 103 | for path, want := range checks { |
| 104 | body := pageBody(t, c, base, path) |
| 105 | if !strings.Contains(body, want) { |
| 106 | t.Errorf("%s missing page content %q", path, want) |
| 107 | } |
| 108 | } |
| 109 | }, |
| 110 | Expect: map[string]int{"/": 303, "/signin": 303, "/auth/callback": 303, "/auth/verify": 303, |
| 111 | // Settings in sections: the old URLs lead to their sections. |
| 112 | "/conversation/connections": 303, "/conversation/notebook": 303, "/settings/thinking": 303, |
| 113 | // GitHub's two callbacks without a setup in flight end on a clean URL. |
| 114 | "/github/connect/installation": 303, "/github/connect/manifest": 303}, |
| 115 | }) |
| 116 | } |
| 117 | |
| 118 | // A cold start, from exec to the first byte of "/", held to 500ms. |
| 119 | func TestPerfBoot(t *testing.T) { |
| 120 | perftest.Boot(t, perftest.BootConfig{ |
| 121 | Prepare: schema.Path, |
| 122 | Build: func(path string) (http.Handler, func(), error) { |
| 123 | started := time.Now() |
| 124 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 125 | d, err := db.Open(path, logger) |
| 126 | if err != nil { |
| 127 | return nil, nil, err |
| 128 | } |
| 129 | mux, err := equipo.App(d, testConfig(testOrigin), mail.Logged(logger), logger) |
| 130 | if err != nil { |
| 131 | d.Close() |
| 132 | return nil, nil, err |
| 133 | } |
| 134 | opts := rastrillo.Options{Logger: logger} |
| 135 | equipo.Configure(&opts, mux, started) |
| 136 | h, closeAll, err := rastrillo.Handler(opts) |
| 137 | if err != nil { |
| 138 | opts.Background.Stop() |
| 139 | d.Close() |
| 140 | return nil, nil, err |
| 141 | } |
| 142 | return h, func() { closeAll(); d.Close() }, nil |
| 143 | }, |
| 144 | Path: "/", Expect: http.StatusSeeOther, |
| 145 | }) |
| 146 | } |
| 147 | |