From 3169a3ad3876d42119764a1bf21e482c11dd4228 Mon Sep 17 00:00:00 2001 From: Kimberly Kreider Date: Fri, 20 Dec 2019 15:34:01 +0000 Subject: Revert submission 9940985-qpr1-dev merge Reason for revert: broke tests b/146476630 Bug: 146476630 Reverted Changes: Ib3436a42b2ca8c9614939a30fb548ab39bc23e57 Change-Id: Ic936e0f03b001fff37308bb576376299c5ebef20 Exclude merging into branches that contain last aosp change included in merge. Merged-In: 483ea33b74d7008d909e817c754ba9165ae704b1 --- .../device/metric/LogcatTimingMetricCollector.java | 201 --------------------- 1 file changed, 201 deletions(-) delete mode 100644 test_framework/com/android/tradefed/device/metric/LogcatTimingMetricCollector.java (limited to 'test_framework') diff --git a/test_framework/com/android/tradefed/device/metric/LogcatTimingMetricCollector.java b/test_framework/com/android/tradefed/device/metric/LogcatTimingMetricCollector.java deleted file mode 100644 index 648a3af..0000000 --- a/test_framework/com/android/tradefed/device/metric/LogcatTimingMetricCollector.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package com.android.tradefed.device.metric; - -import com.android.loganalysis.item.GenericTimingItem; -import com.android.loganalysis.parser.TimingsLogParser; -import com.android.tradefed.config.Option; -import com.android.tradefed.config.OptionClass; -import com.android.tradefed.device.DeviceNotAvailableException; -import com.android.tradefed.device.ITestDevice; -import com.android.tradefed.device.LogcatReceiver; -import com.android.tradefed.log.LogUtil.CLog; -import com.android.tradefed.metrics.proto.MetricMeasurement.DataType; -import com.android.tradefed.metrics.proto.MetricMeasurement.Measurements; -import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; -import com.android.tradefed.result.InputStreamSource; -import com.android.tradefed.result.LogDataType; -import com.android.tradefed.result.TestDescription; - -import com.google.common.annotations.VisibleForTesting; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -/** - * A metric collector that collects timing information (e.g. user switch time) from logcat during - * one or multiple repeated tests by using given regex patterns to parse start and end signals of an - * event from logcat lines. - */ -@OptionClass(alias = "timing-metric-collector") -public class LogcatTimingMetricCollector extends BaseDeviceMetricCollector { - - private static final String LOGCAT_NAME_FORMAT = "device_%s_test_logcat"; - // Use logcat -T 'count' to only print a few line before we start and not the full buffer - private static final String LOGCAT_CMD = "logcat *:D -T 150"; - - @Option( - name = "start-pattern", - description = - "Key-value pairs to specify the timing metric start patterns to capture from" - + " logcat. Key: metric name, value: regex pattern of logcat line" - + " indicating the start of the timing metric") - private final Map mStartPatterns = new HashMap<>(); - - @Option( - name = "end-pattern", - description = - "Key-value pairs to specify the timing metric end patterns to capture from" - + " logcat. Key: metric name, value: regex pattern of logcat line" - + " indicating the end of the timing metric") - private final Map mEndPatterns = new HashMap<>(); - - @Option( - name = "logcat-buffer", - description = - "Logcat buffers where the timing metrics are captured. Default buffers will be" - + " used if not specified.") - private final List mLogcatBuffers = new ArrayList<>(); - - private final Map mLogcatReceivers = new HashMap<>(); - private final TimingsLogParser mParser = new TimingsLogParser(); - - @Override - public void onTestRunStart(DeviceMetricData testData) { - // Adding patterns - mParser.clearDurationPatterns(); - for (Map.Entry entry : mStartPatterns.entrySet()) { - String name = entry.getKey(); - if (!mEndPatterns.containsKey(name)) { - CLog.w("Metric %s is missing end pattern, skipping.", name); - continue; - } - Pattern start = Pattern.compile(entry.getValue()); - Pattern end = Pattern.compile(mEndPatterns.get(name)); - CLog.d("Adding metric: %s", name); - mParser.addDurationPatternPair(name, start, end); - } - // Start receiving logcat - String logcatCmd = LOGCAT_CMD; - if (!mLogcatBuffers.isEmpty()) { - logcatCmd += " -b " + String.join(",", mLogcatBuffers); - } - for (ITestDevice device : getDevices()) { - CLog.d( - "Creating logcat receiver on device %s with command %s", - device.getSerialNumber(), logcatCmd); - mLogcatReceivers.put(device, createLogcatReceiver(device, logcatCmd)); - try { - device.executeShellCommand("logcat -c"); - } catch (DeviceNotAvailableException e) { - CLog.e( - "Device not available when clear logcat. Skip logcat collection on %s", - device.getSerialNumber()); - continue; - } - mLogcatReceivers.get(device).start(); - } - } - - @Override - public void onTestRunEnd( - DeviceMetricData testData, final Map currentTestCaseMetrics) { - boolean isMultiDevice = getDevices().size() > 1; - for (ITestDevice device : getDevices()) { - try (InputStreamSource logcatData = mLogcatReceivers.get(device).getLogcatData()) { - Map> metrics = parse(logcatData); - for (Map.Entry> entry : metrics.entrySet()) { - String name = entry.getKey(); - List values = entry.getValue(); - if (isMultiDevice) { - testData.addMetricForDevice(device, name, createMetric(values)); - } else { - testData.addMetric(name, createMetric(values)); - } - CLog.d( - "Metric: %s with value: %s, added to device %s", - name, values, device.getSerialNumber()); - } - testLog( - String.format(LOGCAT_NAME_FORMAT, device.getSerialNumber()), - LogDataType.TEXT, - logcatData); - } - mLogcatReceivers.get(device).stop(); - mLogcatReceivers.get(device).clear(); - } - } - - @Override - public void onTestFail(DeviceMetricData testData, TestDescription test) { - for (ITestDevice device : getDevices()) { - try (InputStreamSource logcatData = mLogcatReceivers.get(device).getLogcatData()) { - testLog( - String.format(LOGCAT_NAME_FORMAT, device.getSerialNumber()), - LogDataType.TEXT, - logcatData); - } - mLogcatReceivers.get(device).stop(); - mLogcatReceivers.get(device).clear(); - } - } - - @VisibleForTesting - Map> parse(InputStreamSource logcatData) { - Map> metrics = new HashMap<>(); - try (InputStream inputStream = logcatData.createInputStream(); - InputStreamReader logcatReader = new InputStreamReader(inputStream); - BufferedReader br = new BufferedReader(logcatReader)) { - List items = mParser.parseGenericTimingItems(br); - for (GenericTimingItem item : items) { - String metricKey = item.getName(); - if (!metrics.containsKey(metricKey)) { - metrics.put(metricKey, new ArrayList<>()); - } - metrics.get(metricKey).add(item.getDuration()); - } - } catch (IOException e) { - CLog.e("Failed to parse timing metrics from logcat %s", e); - } - return metrics; - } - - @VisibleForTesting - LogcatReceiver createLogcatReceiver(ITestDevice device, String logcatCmd) { - return new LogcatReceiver(device, logcatCmd, device.getOptions().getMaxLogcatDataSize(), 0); - } - - private Metric.Builder createMetric(List values) { - // TODO: Fix post processors to handle double values. For now use concatenated string as we - // prefer to use AggregatedPostProcessor - String stringValue = - values.stream() - .map(value -> Double.toString(value)) - .collect(Collectors.joining(",")); - return Metric.newBuilder() - .setType(DataType.RAW) - .setMeasurements(Measurements.newBuilder().setSingleString(stringValue)); - } -} -- cgit v1.2.3