tsenart/vegeta · error

lttb: min threshold is 3

Error message

lttb: min threshold is 3

What it means

Downsample in lib/lttb rejects thresholds below 3 because Largest-Triangle-Three-Buckets requires at least three buckets (first point, intermediate buckets, last point). Smaller thresholds cannot produce a meaningful downsampling.

Source

Thrown at lib/lttb/lttb.go:27

// count number of Points or an error.
type Iter func(count int) ([]Point, error)

// Downsample `count` number of data points retrieved from the given iterator
// function to contain only `threshold` number of points while maintaining close
// visual similarity to the original data. The algorithm is called
// Largest-Triangle-Three-Buckets and is described in:
// https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf
//
// This implementation grew out of https://github.com/dgryski/go-lttb
// to limit memory usage by leveraging iterators.
func Downsample(count, threshold int, it Iter) ([]Point, error) {
	if threshold >= count || threshold == 0 {
		points, err := it(count)
		return points, err
	}

	if threshold < 3 {
		return nil, errors.New("lttb: min threshold is 3")
	}

	// Bucket size. Leave room for start and end data points
	size := float64(count-2) / float64(threshold-2)

	// Get the first point and the current bucket.
	points, err := it(int(1 + size))
	if err != nil {
		return nil, err
	}

	samples := make([]Point, 0, threshold)
	samples = append(samples, points[0]) // Always add the first point
	current := points[1:]

	for i := 0; i < threshold-2; i++ {
		// Calculate bucket boundaries (non inclusive hi)
		lo := int(float64(i+1)*size) + 1

View on GitHub (pinned to cf58112690)

Solutions

  1. Use a threshold of at least 3.
  2. Guard the call site: if threshold < 3, either skip downsampling or clamp it to 3.
  3. If the input data itself is shorter than the threshold, note the count<=threshold branch returns data unchanged, so the error only concerns the threshold value.

Example fix

// before
points, err := lttb.Downsample(data, 2)
// after
if t < 3 { t = 3 }
points, err := lttb.Downsample(data, t)
Defensive patterns

Strategy: validation

Validate before calling

if threshold < 3 {
    threshold = 3
}
points, err := lttb.Downsample(data, threshold)

Prevention

When it happens

Trigger: Calling lttb.Downsample(data, threshold) with threshold 1 or 2 (but not 0, which takes the earlier pass-through branch).

Common situations: Computing a target point count from plot dimensions or user input that rounds down to 1–2, or hardcoding a tiny threshold in tests/scripts.

Related errors


AI-assisted analysis of tsenart/vegeta@cf58112690 (2026-08-31). Data as JSON: /api/errors/31af02cefb685f6c. Report an issue: GitHub.