Skip to content

feat: add provisioning timings to understand slow build times #14274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7e36010
Initial implementation
dannykopping Aug 7, 2024
45b7fb4
More hacking, now including a job-wide view of timings
dannykopping Aug 13, 2024
38c4197
API
dannykopping Aug 13, 2024
803a9d4
Smol refactor
dannykopping Aug 14, 2024
973ec6d
Capture dependency graph timings
dannykopping Aug 14, 2024
a070e07
Expand hash to include span category so multiple operations on the sa…
dannykopping Aug 14, 2024
4a29b96
Tests
dannykopping Aug 15, 2024
275bfca
lint/fmt
dannykopping Aug 15, 2024
73bac3f
Moar tests
dannykopping Aug 15, 2024
3d77c63
Improve coverage
dannykopping Aug 15, 2024
c0ae1ba
Remove stats API call, will follow up in another PR
dannykopping Aug 15, 2024
28fa2f7
Fixing tests
dannykopping Aug 15, 2024
68b16ff
Use max(end)-min(start) as stage timings, not local maximum
dannykopping Aug 15, 2024
6f0b8f8
make fmt
dannykopping Aug 15, 2024
724f139
Minor fix-ups
dannykopping Aug 15, 2024
c30a900
Pls god let this work
dannykopping Aug 15, 2024
0d68e69
Move terraform test helpers into internal package
dannykopping Aug 19, 2024
15282bb
Review comments
dannykopping Aug 19, 2024
82ca13e
Merge branch 'main' of github.com:coder/coder into dk/provision-detai…
dannykopping Aug 19, 2024
597ec85
More CI happiness
dannykopping Aug 19, 2024
805c0f2
Restrict timings tests to non-Windows
dannykopping Aug 19, 2024
46f3318
Give CI exactly what it wants FFS (see https://github.com/coder/coder…
dannykopping Aug 19, 2024
ebbaf31
@mtojek you legend :)
dannykopping Aug 19, 2024
eb5ec5c
Merge branch 'main' of https://github.com/coder/coder into dk/provisi…
dannykopping Aug 20, 2024
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
Prev Previous commit
Next Next commit
Moar tests
Signed-off-by: Danny Kopping <danny@coder.com>
  • Loading branch information
dannykopping committed Aug 15, 2024
commit 73bac3f4f020432f38a158ca81d9b593aa1844ad
148 changes: 148 additions & 0 deletions provisioner/terraform/testdata/timings-aggregation/fake-terraform.sh

Large diffs are not rendered by default.

140 changes: 140 additions & 0 deletions provisioner/terraform/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package terraform

import (
"bufio"
"bytes"
"slices"
"testing"

"github.com/cespare/xxhash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/encoding/protojson"
protobuf "google.golang.org/protobuf/proto"

"github.com/coder/coder/v2/provisionersdk/proto"
)

func ParseTimingLines(t *testing.T, input []byte) []*proto.Timing {
t.Helper()

// Parse the input into *proto.Timing structs.
var expected []*proto.Timing
scanner := bufio.NewScanner(bytes.NewBuffer(input))
for scanner.Scan() {
line := scanner.Bytes()

var msg proto.Timing
require.NoError(t, protojson.Unmarshal(line, &msg))

expected = append(expected, &msg)
}
require.NoError(t, scanner.Err())
StableSortTimings(t, expected) // To reduce flakiness.

return expected
}

func TimingsAreEqual(t *testing.T, expected []*proto.Timing, actual []*proto.Timing) bool {
t.Helper()

// Shortcut check.
if len(expected)+len(actual) == 0 {
t.Logf("both timings are empty")
return true
}

// Shortcut check.
if len(expected) != len(actual) {
t.Logf("timings lengths are not equal: %d != %d", len(expected), len(actual))
return false
}

// Compare each element; both are expected to be sorted in a stable manner.
for i := 0; i < len(expected); i++ {
ex := expected[i]
ac := actual[i]
if !protobuf.Equal(ex, ac) {
t.Logf("timings are not equivalent: %q != %q", ex.String(), ac.String())
return false
}
}

return true
}

func IngestAllSpans(t *testing.T, input []byte, aggregator *timingAggregator) {
t.Helper()

scanner := bufio.NewScanner(bytes.NewBuffer(input))
for scanner.Scan() {
line := scanner.Bytes()
log := parseTerraformLogLine(line)
if log == nil {
continue
}

ts, span, err := extractTimingSpan(log)
if err != nil {
// t.Logf("%s: failed span extraction on line: %q", err, line)
continue
}

require.NotZerof(t, ts, "failed on line: %q", line)
require.NotNilf(t, span, "failed on line: %q", line)

aggregator.ingest(ts, span)
}

require.NoError(t, scanner.Err())
}

func PrintTiming(t *testing.T, timing *proto.Timing) {
t.Helper()

marshaler := protojson.MarshalOptions{
Multiline: false, // Ensure it's set to false for single-line JSON
Indent: "", // No indentation
}

out, err := marshaler.Marshal(timing)
assert.NoError(t, err)
t.Logf("%s", out)
}

