Skip to content
quarry Artifact [a46471c772]
Login

Artifact [a46471c772]

Artifact a46471c77240b8c4b4dc7890cce98cf15fc3dda8fd59986d6866ee12edda9ca6:


package timing

import (
	"strings"
	"testing"
	"time"
)

// "<1ms" was true and useless. Most pages are sub-millisecond now, so
// the floor collapsed the interesting range into one label — an
// improvement from 900µs to 90µs was invisible. Fossil has the
// mirror-image problem: three decimals of seconds means everything
// faster than a millisecond prints as "0.000s".
func TestFormatKeepsPrecisionBelowAMillisecond(t *testing.T) {
	for _, tc := range []struct {
		d    time.Duration
		want string
	}{
		{4200 * time.Nanosecond, "4.2µs"},
		{421 * time.Microsecond, "421µs"},
		{999 * time.Microsecond, "999µs"},
		{4210 * time.Microsecond, "4.21ms"},
		{42100 * time.Microsecond, "42.1ms"},
		{536 * time.Millisecond, "536.0ms"},
		{2632 * time.Millisecond, "2.63s"},
		{0, "0"},
	} {
		if got := Format(tc.d); got != tc.want {
			t.Errorf("Format(%v) = %q, want %q", tc.d, got, tc.want)
		}
	}
	// The whole point: two sub-millisecond durations an order of
	// magnitude apart must not render identically.
	if Format(900*time.Microsecond) == Format(90*time.Microsecond) {
		t.Error("900µs and 90µs render the same — the floor is back")
	}
}

// The breakdown names only layers that did work, busiest first.
func TestBreakdownOrdersByCost(t *testing.T) {
	tm := &Timing{by: map[Category]*entry{}}
	tm.Add(SQL, 2*time.Millisecond)
	tm.Add(Zit, 150*time.Millisecond)
	tm.Add(Zit, 100*time.Millisecond)

	got := tm.Breakdown()
	if !strings.HasPrefix(got, "zit ") {
		t.Errorf("breakdown = %q, want the costliest layer first", got)
	}
	if !strings.Contains(got, "×2") {
		t.Errorf("breakdown = %q, want the zit call count", got)
	}
	if strings.Contains(got, "chat") || strings.Contains(got, "http") {
		t.Errorf("breakdown = %q, want idle layers omitted", got)
	}
	if (*Timing)(nil).Breakdown() != "" {
		t.Error("nil Timing must render empty")
	}
}