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
6 changes: 3 additions & 3 deletions internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/DataDog/ddtest/internal/buildinfo"
"github.com/DataDog/ddtest/internal/constants"
"github.com/DataDog/ddtest/internal/git"
"github.com/DataDog/ddtest/internal/planner"
"github.com/DataDog/ddtest/internal/runner"
"github.com/DataDog/ddtest/internal/settings"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,9 +39,8 @@ var planCmd = &cobra.Command{
),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
testRunner := runner.New()
if err := testRunner.Plan(ctx); err != nil {
slog.Error("Runner failed", "error", err)
if err := planner.Plan(ctx); err != nil {
slog.Error("Planner failed", "error", err)
os.Exit(1)
}
},
Expand Down
73 changes: 73 additions & 0 deletions internal/planner/discovered_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package planner

import (
"log/slog"

"github.com/DataDog/ddtest/internal/testoptimization"
)

func (tp *TestPlanner) recordFullDiscoveryResults(
discoveredTests []testoptimization.Test,
skippableTests map[string]bool,
subdirPrefix string,
) {
discoveredTestsCount := len(discoveredTests)
if discoveredTestsCount == 0 {
slog.Info("Full test discovery returned no tests")
return
}

slog.Info("Using full test discovery results")
skippableTestsCount := 0
for _, test := range discoveredTests {
normalizedSourceFile := stripCwdSubdirPrefix(test.SuiteSourceFile, subdirPrefix)
if normalizedSourceFile != "" {
tp.testFiles[normalizedSourceFile] = struct{}{}
}

if !skippableTests[test.FQN()] {
slog.Debug("Test is not skipped", "test", test.FQN(), "sourceFile", test.SuiteSourceFile)
recordRunnableTest(tp.suiteAggregates, test, normalizedSourceFile)
} else {
recordSkippedTest(tp.suiteAggregates, test, normalizedSourceFile)
skippableTestsCount++
}
}

slog.Info("Processed the discovered tests", "skippableTestsCount", skippableTestsCount, "discoveredTestsCount", discoveredTestsCount)
}

func (tp *TestPlanner) recordFastDiscoveryFallbackFiles(discoveredTestFiles []string) {
for _, testFile := range discoveredTestFiles {
if testFile != "" {
tp.testFiles[testFile] = struct{}{}
}
}
}

func recordRunnableTest(suiteAggregates map[testSuiteKey]testSuiteAggregate, test testoptimization.Test, sourceFile string) {
aggregate := suiteAggregateForTest(suiteAggregates, test, sourceFile)
aggregate.NumTests++
suiteAggregates[testSuiteKey{Module: test.Module, Suite: test.Suite}] = aggregate
}

func recordSkippedTest(suiteAggregates map[testSuiteKey]testSuiteAggregate, test testoptimization.Test, sourceFile string) {
aggregate := suiteAggregateForTest(suiteAggregates, test, sourceFile)
aggregate.NumTests++
aggregate.NumTestsSkipped++
suiteAggregates[testSuiteKey{Module: test.Module, Suite: test.Suite}] = aggregate
}

func suiteAggregateForTest(suiteAggregates map[testSuiteKey]testSuiteAggregate, test testoptimization.Test, sourceFile string) testSuiteAggregate {
key := testSuiteKey{
Module: test.Module,
Suite: test.Suite,
}
aggregate := suiteAggregates[key]
if aggregate.SourceFile == "" {
aggregate.Module = test.Module
aggregate.Suite = test.Suite
aggregate.SourceFile = sourceFile
}
return aggregate
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package runner
package planner

import (
"container/heap"
"errors"
"fmt"
"log/slog"
"os"
"path/filepath"
"slices"
Expand All @@ -12,19 +14,47 @@ import (
"github.com/DataDog/ddtest/internal/constants"
)

// DistributeTestFiles distributes test files across parallel runners using weighted list scheduling.
func DistributeTestFiles(testFiles map[string]int, parallelRunners int) [][]string {
// DistributeTestFiles distributes test files using weights loaded into this planner.
func (tp *TestPlanner) DistributeTestFiles(testFiles []string, parallelRunners int) [][]string {
if !tp.planLoaded {
if err := tp.restoreTestOptimizationPlanCache(); err != nil {
if errors.Is(err, os.ErrNotExist) {
slog.Debug("Test optimization run artifacts not found; distributing test files with default weights")
} else {
slog.Warn("Failed to load test optimization run artifacts; distributing test files with default weights", "error", err)
}
}
}

testFileWeights := testFileWeightsForFiles(tp.testFileWeights, testFiles)
return tp.DistributeWeightedTestFiles(testFileWeights, parallelRunners)
}

// DistributeWeightedTestFiles distributes test files across parallel runners using weighted list scheduling.
func (tp *TestPlanner) DistributeWeightedTestFiles(testFiles map[string]int, parallelRunners int) [][]string {
builder := newTestSplitBuilder(parallelRunners)
return builder.distributeFiles(testFiles)
}

func testFileWeightsForFiles(cacheWeights map[string]int, testFiles []string) map[string]int {
testFileWeights := make(map[string]int, len(testFiles))
for _, testFile := range testFiles {
if cachedWeight, ok := cacheWeights[testFile]; ok && cachedWeight > 0 {
testFileWeights[testFile] = cachedWeight
} else {
testFileWeights[testFile] = DefaultTestFileWeight
}
}
return testFileWeights
}

// CreateTestSplits creates test split files for parallel runners
// For multiple runners: distributes files using weighted list scheduling and writes to separate runner files
// For single runner: copies test-files.txt content to runner-0
func CreateTestSplits(testFiles map[string]int, parallelRunners int, testFilesOutputPath string) error {
func (tp *TestPlanner) CreateTestSplits(testFiles map[string]int, parallelRunners int, testFilesOutputPath string) error {
if parallelRunners > 1 {
// Distribute test files across parallel runners using weighted list scheduling.
distribution := DistributeTestFiles(testFiles, parallelRunners)
distribution := tp.DistributeWeightedTestFiles(testFiles, parallelRunners)
if err := writeDistributedTestSplits(distribution, constants.TestsSplitDir); err != nil {
return err
}
Expand Down
Loading
Loading