go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/changepoints/grouping_test.go (about)

     1  // Copyright 2024 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package changepoints
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    22  	"cloud.google.com/go/bigquery"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func TestGroupChangepoints(t *testing.T) {
    28  	t.Parallel()
    29  	Convey("TestGroupChangepoints", t, func() {
    30  		// Group 1 - test id num gap less than the TestIDGroupingThreshold in the same group.
    31  		cp1 := makeChangepointRow(4, 100, 200, "refhash")
    32  		cp2 := makeChangepointRow(30, 160, 261, "refhash") // 41 commits, 40.6% overlap with cp1
    33  		cp3 := makeChangepointRow(90, 100, 200, "refhash")
    34  		cp4 := makeChangepointRow(1, 100, 300, "refhash") // large regression range.
    35  		// Group 2 - same test id group, but different regression range with group 1.
    36  		cp5 := makeChangepointRow(2, 161, 263, "refhash") // 40 commits. 39.6% overlap wtih cp 1.
    37  		cp6 := makeChangepointRow(3, 161, 264, "refhash")
    38  		// Group 4 - different id group
    39  		cp7 := makeChangepointRow(1000, 100, 200, "refhash")
    40  		cp8 := makeChangepointRow(1001, 100, 200, "refhash")
    41  		// Group 5 - same test variant can't be in the same group more than once.
    42  		cp9 := makeChangepointRow(1001, 120, 230, "refhash")
    43  		// Group 6 - different branch should't be grouped together.
    44  		cp10 := makeChangepointRow(1002, 120, 230, "otherrefhash")
    45  
    46  		groups := GroupChangepoints([]*ChangepointRow{cp1, cp2, cp3, cp4, cp5, cp6, cp7, cp8, cp9, cp10})
    47  		So(groups, ShouldResemble, [][]*ChangepointRow{
    48  			{cp1, cp3, cp2, cp4},
    49  			{cp5, cp6},
    50  			{cp7, cp8},
    51  			{cp9},
    52  			{cp10},
    53  		})
    54  	})
    55  }
    56  
    57  func makeChangepointRow(TestIDNum, lowerBound, upperBound int64, refHash string) *ChangepointRow {
    58  	return &ChangepointRow{
    59  		Project:     "chromium",
    60  		TestIDNum:   TestIDNum,
    61  		TestID:      fmt.Sprintf("test%d", TestIDNum),
    62  		VariantHash: "varianthash",
    63  		Ref: &Ref{
    64  			Gitiles: &Gitiles{
    65  				Host:    bigquery.NullString{Valid: true, StringVal: "host"},
    66  				Project: bigquery.NullString{Valid: true, StringVal: "project"},
    67  				Ref:     bigquery.NullString{Valid: true, StringVal: "ref"},
    68  			},
    69  		},
    70  		RefHash:                      refHash,
    71  		UnexpectedVerdictRateCurrent: 0,
    72  		UnexpectedVerdictRateAfter:   0.99,
    73  		UnexpectedVerdictRateBefore:  0.3,
    74  		StartHour:                    time.Unix(1000, 0),
    75  		LowerBound99th:               lowerBound,
    76  		UpperBound99th:               upperBound,
    77  		NominalStartPosition:         (lowerBound + upperBound) / 2,
    78  	}
    79  }