Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion v5/client/src/chart-markers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { markerOf } from './chart-markers'
import { markerOf, positionsInView, traceMode } from './chart-markers'

describe('markerOf', () => {
it('gives each trace its own marker, starting over once all are taken', () => {
Expand All @@ -8,3 +8,54 @@ describe('markerOf', () => {
expect(markerOf(8)).toBe(markerOf(0))
})
})

describe('when points have markers', () => {
it('draws a line alone without markers, but keeps the marker of a single point', () => {
expect(traceMode(2, true)).toBe('lines+markers')
expect(traceMode(2, false)).toBe('lines')
expect(traceMode(1, false)).toBe('lines+markers')
})

it('counts the positions of the x-axis between the edges of the plot', () => {
// Position i at 100 + 10 * i pixels, in a plot from 100 to 300 pixels.
const geometry = {
area: { left: 100, top: 0, width: 200, height: 100 },
x: (value: number) => 100 + 10 * value,
y: (value: number) => value,
yAxes: new Map(),
}
expect(positionsInView(geometry, 50)).toBe(21)
// Fewer positions than room for them.
expect(positionsInView(geometry, 5)).toBe(5)
// Zoomed in between positions 4.5 and 6.5, then panned past the last position.
expect(positionsInView({ ...geometry, x: (value) => 100 + 100 * (value - 4.5) }, 50)).toBe(2)
expect(positionsInView({ ...geometry, x: (value) => 100 + 10 * (value - 48) }, 50)).toBe(2)
})

it('counts them right far from the first position, though Plotly rounds pixels', () => {
// Positions 325.95 to 526.25 of 600 in view, in a plot 1014 pixels wide, each position placed
// to a hundredth of a pixel, as Plotly does: 326 to 526 are in view.
const [from, to] = [325.9483905287492, 526.2466917324643]
const pixels = 1014 / (to - from)
const geometry = {
area: { left: 70, top: 0, width: 1014, height: 100 },
x: (value: number) => 70 + Math.round(pixels * (value - from) * 100) / 100,
y: (value: number) => value,
yAxes: new Map(),
}
expect(positionsInView(geometry, 600)).toBe(201)
})

it('counts a position a fraction of a pixel beyond an edge, where rounding may put it', () => {
// Positions 0 to 200 across a plot from 70 to 1070 pixels, each placed to a hundredth of a
// pixel: the last lands at 1070.01.
const geometry = {
area: { left: 70, top: 0, width: 1000, height: 100 },
x: (value: number) => 70 + Math.round(value * 500.005) / 100,
y: (value: number) => value,
yAxes: new Map(),
}
expect(geometry.x(200)).toBeGreaterThan(1070)
expect(positionsInView(geometry, 201)).toBe(201)
})
})
78 changes: 77 additions & 1 deletion v5/client/src/chart-markers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
* The marker shapes the charts tell their traces apart by, beside their colours: the Graph page's
* machines (GR1), the Compare page's shadows (CP8).
* machines (GR1), the Compare page's shadows (CP8). And when their points have markers: only while
* few enough positions of the x-axis are in view.
*/

import { useState } from 'react'
import type { PlotGeometry } from './components/chart'

/** Each marker: Plotly's symbol, and the character showing it on a chip. */
const MARKERS = [
{ symbol: 'circle', char: '●' },
Expand All @@ -19,3 +23,75 @@ const MARKERS = [
export function markerOf(index: number): (typeof MARKERS)[number] {
return MARKERS[index % MARKERS.length]
}

/**
* At most how many positions of the x-axis -- the Graph's commits (GR1), Compare's tests (CP8) --
* can be in view for the traces' points to have markers. Beyond, the markers are too close
* together to tell apart, and drawing one per point is what makes a large chart slow to draw.
*/
export const MARKED_POSITIONS = 200

/**
* The `maxdisplayed` of a trace's markers. With it, Plotly draws only the markers of the points in
* view (x and y in range), rather than every point's, and only that many of them, evenly spaced,
* when more are in view. That leaves room for the marker of every point in view while the points
* have markers, but for a draw counted on the axis drawn before (see `useMarkers`), which it keeps
* cheap.
*/
export const MAX_DISPLAYED_MARKERS = 2 * MARKED_POSITIONS

/**
* How many pixels beyond an edge of the plot a position still counts as in view (see
* `positionsInView`). An x-axis fitting a line without markers ends at its last point, which
* Plotly's rounding may put a fraction of a pixel beyond the edge. Counted out of view, it would
* give the points markers, which Plotly pads the axis for, bringing it back into view, and so on.
*/
const EDGE_SLACK = 0.5

/**
* How many of the `count` positions of the x-axis are in view in a plot drawn as `geometry` says:
* the positions from 0 to `count - 1` between the left and right edges of the plot, give or take
* EDGE_SLACK. Plotly rounds the pixels of a position, so the step between two is taken across the
* whole axis, lest its error add up far from position 0.
*/
export function positionsInView({ area, x }: PlotGeometry, count: number): number {
const span = Math.max(count - 1, 1)
const step = (x(span) - x(0)) / span
const [left, right] = [area.left - EDGE_SLACK, area.left + area.width + EDGE_SLACK]
const first = Math.max(0, Math.ceil((left - x(0)) / step))
const last = Math.min(count - 1, Math.floor((right - x(0)) / step))
return Math.max(0, last - first + 1)
}

/**
* How a trace of `points` points is drawn: a line with a marker at each point, or the line alone
* when the points have no `markers` (see `MARKED_POSITIONS`). A single point keeps its marker,
* without which it would draw nothing.
*/
export function traceMode(points: number, markers: boolean): 'lines' | 'lines+markers' {
return markers || points === 1 ? 'lines+markers' : 'lines'
}

/**
* Where a chart of `count` positions on its x-axis last drew, as its `onDraw` reports, and whether
* its points have markers: while at most MARKED_POSITIONS are in view, all of them counting as in
* view until the chart has drawn for `revision`, a change of which resets its zoom. Drawing the
* markers or not asks for no other draw: zoomed in, the axis stays as the user left it, and not
* zoomed, every position is in view, however Plotly pads the axis for the markers.
*
* A new `count` alone does not forget the draw: zoomed in, it moves nothing, and the chart reports
* no draw that moves nothing (see chart.tsx), so that the count would wait for the next zoom. Not
* zoomed, it is counted on the axis drawn before until the chart draws again: a `count` growing
* past MARKED_POSITIONS is drawn once with markers, then again without.
*/
export function useMarkers(count: number, revision: string) {
const [drawnAt, setDrawnAt] = useState<{ geometry: PlotGeometry; revision: string } | null>(
null,
)
const inView = drawnAt?.revision === revision ? positionsInView(drawnAt.geometry, count) : count
return {
geometry: drawnAt?.geometry ?? null,
markers: inView <= MARKED_POSITIONS,
onDraw: (geometry: PlotGeometry) => setDrawnAt({ geometry, revision }),
}
}
86 changes: 31 additions & 55 deletions v5/client/src/pages/compare/chart.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest'
import { MAX_DISPLAYED_MARKERS } from '../../chart-markers'
import { hoverText } from '../../test/hover-text'
import {
barTrace,
Expand All @@ -7,50 +8,22 @@ import {
compareLayout,
ratioTicks,
SHADOW_COLORS,
shadowLook,
shadowTrace,
} from './chart'
import { statisticsOf, type Change, type ComparisonRow, type Status } from './comparison'
import { interval } from './compare-world'
import { comparedRow, interval } from './compare-world'
import { MAX_SHADOWS } from './shadow-specs'

/** A change with only one run on side A. */
const ONE_RUN: Change = { kind: 'runs', sides: 'A' }

/** A compared row of `test`, valued `a` and `b`, with the ratio and Delta % they give. */
function row(
test: string,
a: number | null,
b: number | null,
status: Status = 'inconclusive',
change: Change = ONE_RUN,
) {
const statistics = { ...statisticsOf([]), runs: 0 }
const ratio = a !== null && b !== null && a !== 0 ? b / a : null
return {
test,
a,
b,
delta: a !== null && b !== null ? b - a : null,
deltaPct: ratio === null ? null : ((b! - a!) / Math.abs(a!)) * 100,
ratio,
status,
change,
margin: 5,
statisticsA: statistics,
statisticsB: statistics,
} satisfies ComparisonRow
}

describe('chartBars', () => {
it('keeps the rows with a positive ratio, by ratio, ties by name', () => {
const bars = chartBars([
row('d', 1, 3),
row('b', 2, 1),
row('c', 1, 2),
row('a', 2, 4),
row('one-sided', 1, null),
row('zero', 0, 1),
row('opposite', -1, 1),
comparedRow('d', 1, 3),
comparedRow('b', 2, 1),
comparedRow('c', 1, 2),
comparedRow('a', 2, 4),
comparedRow('one-sided', 1, null),
comparedRow('zero', 0, 1),
comparedRow('opposite', -1, 1),
])
expect(bars.map((bar) => bar.test)).toEqual(['b', 'a', 'c', 'd'])
})
Expand Down Expand Up @@ -98,23 +71,23 @@ describe('ratioTicks', () => {

describe('chartExtent', () => {
it('spans the bars from 0', () => {
expect(chartExtent(chartBars([row('a', 1, 2), row('b', 1, 1.5)]))).toEqual([0, 1])
expect(chartExtent(chartBars([row('a', 2, 1)]))).toEqual([-1, 0])
expect(chartExtent(chartBars([comparedRow('a', 1, 2), comparedRow('b', 1, 1.5)]))).toEqual([0, 1])
expect(chartExtent(chartBars([comparedRow('a', 2, 1)]))).toEqual([-1, 0])
})

it("spans every shadow's points too", () => {
const bars = chartBars([row('a', 1, 2)])
const shadows = [chartBars([row('a', 1, 4)]), chartBars([row('b', 2, 1)])]
const bars = chartBars([comparedRow('a', 1, 2)])
const shadows = [chartBars([comparedRow('a', 1, 4)]), chartBars([comparedRow('b', 2, 1)])]
expect(chartExtent(bars, shadows)).toEqual([-1, 2])
})
})

describe('barTrace', () => {
const bars = chartBars([
row('<b>', 2, 1, 'improved', interval(0.4, 0.6)),
row('slow', 1, 2, 'regressed', interval(1.8, 2.2)),
row('same', 1, 1.001),
row('flat', 1, 1.0001, 'unchanged', interval(0.99, 1.02, 1)),
comparedRow('<b>', 2, 1, 'improved', interval(0.4, 0.6)),
comparedRow('slow', 1, 2, 'regressed', interval(1.8, 2.2)),
comparedRow('same', 1, 1.001),
comparedRow('flat', 1, 1.0001, 'unchanged', interval(0.99, 1.02, 1)),
])

it('draws a bar per test at its log2 ratio, coloured by status', () => {
Expand Down Expand Up @@ -154,11 +127,12 @@ describe('barTrace', () => {
})

describe('shadowTrace', () => {
const points = chartBars([row('a', 1, 2), row('b', 2, 1)])
const points = chartBars([comparedRow('a', 1, 2), comparedRow('b', 2, 1)])

it('draws a line through the points, with the tooltip of a bar naming the shadow', () => {
const trace = shadowTrace(points, 0, 'Shadow: s › m › c')
const trace = shadowTrace(points, 0, 'Shadow: s › m › c', true)
expect(trace).toMatchObject({ type: 'scatter', mode: 'lines+markers', x: [0, 1], y: [-1, 1] })
expect(trace.marker).toMatchObject({ maxdisplayed: MAX_DISPLAYED_MARKERS })
expect(hoverText(trace, 0)).toBe(
'<b>b</b><br>Shadow: s › m › c<br>Ratio: 0.5000<br>A: 2<br>B: 1<br>Delta %: -50.00%' +
'<extra></extra>',
Expand All @@ -168,26 +142,28 @@ describe('shadowTrace', () => {
})

it('escapes the label, and keeps it out of the template', () => {
const trace = shadowTrace(points, 0, 'Shadow: s › <m&> › %{x}')
const trace = shadowTrace(points, 0, 'Shadow: s › <m&> › %{x}', true)
expect((trace.customdata as unknown as string[][])[1][5]).toBe(
'<br>Shadow: s › &lt;m&amp;&gt; › %{x}',
)
expect(trace.hovertemplate).not.toContain('%{x}')
})

it('gives each of the shadows a colour and a marker of its own, apart from the statuses', () => {
it('gives each of the shadows a colour, a marker and a dash of its own, apart from the statuses', () => {
const looks = SHADOW_COLORS.map((_, i) => {
const { marker, line } = shadowTrace(points, i, '')
const { marker, line } = shadowTrace(points, i, '', true)
expect(line?.color).toBe((marker as { color: string }).color)
return marker as { color: string; symbol: string }
return { ...(marker as { color: string; symbol: string }), dash: line?.dash }
})
expect(new Set(looks.map((look) => look.color)).size).toBe(MAX_SHADOWS)
expect(new Set(looks.map((look) => look.symbol)).size).toBe(MAX_SHADOWS)
expect(new Set(looks.map((look) => look.dash)).size).toBe(MAX_SHADOWS)
expect(looks.map((look) => look.dash)).toEqual(looks.map((_, i) => shadowLook(i).dash))
const everyStatus = [
row('i', 2, 1, 'improved', interval(0.4, 0.6)),
row('r', 1, 2, 'regressed', interval(1.8, 2.2)),
row('u', 1, 1, 'unchanged', interval(0.99, 1.01)),
row('n', 1, 1),
comparedRow('i', 2, 1, 'improved', interval(0.4, 0.6)),
comparedRow('r', 1, 2, 'regressed', interval(1.8, 2.2)),
comparedRow('u', 1, 1, 'unchanged', interval(0.99, 1.01)),
comparedRow('n', 1, 1),
]
const statuses = (barTrace(chartBars(everyStatus)).marker as { color: string[] }).color
expect(looks.some((look) => statuses.includes(look.color))).toBe(false)
Expand Down
38 changes: 26 additions & 12 deletions v5/client/src/pages/compare/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* chart.
*/

import type { Layout, PlotData } from 'plotly.js'
import { markerOf } from '../../chart-markers'
import type { Dash, Layout, PlotData } from 'plotly.js'
import { markerOf, MAX_DISPLAYED_MARKERS, traceMode } from '../../chart-markers'
import { noDataAnnotations } from '../../components/plot-annotations'
import { compareStrings, escapeHtml, formatNumber } from '../../format'
import { formatDeltaPct, formatRatio, intervalIs, noIntervalReason } from './columns'
Expand Down Expand Up @@ -60,14 +60,26 @@ const STATUS_COLORS: Record<Status, string> = {
/**
* The colours of the shadows' lines (CP8), by their position among the shadows: apart from those
* of the statuses, from the outline of a bar (CP5) and from each other, each with a contrast of at
* least 3:1 against the page's background. Each shadow also has a marker of its own, since
* colours alone cannot keep five lines apart for every reader.
* least 3:1 against the page's background. Each shadow also has a dash pattern of its own, and a
* marker while its points have them, since colours alone cannot keep five lines apart for every
* reader.
*/
export const SHADOW_COLORS = ['#7c3aed', '#552200', '#0369a1', '#86198f', '#312e81'] as const

/** The colour and the marker of the `index`th shadow. */
/**
* The dash patterns of the shadows' lines (CP8), by their position among the shadows: `solid`, or
* the lengths of their dashes and gaps, which Plotly and the shadows' chips both draw, so that a
* chip's stroke is dashed as its line is.
*/
export const SHADOW_DASHES = ['solid', '6px,4px', '2px,3px', '7px,3px,2px,3px', '14px,5px'] as const

/** The colour, the marker and the dash pattern of the `index`th shadow. */
export function shadowLook(index: number) {
return { color: SHADOW_COLORS[index % SHADOW_COLORS.length], marker: markerOf(index) }
return {
color: SHADOW_COLORS[index % SHADOW_COLORS.length],
marker: markerOf(index),
dash: SHADOW_DASHES[index % SHADOW_DASHES.length],
}
}

/** A main bar's status, with its confidence interval or why it has none, as a line of a tooltip. */
Expand Down Expand Up @@ -123,23 +135,25 @@ export function barTrace(bars: readonly Bar<ComparisonRow>[]): Partial<PlotData>

/**
* The comparison of the `index`th shadow, which `label` names on a line of the tooltips after the
* test's, as a thin line through its points, sorted by their own ratios, in its colour and with
* its marker (CP8).
* test's, as a thin line through its points, sorted by their own ratios, in its colour and dash
* pattern, with its marker at each point while they have `markers` (CP8).
*/
export function shadowTrace(
points: readonly Bar[],
index: number,
label: string,
markers: boolean,
): Partial<PlotData> {
const { color, marker } = shadowLook(index)
const { color, marker, dash } = shadowLook(index)
return {
type: 'scatter',
mode: 'lines+markers',
mode: traceMode(points.length, markers),
name: `shadow ${index + 1}`,
// A point of a line has no details, nor a status (CP8).
...plotted(points, { label }),
line: { color, width: 1.5 },
marker: { color, size: 6, symbol: marker.symbol },
// Plotly takes lengths as well as the names of its own patterns, which are all its types allow.
line: { color, width: 1.5, dash: dash as Dash },
marker: { color, size: 6, symbol: marker.symbol, maxdisplayed: MAX_DISPLAYED_MARKERS },
}
}

Expand Down
Loading