Skip to content

Commit

Permalink
Add new attempt_count field to BES.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 381590740
  • Loading branch information
Googler authored and copybara-github committed Jun 26, 2021
1 parent 68777a2 commit 766cd0e
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,11 @@ message TestSummary {
// Value of runs_per_test for the test.
int32 run_count = 10;

// Number of attempts.
// If there are a different number of attempts per shard, the highest attempt
// count across all shards for each run is used.
int32 attempt_count = 15;

// Number of shards.
int32 shard_count = 11;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ private void incrementalAnalyze(TestResult result) {
Preconditions.checkNotNull(target, "The existing TestSummary must be associated with a target");
TestParams testParams = target.getProvider(TestProvider.class).getTestParams();

int shardNumber = result.getShardNum();
summary.addShardAttempts(shardNumber, result.getData().getTestTimesCount());

if (!testParams.runsDetectsFlakes()) {
status = aggregateStatus(status, result.getData().getStatus());
} else {
int shardNumber = result.getShardNum();
int runsPerTestForLabel = testParams.getRuns();
List<BlazeTestStatus> singleShardStatuses =
summary.addShardStatus(shardNumber, result.getData().getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -315,6 +316,13 @@ public ImmutableList<BlazeTestStatus> addShardStatus(int shardNumber, BlazeTestS
return ImmutableList.copyOf(statuses);
}

/** Records new attempts for the given shard of the target. */
public Builder addShardAttempts(int shardNumber, int newAtttempts) {
checkMutation();
summary.shardAttempts[shardNumber] += newAtttempts;
return this;
}

/**
* Returns the created TestSummary object.
* Any actions following a build() will create another copy of the same values.
Expand Down Expand Up @@ -360,6 +368,7 @@ private void makeSummaryImmutable() {
private BuildConfiguration configuration;
private BlazeTestStatus status;
private boolean skipped;
private final int[] shardAttempts;
private int numCached;
private int numLocalActionCached;
private boolean actionRan;
Expand All @@ -383,9 +392,9 @@ private void makeSummaryImmutable() {
private TestSummary(ConfiguredTarget target) {
this.target = target;
TestParams testParams = getTestParams();
shardRunStatuses =
createAndInitialize(
testParams.runsDetectsFlakes() ? Math.max(testParams.getShards(), 1) : 0);
int sz = Math.max(testParams.getShards(), 1);
shardAttempts = new int[sz];
shardRunStatuses = createAndInitialize(testParams.runsDetectsFlakes() ? sz : 0);
}

private static ImmutableList<ArrayList<BlazeTestStatus>> createAndInitialize(int sz) {
Expand Down Expand Up @@ -540,6 +549,10 @@ public List<Long> getTestTimes() {
return testTimes;
}

public int getNumAttempts() {
return Arrays.stream(this.shardAttempts).max().getAsInt();
}

public int getNumCached() {
return numCached;
}
Expand Down Expand Up @@ -610,6 +623,7 @@ public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext convert
.setOverallStatus(BuildEventStreamerUtils.bepStatus(status))
.setTotalNumCached(getNumCached())
.setTotalRunCount(totalRuns())
.setAttemptCount(getNumAttempts())
.setRunCount(testParams.getRuns())
.setShardCount(testParams.getShards())
.setFirstStartTime(Timestamps.fromMillis(firstStartTimeMillis))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,80 @@ public void timingAggregation() {
assertThat(summary.getTotalRunDurationMillis()).isEqualTo(11);
}

@Test
public void attemptCount_agggregatesSingleShardSingleAttempt() {
when(mockParams.runsDetectsFlakes()).thenReturn(true);
TestResultAggregator underTest = createAggregatorWithTestRuns(1);

underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(1L, 2L)),
/*shardNum=*/ 0));

assertThat(underTest.aggregateAndReportSummary(false).getNumAttempts()).isEqualTo(2);
}

@Test
public void attemptCount_agggregatesSingleShardMultipleAttempts() {
when(mockParams.runsDetectsFlakes()).thenReturn(true);
TestResultAggregator underTest = createAggregatorWithTestRuns(2);

underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(1L, 2L)),
/*shardNum=*/ 0));
underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(3L, 4L)),
/*shardNum=*/ 0));

assertThat(underTest.aggregateAndReportSummary(false).getNumAttempts()).isEqualTo(4);
}

@Test
public void attemptCount_agggregatesMultipleShardsMultipleAttempts() {
when(mockParams.runsDetectsFlakes()).thenReturn(true);
when(mockParams.getShards()).thenReturn(2);
TestResultAggregator underTest = createAggregatorWithTestRuns(3);

underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(1L, 2L, 3L)),
/*shardNum=*/ 0));
underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(3L, 4L)),
/*shardNum=*/ 1));
underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(3L, 4L)),
/*shardNum=*/ 1));

assertThat(underTest.aggregateAndReportSummary(false).getNumAttempts()).isEqualTo(4);
}

@Test
public void attemptCount_agggregatesMultipleShardsSingleShardHasMostAttempts() {
when(mockParams.runsDetectsFlakes()).thenReturn(true);
when(mockParams.getShards()).thenReturn(2);
TestResultAggregator underTest = createAggregatorWithTestRuns(3);

underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(1L, 2L, 3L, 4L, 5L)),
/*shardNum=*/ 0));
underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(3L, 4L)),
/*shardNum=*/ 1));
underTest.testEvent(
shardedTestResult(
TestResultData.newBuilder().addAllTestTimes(ImmutableList.of(3L, 4L)),
/*shardNum=*/ 1));

assertThat(underTest.aggregateAndReportSummary(false).getNumAttempts()).isEqualTo(5);
}

@Test
public void cancelConcurrentTests_cancellationAfterPassIgnored() {
when(mockParams.runsDetectsFlakes()).thenReturn(true);
Expand Down Expand Up @@ -180,4 +254,11 @@ private static TestResult testResult(TestResultData.Builder data, boolean locall
when(mockTestAction.getTestOutputsMapping(any(), any())).thenReturn(ImmutableList.of());
return new TestResult(mockTestAction, data.build(), locallyCached, /*systemFailure=*/ null);
}

private static TestResult shardedTestResult(TestResultData.Builder data, int shardNum) {
TestRunnerAction mockTestAction = mock(TestRunnerAction.class);
when(mockTestAction.getTestOutputsMapping(any(), any())).thenReturn(ImmutableList.of());
when(mockTestAction.getShardNum()).thenReturn(shardNum);
return new TestResult(mockTestAction, data.build(), /*cached=*/ false, /*systemFailure=*/ null);
}
}

0 comments on commit 766cd0e

Please sign in to comment.