Back to trackLesson 18 of 25

Latency 3: Fair Comparisons

Requires Pro (declare and Import; per-node random streams)

Problem statement

Lesson 4.2 compared the new deploy with the current service by running both in one model, and found the comparison noisy. The p99 of the new deploy minus the p99 of the baseline is 56.8 ms in truth, but a run of 100,000 requests measures it with a standard deviation of 3.05 ms, and for a small change (a median of 31 ms against 30) the noise was 40% of the thing being measured. The cause was that the two services were tested on different random requests, so part of every difference was luck about which requests each one got.

The questions in this lesson:

  • Can both versions see the same random requests, so that the luck cancels?
  • How much does that sharpen a comparison, in rows saved?
  • When does a same-seed comparison quietly stop being fair?

The mechanism is the one from the Determinism page: every node that draws random numbers gets its own stream, derived from the seed and the node’s name. The lesson uses that on purpose, and then shows how it can be lost without any error message.

The model

The two versions are now two small models, one file each, and both import the library from lesson 4.1 under the same name. samples/pro/latency-crn-baseline.xml (seed 404):

<dataset columns="latency" seed="404">
  <root node="Sys"/>
  <nodes>
    <Import name="Sys" file="latency-lib.xml" sigma="0.8"/>
  </nodes>
</dataset>

and samples/pro/latency-crn-newdeploy.xml (seed 404):

<dataset columns="latency" seed="404">
  <root node="Sys"/>
  <nodes>
    <Import name="Sys" file="latency-lib.xml" median="33" sigma="0.87"/>
  </nodes>
</dataset>

The files differ only in their overrides. Each has one node of its own (the import) and brings the library’s five, so the validator counts 5 nodes for each. Each file has a single column, the latency of one request in milliseconds. The library is the same file that lessons 4.1 and 4.2 use.

Inside the library, the node that draws the random numbers is the lognormal Spread, and the latency is the median times Spread (which is exp(sigma x Z) for a standard normal Z). Imported under the name Sys, that node’s full name is Sys.Spread in both files. Its stream comes from the seed and that name, and nothing else. Both files therefore draw the same sequence of Z, and only the median and sigma that scale it differ.

grind --count 5 latency-crn-baseline.xml
grind --count 5 latency-crn-newdeploy.xml

The first five rows with the models’ own seed, 404. The last column is not in the output; it is recovered from each file’s latency as Z = ln(latency / median) / sigma:

RowBaseline (ms)NewDeploy (ms)Z from BaselineZ from NewDeploy
145.23751.5820.51340.5134
238.95643.8420.32650.3265
324.17126.091-0.2700-0.2700
4116.276144.0011.69351.6935
532.94836.5420.11720.1172

Compare this with the table of lesson 4.2, where the two columns of one model showed a slow request for one service next to a quick one for the other. Here row 4 is the slow request for both versions (116 ms and 144 ms), and the new deploy’s version of each request is the baseline’s, stretched. That is common random numbers: both models are run on the same underlying randomness, so every difference between their outputs is caused by the change and not by chance.

How exactly the same? Over 100,000 rows at seed 404 the largest difference between the two files’ Z, over all rows, is 8.9e-16, which is rounding in the last digit of a double (over the first 2,000 rows it is the same 8.9e-16). The correlation of the two Z columns is 1.0000. The two files are not similar, they are the same draws.

The same import under a different name

The sharing comes from the name, not from the file. For example, change one thing in the new deploy’s file: naming the import Other instead of Sys, so its spread node is Other.Spread. With the same seed, 404, and 100,000 rows, the correlation between its Z and the baseline’s Z is -0.0014 (standard error about 0.003): zero. The two models are now independent, with no error and no warning. The two-column model of lesson 4.2 is the same thing in one file: its imports are named Baseline and NewDeploy, and the correlation of its two columns’ Z is 0.0045 over 100,000 rows. So renaming an import is a modeling decision. It decides whether two runs are tied together or not.

What a change to the model does to the other nodes

Common random numbers needs the shared stream to stay the same when the model changes. With per-node streams (the default in 1.0) a change to one node does not touch the others; <dataset streams="shared"> is the layout of the Beta releases, in which all nodes draw from one stream in the order the values are pulled. Test both in a throwaway file next to the library: the import alone (seed 405), and the same import with an unrelated Normal node added next to it (an Append puts the two side by side, so column 0 is still the Sys latency). Column 0 for the first five rows, without and with the extra node:

Rowper-node: withoutper-node: withshared: withoutshared: with
138.76980038.76980022.44817922.448179
224.21434124.21434144.21277421.275580
325.00650725.00650721.27558034.297861
444.11084344.11084343.54905129.188085
523.00198323.00198334.29786122.128739

Among the first 1,000 rows the Sys value is identical with and without the extra node in 1,000 of 1,000 rows with per-node streams, and in 1 of 1,000 with streams="shared". The shared column shows why. With one stream, Sys took draws 1, 3 and 5 of it before the new node existed, and the new node pulls every second draw: the “with” column’s rows 1 to 3 (22.448, 21.276, 34.298) are the “without” column’s rows 1, 3 and 5. Every later row is shifted. The distributions are untouched, the individual values are not.

