• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.retry;
17 
18 import com.android.tradefed.result.TestDescription;
19 import com.android.tradefed.result.TestRunResult;
20 import com.android.tradefed.result.TestStatus;
21 
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.HashSet;
25 import java.util.Set;
26 
27 /** Calculate the retry statistics and metrics based on attempts comparison. */
28 final class RetryStatsHelper {
29 
30     private RetryStatistics mStats = new RetryStatistics();
31     private Set<TestDescription> mPassedTestCases = new HashSet<>();
32     private Set<TestDescription> mFailedTestCases = new HashSet<>();
33 
34     /** Add the results from the latest run to be tracked for statistics purpose. */
addResultsFromRun(List<TestRunResult> mLatestResults)35     public void addResultsFromRun(List<TestRunResult> mLatestResults) {
36         addResultsFromRun(mLatestResults, 0L, 0);
37     }
38 
39     /** Add the results from the latest run to be tracked for statistics purpose. */
addResultsFromRun( List<TestRunResult> mLatestResults, long timeForIsolation, int attempt)40     public void addResultsFromRun(
41             List<TestRunResult> mLatestResults, long timeForIsolation, int attempt) {
42         if (timeForIsolation != 0L) {
43             mStats.mAttemptIsolationCost.put(attempt, timeForIsolation);
44         }
45         for (var runResult : mLatestResults) {
46             if (runResult != null) {
47                 // Track all tests where we failed to clear the failure (so if the test either
48                 // failed or didn't run in subsequent attempts, it's a failed retry.)
49                 var failedStatuses = Arrays.asList(TestStatus.FAILURE, TestStatus.SKIPPED);
50                 mFailedTestCases.addAll(runResult.getTestsInState(failedStatuses));
51                 mFailedTestCases.removeAll(runResult.getPassedTests());
52 
53                 // Only track retries as retrySuccess.
54                 if (attempt > 0) {
55                     mPassedTestCases.addAll(runResult.getPassedTests());
56                 }
57             }
58         }
59     }
60 
61     /**
62      * Calculate the retry statistics based on currently known results and return the associated
63      * {@link RetryStatistics} to represent the results.
64      */
calculateStatistics()65     public RetryStatistics calculateStatistics() {
66         mStats.mRetryFailure = mFailedTestCases.size();
67         mStats.mRetrySuccess = mPassedTestCases.size();
68         return mStats;
69     }
70 
71 }
72