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 21 import com.google.common.collect.Sets; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.Set; 26 27 /** Calculate the retry statistics and metrics based on attempts comparison. */ 28 final class RetryStatsHelper { 29 30 private List<List<TestRunResult>> mResults = new ArrayList<>(); 31 private RetryStatistics mStats = new RetryStatistics(); 32 33 /** Add the results from the latest run to be tracked for statistics purpose. */ addResultsFromRun(List<TestRunResult> mLatestResults)34 public void addResultsFromRun(List<TestRunResult> mLatestResults) { 35 addResultsFromRun(mLatestResults, 0L, 0); 36 } 37 38 /** Add the results from the latest run to be tracked for statistics purpose. */ addResultsFromRun(List<TestRunResult> mLatestResults, long timeForIsolation, int attempt)39 public void addResultsFromRun(List<TestRunResult> mLatestResults, long timeForIsolation, int attempt) { 40 if (!mResults.isEmpty()) { 41 updateSuccess(mResults.get(mResults.size() - 1), mLatestResults); 42 } 43 if (timeForIsolation != 0L) { 44 mStats.mAttemptIsolationCost.put(attempt, timeForIsolation); 45 } 46 mResults.add(mLatestResults); 47 } 48 49 /** 50 * Calculate the retry statistics based on currently known results and return the associated 51 * {@link RetryStatistics} to represent the results. 52 */ calculateStatistics()53 public RetryStatistics calculateStatistics() { 54 if (!mResults.isEmpty()) { 55 List<TestRunResult> attemptResults = mResults.get(mResults.size() - 1); 56 Set<TestDescription> attemptFailures = 57 BaseRetryDecision.getFailedTestCases(attemptResults).keySet(); 58 mStats.mRetryFailure = attemptFailures.size(); 59 } 60 return mStats; 61 } 62 updateSuccess( List<TestRunResult> previousResults, List<TestRunResult> latestResults)63 private void updateSuccess( 64 List<TestRunResult> previousResults, List<TestRunResult> latestResults) { 65 Set<TestDescription> diff = 66 Sets.difference( 67 BaseRetryDecision.getFailedTestCases(previousResults).keySet(), 68 BaseRetryDecision.getFailedTestCases(latestResults).keySet()); 69 mStats.mRetrySuccess += diff.size(); 70 } 71 } 72