The same test in Module 1 terms: the release schedule of lesson 1.3 (samples/simulation/release-schedule.xml, seed 20261006) with one extra task, a Triangular named Review, appended as a seventh column. Among the first 1,000 rows, with the default streams 0 rows change in the six original columns (Design, Build, Test, Docs, Finish and Late). With streams="shared", 999 of the 1,000 rows change (Design, Build, Test, Docs and Finish in 999 rows each; Late, a 0-or-1 flag, in 255). Only the first row is the same in both.

Per-node streams are not a guarantee against every change, though. The Determinism page says what they do not give you: a node’s own later values still move when its own draw count changes, and a stream follows the node’s name. Try adding the Review task a second way, as a fifth dimension of the Copula (a 5 x 5 identity matrix) instead of next to it. A five-dimensional copula draws five numbers per row where the four-dimensional one drew four, so with the default streams 999 of the 1,000 rows change in the original columns. So “the model changed” means a change in anything the shared node draws, not only in its parameters.

How much sharper is the comparison?

Now the measurement. The quantity is the same as in lesson 4.2, the p99 of the new deploy minus the p99 of the baseline, read from the sorted rows of one run. For each seed S from 1 to 50 run grind --count 100000 --seed S on the two common-stream files, and on the two-column model of 4.2 (latency-scenarios.xml, the independent streams), and subtract the baseline’s p99 from the new deploy’s:

grind --count 100000 --seed 1 latency-crn-baseline.xml
grind --count 100000 --seed 1 latency-crn-newdeploy.xml

The 50 differences of the two p99s for the main change on one scale, one dot per seed. With independent streams they spread from 48 to 62 ms with a standard deviation of 3.05 ms. With common random numbers they sit between 55 and 58.5 ms, standard deviation 0.78 ms. The exact difference, 56.82 ms, is marked

50 seeds of 100,000 rowsIndependent: mean (sd)Common: mean (sd)ExactRatio of sdRows for the same precision
Main change (33 ms, 0.87)56.78 (3.05)56.79 (0.78)56.823.915 times fewer
Small change (31 ms, 0.8)6.44 (2.56)6.43 (0.064)6.4340about 1,600 times fewer

Both ways are on the right value; common random numbers is not biased, it is quieter. The standard deviation of an estimate falls with the square root of the rows, so a standard deviation smaller by a factor r is worth r squared times the rows: 3.89 squared is 15.1 for the main change, and 39.96 squared is 1,597 for the small one. To see what that means in rows: for a 95% interval of plus or minus 1 ms on the difference, the theory below puts the independent comparison at about 3.8 million rows and the common one at about 212,000 for the main change, and about 2.6 million against 1,400 for the small one.

Those ratios come from 50 seeds, and a standard deviation from 50 values is itself uncertain by about 10% (so a variance ratio by about 20%). Running 200 more seeds (1001 to 1200) at 100,000 rows firms them up. Over all 250 seeds the main change has standard deviations of 3.21 and 0.72 (ratio 4.5, variance ratio 19.8) and the small change 2.66 and 0.059 (ratio 45, variance ratio 2,031). So a good summary is “about 15 to 20 times fewer rows” for the main change and “about 1,600 to 2,000 times” for the small one; the theory below gives 17.9 and 1,861.

The same two strips for the small change, median 31 and sigma 0.8, on a scale of -6 to 18 ms. Independent streams spread the 50 differences from about -1 to 11 ms; common random numbers put all 50 between 6.29 and 6.57 ms, a single column of dots at the exact 6.43

For the small change the noise nearly disappears. Independent streams measure a 6.4 ms difference with a standard deviation of 2.56, and one run in 50 has the wrong sign; common random numbers put every one of the 50 differences between 6.29 and 6.57. At 10,000 rows per run the contrast is sharper still: independent streams have the wrong sign in 14 of 50 runs (standard deviation 8.40), common random numbers in none (standard deviation 0.21). At 10,000 rows the main change has standard deviations of 10.03 and 2.57.

Why the gain is so large (and when it would not be)

A sample p99 is, in effect, one order statistic of the 100,000 draws, and both versions compute theirs from the same draws. Each latency is median x exp(sigma x Z), an increasing function of Z, so both p99s are the same row’s Z passed through a different curve. The two p99s are therefore not just correlated across seeds; they are almost the same random quantity. In the common runs of the main change the correlation between the two p99s across the 250 seeds is 1.0000, and the new deploy’s p99 alone is no better than before: its standard deviation across seeds is still 2.49 ms (the baseline’s 1.77), as in lesson 4.2 (2.40 and 1.79 over 50 seeds). Only the difference gains, because the noise they share cancels in it.

