Osy#betaRun it on Osyrin: osyrin.com

Reference / Testing

[Growth(Small, Large)]: a test run at two sizes, each span held flat between them

[Test, Growth(Small = 10, Large = 40)] void the_test(int n) { … } · [Growth(Small = 10, Large = 40, Allowed = 2)]

Put [Growth] on a test that takes int n and osy test runs it twice, once with each size, each run cold on its own clone of the fixture. It compares every Cost.Span the two runs named: a span whose SQL statements grow from the small run to the large one — a read per row — fails the test with both numbers and the large run's heaviest lines.

preview2 examples compiled by CItestingcostperformance

Summary#

A page whose cost grows with its data looks fine at fixture size. [Growth] runs the same test at two sizes and holds each named part of it — each Cost.Span(name, mark): a named part of a test, in the report and held to a budget span — flat between them:

[Test(Slices)]
[Growth(Small = 10, Large = 40)]
[runas(Planner)]
void plan_tab_is_flat(int n) {
  SeedSlices(n);
  var mark = Cost.Mark();
  Ui.Visit("/plan");
  Cost.Span("Plan tab", mark);
}

Signature#

[Growth(Small = <n>, Large = <n>)]                  // on a [Test] taking one int; Small < Large, both 0 or more
[Growth(Small = <n>, Large = <n>, Allowed = <k>)]   // each span may grow by at most k statements (default 0)

Description#

The runner runs the test once per size. The test takes one int and uses it to make the world bigger — seed n rows, add n lines to an order. osy test calls it with Small, then with Large, and reports ONE result.

Each size is its own run, cold. Each run gets its own clone of the fixture, and its own request memo, step budget, read budget and counter, as any test does. Nothing the small run read or kept answers the large run. Running both sizes inside one test would share that test's request memo: the second run would be served what the first memoised, and would look cheaper than it is, or per-row when it is not. Whatever the platform keeps for a request, each size starts without it.

Spans are compared by name. For every span the two runs named, the statements at Large minus the statements at Small is what it grew by. More than Allowed fails the test, after both runs, naming each span that grew, both numbers and the large run's five heaviest lines:

  ✗ shelf_is_read_one_parcel_at_a_time (1240ms · 47 statements · 80 rows read · 40 written)
      ▸ read the shelf: 1 statements · 40 rows read · 0 written   (n = 10 → 40: 1 → 1 statements, +0, allowed 0)
      ▸ weigh each parcel: 40 statements · 40 rows read · 0 written   (n = 10 → 40: 10 → 40 statements, +30, allowed 0 — over by 30)
    span 'weigh each parcel' grows with n: 10 statements at 10, 40 at 40 (+30, allowed 0) — a read per row?
      where its statements came from at n = 40, most first:
        40 × tests/shelf.test.osy:9 · test.body > db.materialize · SELECT t0."Weight" … FROM "data"."Parcel" AS t0 WHERE (t0."Id" = @p0) LIMIT 2

The failure points at the line that named the first span that grew. Without --cost or --cost-report the run keeps no lines, and the message says to re-run with --cost instead.

Statements are judged; rows read are not. A page that shows more rows reads more of them, so rows read are reported at both sizes and never fail the test. Statements are what a read per row adds.

A span named at one size only fails the test. Growth compares a span with itself. A span named on a path that runs at one size and not the other is reported with both sizes, so name it on every path the test takes. A test whose runs named no span at all fails too: there was nothing to compare.

Everything else is as in any test. Ui.*, fixtures, runas and stubs behave the same at each size. A span's own budget (Cost.Span("Plan tab", mark, 255)) is held at each size. A red at the small size is the verdict: its message starts at n = 10:, and the large run is not started. A red in the large run starts at n = 40:.

What the report carries. In osy test --json and osy test --cost-report, a [Growth] test's totals, lines and spans are its large run's. Each span adds growth, keyed by size:

{ "name": "weigh each parcel", "statements": 40, "roundTrips": 40, "rowsRead": 40, "rowsWritten": 0,
  "budget": null, "over": false, "top": [ … ],
  "growth": { "n": [10, 40], "statements": { "10": 10, "40": 40 }, "rowsRead": { "10": 10, "40": 40 },
              "grows": 30, "allowed": 0, "over": true } }

Every other test's spans are as Cost.Span(name, mark): a named part of a test, in the report and held to a budget describes, with no growth.

Checked as you write it. osy validate refuses [Growth] on a function that is not a [Test], a test without exactly one int parameter, a missing or non-number Small or Large, Small not smaller than Large, a negative Allowed, and a test that names no Cost.Span. It also refuses a [Test] that takes parameters without [Growth]: nothing else passes a test anything. Whether a test names a span is read off its body and the functions of the same file it calls. A test that calls a helper from another file is accepted, because that helper may name one.

Growth or a budget? A budget (Cost.Span(name, mark): a named part of a test, in the report and held to a budget, [PageBudget]: what a page's first paint and live re-run may cost, held by osy test) holds a number at one size. Growth holds the shape: a page may cost 230 statements, but it must cost 230 with 10 rows and with 40. Use both. The budget catches a page that got heavier; growth catches one whose cost will keep growing with the data.

Examples#

entity Parcel {
  [Required, MaxLength(40)] string Label;
  int Weight;
  security { allow read, create when IsAnonymous || IsAuthenticated; }
}

void Receive(int n) {
  for (var i = 0; i < n; i++) { new Parcel { Label = $"parcel {i}", Weight = i }; }
  UnitOfWork.Commit();
}
[Test]
[Growth(Small = 10, Large = 40)]
void reading_the_shelf_is_flat(int n) {
  Receive(n);
  var mark = Cost.Mark();
  var all = Parcel.Where(p => p.Weight >= 0).ToList();
  Cost.Span("read the shelf", mark);
  Assert.Equal(n, all.Count);
}

Weighing each parcel with a read of its own is a statement per row, and fails:

[Test]
[Growth(Small = 10, Large = 40)]
void shelf_is_read_one_parcel_at_a_time(int n) {
  Receive(n);
  var shelf = Cost.Mark();
  var all = Parcel.Where(p => p.Weight >= 0).ToList();
  Cost.Span("read the shelf", shelf);
  var weighing = Cost.Mark();
  var total = 0;
  foreach (var p in all) { total = total + Parcel.Single(x => x.Id == p.Id).Weight; }
  Cost.Span("weigh each parcel", weighing);   // 10 statements at 10, 40 at 40 — the test fails here
}
$ osy test --filter shelf --cost-report out/cost.json

See also#

Related

Cost.Span(name, mark): a named part of a test, in the report and held to a budget

Name what a part of a test spent — a tab opened, a click, a commit — and it leaves the test: printed under its result…

Cost.Mark() and Cost.StatementsSince(mark)

What a test's unit of work sent to the database between two points: take a mark, do the thing, read what it cost — in…

[PageBudget]: what a page's first paint and live re-run may cost, held by osy test

Declare beside a page's [Route] the most SQL statements and rows read its first paint may cost. osy test opens the page…

Fixtures: built once, cloned for every test, and built on each other

A [TestFixture] is built once per run, and every test that names it runs on its own copy of what it left — rows, files…

Running tests

Runs your app's tests against a throwaway copy of its database, reporting each test as it finishes. Your local source…