func StableSortTimings(t *testing.T, timings []*proto.Timing) {
t.Helper()

slices.SortStableFunc(timings, func(a, b *proto.Timing) int {
if a == nil || b == nil || a.Start == nil || b.Start == nil {
return 0
}

if a.Start.AsTime().Equal(b.Start.AsTime()) {
// Special case: when start times are equal, we need to keep the ordering stable, so we hash both entries
// and sort based on that (since end times could be equal too, in principle).
ah := xxhash.Sum64String(a.String())
bh := xxhash.Sum64String(b.String())

if ah == bh {
// WTF.
t.Logf("identical timings detected!")
PrintTiming(t, a)
PrintTiming(t, b)
return 0
}

if ah < bh {
return -1
}

return 1
}

if a.Start.AsTime().Before(b.Start.AsTime()) {
return -1
}

return 1
})
}
134 changes: 7 additions & 127 deletions provisioner/terraform/timings_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package terraform

import (
"bufio"
"bytes"
_ "embed"
"slices"
"testing"

"github.com/cespare/xxhash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/tools/txtar"
"google.golang.org/protobuf/encoding/protojson"
protobuf "google.golang.org/protobuf/proto"

"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/provisionersdk/proto"
Expand Down Expand Up @@ -79,138 +73,24 @@ func TestAggregation(t *testing.T) {
file.Name, database.AllProvisionerJobTimingStageValues())

agg := newTimingAggregator(stage)
extractAllSpans(t, file.Data, agg)
IngestAllSpans(t, file.Data, agg)
actualTimings = append(actualTimings, agg.aggregate()...)
}

stableSortTimings(t, actualTimings) // To reduce flakiness.
require.True(t, timingsAreEqual(t, expectedTimings.Data, actualTimings))
expected := ParseTimingLines(t, expectedTimings.Data)
StableSortTimings(t, actualTimings) // To reduce flakiness.
if !assert.True(t, TimingsAreEqual(t, expected, actualTimings)) {
printExpectation(t, expected)
}
})
}
}

func timingsAreEqual(t *testing.T, input []byte, actual []*proto.Timing) bool {
t.Helper()

// Parse the input into *proto.Timing structs.
var expected []*proto.Timing
scanner := bufio.NewScanner(bytes.NewBuffer(input))
for scanner.Scan() {
line := scanner.Bytes()

var msg proto.Timing
require.NoError(t, protojson.Unmarshal(line, &msg))

expected = append(expected, &msg)
}
require.NoError(t, scanner.Err())

// Shortcut check.
if len(expected)+len(actual) == 0 {
t.Logf("both timings are empty")
return true
}

// Shortcut check.
if len(expected) != len(actual) {
t.Logf("timings lengths are not equal: %d != %d", len(expected), len(actual))
printExpectation(t, actual)
return false
}

// Compare each element; both are expected to be sorted in a stable manner.
for i := 0; i < len(expected); i++ {
ex := expected[i]
ac := actual[i]
if !protobuf.Equal(ex, ac) {
t.Logf("timings are not equivalent: %q != %q", ex.String(), ac.String())
printExpectation(t, actual)
return false
}
}

return true
}

func extractAllSpans(t *testing.T, input []byte, aggregator *timingAggregator) {
t.Helper()

scanner := bufio.NewScanner(bytes.NewBuffer(input))
for scanner.Scan() {
line := scanner.Bytes()
log := parseTerraformLogLine(line)
if log == nil {
continue
}

ts, span, err := extractTimingSpan(log)
if err != nil {
// t.Logf("%s: failed span extraction on line: %q", err, line)
continue
}

require.NotZerof(t, ts, "failed on line: %q", line)
require.NotNilf(t, span, "failed on line: %q", line)

aggregator.ingest(ts, span)
}

require.NoError(t, scanner.Err())
}

func printExpectation(t *testing.T, actual []*proto.Timing) {
t.Helper()

t.Log("expected:")
for _, a := range actual {
printTiming(t, a)
}
}

func printTiming(t *testing.T, timing *proto.Timing) {
t.Helper()

marshaler := protojson.MarshalOptions{
Multiline: false, // Ensure it's set to false for single-line JSON
Indent: "", // No indentation
PrintTiming(t, a)
}

out, err := marshaler.Marshal(timing)
assert.NoError(t, err)
t.Logf("%s", out)
}

func stableSortTimings(t *testing.T, timings []*proto.Timing) {
slices.SortStableFunc(timings, func(a, b *proto.Timing) int {
if a == nil || b == nil || a.Start == nil || b.Start == nil {
return 0
}

if a.Start.AsTime().Equal(b.Start.AsTime()) {
// Special case: when start times are equal, we need to keep the ordering stable, so we hash both entries
// and sort based on that (since end times could be equal too, in principle).
ah := xxhash.Sum64String(a.String())
bh := xxhash.Sum64String(b.String())

if ah == bh {
// WTF.
t.Logf("identical timings detected!")
printTiming(t, a)
printTiming(t, b)
return 0
}

if ah < bh {
return -1
}

return 1
}

if a.Start.AsTime().Before(b.Start.AsTime()) {
return -1
}

return 1
})
}
Loading