Home | History | Annotate | Download | only in loadtest
      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.statsd.loadtest;
     17 
     18 import com.android.internal.os.StatsdConfigProto.TimeUnit;
     19 
     20 import android.annotation.Nullable;
     21 import android.app.AlarmManager;
     22 import android.app.PendingIntent;
     23 import android.content.BroadcastReceiver;
     24 import android.content.Context;
     25 import android.content.Intent;
     26 import android.os.SystemClock;
     27 import android.util.Log;
     28 
     29 import java.util.HashSet;
     30 import java.util.Set;
     31 
     32 /** Prints some information about the device via Dumpsys in order to evaluate health metrics. */
     33 public class PerfData extends PerfDataRecorder {
     34 
     35     private static final String TAG = "loadtest.PerfData";
     36 
     37     /** Polling period for performance snapshots like memory. */
     38     private static final long POLLING_PERIOD_MILLIS = 1 * 60 * 1000;
     39 
     40     public final static class PerfAlarmReceiver extends BroadcastReceiver {
     41         @Override
     42         public void onReceive(Context context, Intent intent) {
     43             Intent activityIntent = new Intent(context, LoadtestActivity.class);
     44             activityIntent.putExtra(LoadtestActivity.TYPE, LoadtestActivity.PERF_ALARM);
     45             context.startActivity(activityIntent);
     46          }
     47     }
     48 
     49     private AlarmManager mAlarmMgr;
     50 
     51     /** Used to periodically poll some dumpsys data. */
     52     private PendingIntent mPendingIntent;
     53 
     54     private final Set<PerfDataRecorder> mRecorders;
     55 
     56     public PerfData(LoadtestActivity loadtestActivity, boolean placebo, int replication,
     57         TimeUnit bucket, long periodSecs,  int burst, boolean includeCountMetric,
     58         boolean includeDurationMetric, boolean includeEventMetric,  boolean includeValueMetric,
     59         boolean includeGaugeMetric) {
     60       super(placebo, replication, bucket, periodSecs, burst, includeCountMetric,
     61           includeDurationMetric, includeEventMetric, includeValueMetric, includeGaugeMetric);
     62         mRecorders = new HashSet();
     63         mRecorders.add(new BatteryDataRecorder(placebo, replication, bucket, periodSecs, burst,
     64                 includeCountMetric, includeDurationMetric, includeEventMetric, includeValueMetric,
     65                 includeGaugeMetric));
     66         mRecorders.add(new MemoryDataRecorder(placebo, replication, bucket, periodSecs, burst,
     67                 includeCountMetric, includeDurationMetric, includeEventMetric, includeValueMetric,
     68                 includeGaugeMetric));
     69         mRecorders.add(new StatsdStatsRecorder(loadtestActivity, placebo, replication, bucket,
     70                 periodSecs, burst, includeCountMetric, includeDurationMetric, includeEventMetric,
     71                 includeValueMetric, includeGaugeMetric));
     72         mRecorders.add(new ValidationRecorder(loadtestActivity, placebo, replication, bucket,
     73                 periodSecs, burst, includeCountMetric, includeDurationMetric, includeEventMetric,
     74                 includeValueMetric, includeGaugeMetric));
     75         mAlarmMgr = (AlarmManager) loadtestActivity.getSystemService(Context.ALARM_SERVICE);
     76     }
     77 
     78     public void onDestroy() {
     79         if (mPendingIntent != null) {
     80             mAlarmMgr.cancel(mPendingIntent);
     81             mPendingIntent = null;
     82         }
     83     }
     84 
     85     @Override
     86     public void startRecording(Context context) {
     87         Intent intent = new Intent(context, PerfAlarmReceiver.class);
     88         intent.putExtra(LoadtestActivity.TYPE, LoadtestActivity.PERF_ALARM);
     89         mPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
     90         mAlarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, -1 /* now */,
     91             POLLING_PERIOD_MILLIS, mPendingIntent);
     92 
     93         for (PerfDataRecorder recorder : mRecorders) {
     94             recorder.startRecording(context);
     95         }
     96     }
     97 
     98     @Override
     99     public void onAlarm(Context context) {
    100         for (PerfDataRecorder recorder : mRecorders) {
    101             recorder.onAlarm(context);
    102         }
    103     }
    104 
    105     @Override
    106     public void stopRecording(Context context) {
    107         if (mPendingIntent != null) {
    108             mAlarmMgr.cancel(mPendingIntent);
    109             mPendingIntent = null;
    110         }
    111 
    112         for (PerfDataRecorder recorder : mRecorders) {
    113             recorder.stopRecording(context);
    114         }
    115     }
    116 }
    117