A short derivation gives the size. Call z-hat the sample 99th percentile of the standard normal draws in one run. Its standard error is sqrt(0.99 x 0.01 / n) / 0.02665, which is 0.0118 for n = 100,000 (0.02665 is the standard normal density at 2.3263). The difference is 33 exp(0.87 z-hat) - 30 exp(0.8 z-hat), and its slope in z-hat at 2.3263 is 33 x 0.87 x 7.569 - 30 x 0.8 x 6.431 = 63.0 ms per unit. The standard deviation of the difference is 63.0 x 0.0118 = 0.743 ms, against 0.78 measured at 50 seeds and 0.72 at 250. For the small change the slope is 31 x 0.8 x 6.431 - 154.3 = 5.2, so 0.061 ms, against 0.064 and 0.059. The independent case combines two separate noisy p99s (1.82 and 2.57 ms, each from the formula of lesson 4.2): 3.15 ms for the main change and 2.62 for the small one, against 3.05 and 2.56 measured. In the small change the two scenarios differ only by a 3% scaling of the median, so their noisy parts are nearly identical and almost entirely cancel, which is why that comparison gains 1,600 times and the main change only 15.

Two cautions to bear in mind. First, the gain is not a trick of the p99: for the difference of the mean latency, the 50-seed standard deviations are 0.221 independent against 0.043 common for the main change (a variance ratio of 26) and 0.198 against 0.0043 for the small change (about 2,100). Second, the gain is as large as this because the two scenarios change only parameters of one shared random draw, and both statistics are monotone in it. It would be smaller if the scenarios changed structure, so that the shared draws line up only partly: a branch taken in one version and not in the other, a node added in the path, or a changed Mix or Switch that sends different rows to different branches. The test above with the fifth dimension in the Copula is the extreme: nothing is shared any more, and the comparison is back to independent. Common random numbers also do not help with the question of lesson 4.2 (is the new deploy’s p99 under 250?), which compares one estimate with a fixed line: the new deploy’s p99 on its own has the same noise as before.

Two scatter plots of NewDeploy latency against Baseline latency for the same first 400 rows on log scales. With common random numbers every point lies on a single straight line (a power law, because the same Z is scaled twice). With independent streams the points are a round cloud with a correlation of 0.06 on the logs

The scatter shows what “the same draws, scaled” means. With common random numbers the 400 points lie on one line on the log-log plot, the power law 33 x (x / 30)^(0.87 / 0.8) that takes a baseline latency to the new deploy’s for the same Z, to within 8.9e-16 in the log. With two import names the same plot is a round cloud (a correlation of the two logs of 0.062).

Rules for a fair same-seed comparison

  • Keep the node names the same across the models being compared. The stream follows the node’s full name, including the import’s name or prefix. Give both versions the same import name, as Sys above, and do not rename a node in one of them.
  • Keep the structure the same. Changes to parameters that only scale a draw are safe. Changes to how many values a node draws, or which rows pick which branch (the weights of a Mix, the key of a Switch, the bounds of a Truncate, a dimension added to a Copula), move that node’s own draws even with per-node streams. With streams="shared", adding, removing or reordering any node moves nearly everything.
  • Check by comparing the underlying draws, as this lesson does: recover Z from the two outputs (latency over median, then ln, then over sigma) and look at the largest difference. About 1e-15 means the streams are shared; anything else means they are not. A comparison that looks fair and is not still runs, and it still gives an answer, only a noisier one than you think.

Check: do the numbers match the math?

The two models’ underlying normals agree to 8.9e-16 over 100,000 rows (seed 404), and their correlation is 1.0000, as the Determinism page says they should: the same seed and the same node name give the same stream. With a different import name the correlation is -0.0014 over 100,000 rows (standard error 0.003). The means of the differences, 56.79 and 6.43, are within a standard error of the exact 56.82 and 6.43 (the standard errors of the mean over 50 seeds are 0.11 and 0.009), so common random numbers do not bias the difference. The spread over seeds, 0.78 and 0.064 at 50 seeds (0.72 and 0.059 at 250), is within 6% of the delta-method values 0.743 and 0.061 at 50 seeds, and within 3% of them at 250 (a standard deviation from 50 values is uncertain by 10%, one from 250 by 4%). The independent spreads, 3.05 and 2.56, are 97% and 98% of the theory from lesson 4.2 (3.15 and 2.62). The stream tests are exact counts, not estimates: 1,000 of 1,000 rows unchanged with per-node streams and 1 of 1,000 with shared ones, and 0 and 999 of 1,000 rows changed in the release schedule.

What this gives you: a fair way to compare two versions of a model: give both the same random numbers by giving the imported nodes the same names, and keep the structure the same. For the p99 of the new deploy against the baseline that took the standard deviation of the difference from 3.05 ms to 0.78 ms (15 to 20 times fewer rows for the same precision), and for a small change from 2.56 ms to 0.064 ms (1,600 to 2,000 times). You have also seen the ways that advantage is lost without a message: a different import name, streams="shared" when any node is added, or a change in what a node draws. The checks cost one line each: recover the underlying draws and compare. The companion page lets you pick the change (main or small), the streams (independent or common) and the rows per run (10,000 or 100,000), and shows the 50 differences, their spread and the rows needed.

So far the model has been calm: every request is drawn from the same distribution. The other question the SRE team asked is whether their alerting notices an incident, and for that the model needs something to go wrong. The next lesson injects incidents into the traffic.