Home | History | Annotate | Download | only in metric
      1 /*
      2  * Copyright (C) 2017 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.device.metric;
     17 
     18 import com.android.tradefed.config.Option;
     19 import com.android.tradefed.log.LogUtil.CLog;
     20 
     21 import java.util.Timer;
     22 import java.util.TimerTask;
     23 
     24 /**
     25  * A {@link IMetricCollector} that allows to run a collection task periodically at a set interval.
     26  */
     27 public abstract class ScheduledDeviceMetricCollector extends BaseDeviceMetricCollector {
     28 
     29     @Option(
     30         name = "fixed-schedule-rate",
     31         description = "Schedule the timetask as a fixed schedule rate"
     32     )
     33     private boolean mFixedScheduleRate = false;
     34 
     35     @Option(
     36         name = "interval",
     37         description = "the interval between two tasks being scheduled",
     38         isTimeVal = true
     39     )
     40     private long mIntervalMs = 60 * 1000l;
     41 
     42     private Timer timer;
     43 
     44     @Override
     45     public final void onTestRunStart(DeviceMetricData runData) {
     46         CLog.d("starting");
     47         onStart(runData);
     48         timer = new Timer();
     49         TimerTask timerTask =
     50                 new TimerTask() {
     51                     @Override
     52                     public void run() {
     53                         try {
     54                             collect(runData);
     55                         } catch (InterruptedException e) {
     56                             timer.cancel();
     57                             Thread.currentThread().interrupt();
     58                             CLog.e("Interrupted exception thrown from task:");
     59                             CLog.e(e);
     60                         }
     61                     }
     62                 };
     63 
     64         if (mFixedScheduleRate) {
     65             timer.scheduleAtFixedRate(timerTask, 0, mIntervalMs);
     66         } else {
     67             timer.schedule(timerTask, 0, mIntervalMs);
     68         }
     69     }
     70 
     71     @Override
     72     public final void onTestRunEnd(DeviceMetricData runData) {
     73         if (timer != null) {
     74             timer.cancel();
     75             timer.purge();
     76         }
     77         onEnd(runData);
     78         CLog.d("finished");
     79     }
     80 
     81     /**
     82      * Task periodically & asynchronously run during the test running.
     83      *
     84      * @param runData the {@link DeviceMetricData} where to put metrics.
     85      * @throws InterruptedException
     86      */
     87     abstract void collect(DeviceMetricData runData) throws InterruptedException;
     88 
     89     /**
     90      * Executed when entering this collector.
     91      *
     92      * @param runData the {@link DeviceMetricData} where to put metrics.
     93      */
     94     void onStart(DeviceMetricData runData) {
     95         // Does nothing.
     96     }
     97 
     98     /**
     99      * Executed when finishing this collector.
    100      *
    101      * @param runData the {@link DeviceMetricData} where to put metrics.
    102      */
    103     void onEnd(DeviceMetricData runData) {
    104         // Does nothing.
    105     }
    106 }
    107