summaryrefslogtreecommitdiff
path: root/tests/src/android/cts/statsd/metric/MetricActivationTests.java
blob: 8e796cf93f6ce55214dd42a2a6a824581d663c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
 * 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 android.cts.statsd.metric;

import static com.google.common.truth.Truth.assertThat;

import android.cts.statsd.atom.DeviceAtomTestCase;

import com.android.internal.os.StatsdConfigProto;
import com.android.internal.os.StatsdConfigProto.ActivationType;
import com.android.internal.os.StatsdConfigProto.AtomMatcher;
import com.android.internal.os.StatsdConfigProto.EventActivation;
import com.android.internal.os.StatsdConfigProto.EventMetric;
import com.android.internal.os.StatsdConfigProto.GaugeMetric;
import com.android.internal.os.StatsdConfigProto.MetricActivation;
import com.android.internal.os.StatsdConfigProto.StatsdConfig;
import com.android.os.AtomsProto.AppBreadcrumbReported;
import com.android.os.AtomsProto.Atom;
import com.android.os.StatsLog.ConfigMetricsReport;
import com.android.os.StatsLog.ConfigMetricsReportList;
import com.android.os.StatsLog.EventMetricData;
import com.android.os.StatsLog.StatsLogReport;
import com.android.tradefed.log.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Test Statsd Metric activations and deactivations
 */
public class MetricActivationTests extends DeviceAtomTestCase {
    private final long metric1Id = 1L;
    private final int metric1MatcherId = 1;

    private final long metric2Id = 2L;
    private final int metric2MatcherId = 2;

    private final long metric3Id = 3L;
    private final int metric3MatcherId = 3;

    private final int act1MatcherId = 10;
    private final int act1CancelMatcherId = -10;

    private final int act2MatcherId = 20;
    private final int act2CancelMatcherId = -20;


    private StatsdConfig.Builder createConfig(final int act1TtlSecs, final int act2TtlSecs) {
        AtomMatcher metric1Matcher =
                MetricsUtils.simpleAtomMatcher(metric1MatcherId, metric1MatcherId);
        AtomMatcher metric2Matcher =
                MetricsUtils.simpleAtomMatcher(metric2MatcherId, metric2MatcherId);
        AtomMatcher metric3Matcher =
                MetricsUtils.simpleAtomMatcher(metric3MatcherId, metric3MatcherId);
        AtomMatcher act1Matcher =
                MetricsUtils.simpleAtomMatcher(act1MatcherId, act1MatcherId);
        AtomMatcher act1CancelMatcher =
                MetricsUtils.simpleAtomMatcher(act1CancelMatcherId, act1CancelMatcherId);
        AtomMatcher act2Matcher =
                MetricsUtils.simpleAtomMatcher(act2MatcherId, act2MatcherId);
        AtomMatcher act2CancelMatcher =
                MetricsUtils.simpleAtomMatcher(act2CancelMatcherId, act2CancelMatcherId);

        EventMetric metric1 = EventMetric.newBuilder()
                .setId(metric1Id)
                .setWhat(metric1MatcherId)
                .build();

        EventMetric metric2 = EventMetric.newBuilder()
                .setId(metric2Id)
                .setWhat(metric2MatcherId)
                .build();

        EventMetric metric3 = EventMetric.newBuilder()
                .setId(metric3Id)
                .setWhat(metric3MatcherId)
                .build();

        EventActivation metric1Act1 =
                MetricsUtils.createEventActivation(act1TtlSecs, act1MatcherId, act1CancelMatcherId)
                    .setActivationType(ActivationType.ACTIVATE_IMMEDIATELY)
                    .build();

        EventActivation metric1Act2 =
                MetricsUtils.createEventActivation(act2TtlSecs, act2MatcherId, act2CancelMatcherId)
                    .setActivationType(ActivationType.ACTIVATE_ON_BOOT)
                    .build();

        EventActivation metric2Act1 =
                MetricsUtils.createEventActivation(act1TtlSecs, act1MatcherId, act1CancelMatcherId)
                    .setActivationType(ActivationType.ACTIVATE_ON_BOOT)
                    .build();

        EventActivation metric2Act2 =
                MetricsUtils.createEventActivation(act2TtlSecs, act2MatcherId, act2CancelMatcherId)
                    .setActivationType(ActivationType.ACTIVATE_IMMEDIATELY)
                    .build();

        MetricActivation metric1Activation = MetricActivation.newBuilder()
                .setMetricId(metric1Id)
                .addEventActivation(metric1Act1)
                .addEventActivation(metric1Act2)
                .build();

        MetricActivation metric2Activation = MetricActivation.newBuilder()
                .setMetricId(metric2Id)
                .addEventActivation(metric2Act1)
                .addEventActivation(metric2Act2)
                .build();

        return createConfigBuilder()
                .addAtomMatcher(metric1Matcher)
                .addAtomMatcher(metric2Matcher)
                .addAtomMatcher(metric3Matcher)
                .addAtomMatcher(act1Matcher)
                .addAtomMatcher(act1CancelMatcher)
                .addAtomMatcher(act2Matcher)
                .addAtomMatcher(act2CancelMatcher)
                .addEventMetric(metric1)
                .addEventMetric(metric2)
                .addEventMetric(metric3)
                .addMetricActivation(metric1Activation)
                .addMetricActivation(metric2Activation);
    }

    /**
     * Metric 1:
     *     Activation 1:
     *         - Ttl: 5 seconds
     *         - Type: IMMEDIATE
     *     Activation 2:
     *         - Ttl: 8 seconds
     *         - Type: ON_BOOT
     *
     * Metric 2:
     *     Activation 1:
     *         - Ttl: 5 seconds
     *         - Type: ON_BOOT
     *     Activation 2:
     *         - Ttl: 8 seconds
     *         - Type: IMMEDIATE
     *
     * Metric 3: No activations; always active
     **/
    public void testCancellation() throws Exception {
        final int act1TtlSecs = 5;
        final int act2TtlSecs = 8;
        uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));

        // Ignored, metric not active.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Trigger cancel for already inactive event activation 1.
        doAppBreadcrumbReported(act1CancelMatcherId);
        Thread.sleep(10L);

        // Trigger event activation 1.
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // First logged event.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Second logged event.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Cancel event activation 1.
        doAppBreadcrumbReported(act1CancelMatcherId);
        Thread.sleep(10L);

        // Ignored, metric not active.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Trigger event activation 1.
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // Trigger event activation 2.
        doAppBreadcrumbReported(act2MatcherId);
        Thread.sleep(10L);

        // Third logged event.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Cancel event activation 2.
        doAppBreadcrumbReported(act2CancelMatcherId);
        Thread.sleep(10L);

        // Fourth logged event.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Expire event activation 1
        Thread.sleep(act1TtlSecs * 1000);

        // Ignored, metric 1 not active. Activation 1 expired and Activation 2 was cancelled.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // Trigger event activation 2.
        doAppBreadcrumbReported(act2MatcherId);
        Thread.sleep(10L);

        // Metric 1 log ignored, Activation 1 expired and Activation 2 needs reboot to activate.
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        // First logged event for Metric 3.
        doAppBreadcrumbReported(metric3MatcherId);
        Thread.sleep(10L);

        ConfigMetricsReportList reportList = getReportList();
        List<ConfigMetricsReport> reports = getSortedConfigMetricsReports(reportList);
        ConfigMetricsReport report = reports.get(0);
        verifyMetrics(report, 4, 0, 1);
    }

    /**
     * Metric 1:
     *     Activation 1:
     *         - Ttl: 100 seconds
     *         - Type: IMMEDIATE
     *     Activation 2:
     *         - Ttl: 200 seconds
     *         - Type: ON_BOOT
     *
     * Metric 2:
     *     Activation 1:
     *         - Ttl: 100 seconds
     *         - Type: ON_BOOT
     *     Activation 2:
     *         - Ttl: 200 seconds
     *         - Type: IMMEDIATE
     *
     * Metric 3: No activations; always active
     **/
    public void testRestart() throws Exception {
        final int act1TtlSecs = 200;
        final int act2TtlSecs = 400;
        uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));

        // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 0 seconds
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // First logged event for Metric 1.
        // Metric 2 event ignored, will activate after boot.
        // First logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 200 seconds
        // Metric 2 Activation 2: 0 seconds
        rebootDeviceAndWaitUntilReady();

        // Second logged event for Metric 1.
        // First logged event for Metric 2.
        // Second logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds
        // Metric 2 Activation 2: 0 seconds
        Thread.sleep(act1TtlSecs * 1000L);

        // Metric 1 event ignored, Activation 1 expired.
        // Metric 2 event ignored, Activation 1 expired.
        // Third logged event for Metric 3.
        logAllMetrics();

        // Trigger Metric 1 Activation 2 and Metric 2 Activation 2.
        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 0 seconds (will activate after boot)
        // Metric 2 Activation 1: 0 seconds
        // Metric 2 Activation 2: 400 seconds
        doAppBreadcrumbReported(act2MatcherId);
        Thread.sleep(10L);

        // Metric 1 event ignored, will activate after boot.
        // Second logged event for Metric 2.
        // Fourth logged event for Metric 3.
        logAllMetrics();

        // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds (will activate after boot)
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 400 seconds
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // Third logged event for Metric 1.
        // Third logged event for Metric 2.
        // Fifth logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 100 seconds
        // Metric 1 Activation 2: 0 seconds (will activate after boot)
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 300 seconds
        Thread.sleep(act1TtlSecs * 1000L / 2);

        // Time remaining:
        // Metric 1 Activation 1: 100 seconds
        // Metric 1 Activation 2: 400 seconds
        // Metric 2 Activation 1: 200 seconds
        // Metric 2 Activation 2: 300 seconds
        rebootDeviceAndWaitUntilReady();

        // Fourth logged event for Metric 1.
        // Fourth logged event for Metric 2.
        // Sixth logged event for Metric 3.
        logAllMetrics();

        // Expire Metric 1 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 300 seconds
        // Metric 2 Activation 1: 100 seconds
        // Metric 2 Activation 2: 200 seconds
        Thread.sleep(act1TtlSecs * 1000L / 2);

        // Fifth logged event for Metric 1.
        // Fifth logged event for Metric 2.
        // Seventh logged event for Metric 3.
        logAllMetrics();

        // Expire all activations.
        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds
        // Metric 2 Activation 2: 0 seconds
        Thread.sleep(act2TtlSecs * 1000L);

        // Metric 1 event ignored.
        // Metric 2 event ignored.
        // Eighth logged event for Metric 3.
        logAllMetrics();

        ConfigMetricsReportList reportList = getReportList();
        List<ConfigMetricsReport> reports = getSortedConfigMetricsReports(reportList);
        assertThat(reports).hasSize(3);

        // Report before restart.
        ConfigMetricsReport report = reports.get(0);
        verifyMetrics(report, 1, 0, 1);

        // Report after first restart.
        report = reports.get(1);
        verifyMetrics(report, 2, 3, 4);

        // Report after second restart.
        report = reports.get(2);
        verifyMetrics(report, 2, 2, 3);
    }

    /**
     * Metric 1:
     *     Activation 1:
     *         - Ttl: 100 seconds
     *         - Type: IMMEDIATE
     *     Activation 2:
     *         - Ttl: 200 seconds
     *         - Type: ON_BOOT
     *
     * Metric 2:
     *     Activation 1:
     *         - Ttl: 100 seconds
     *         - Type: ON_BOOT
     *     Activation 2:
     *         - Ttl: 200 seconds
     *         - Type: IMMEDIATE
     *
     * Metric 3: No activations; always active
     **/
    public void testMultipleActivations() throws Exception {
        final int act1TtlSecs = 200;
        final int act2TtlSecs = 400;
        uploadConfig(createConfig(act1TtlSecs, act2TtlSecs));

        // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 0 seconds
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // First logged event for Metric 1.
        // Metric 2 event ignored, will activate after boot.
        // First logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 100 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 0 seconds
        Thread.sleep(act1TtlSecs * 1000L / 2);

        // Second logged event for Metric 1.
        // Metric 2 event ignored, will activate after boot.
        // Second logged event for Metric 3.
        logAllMetrics();

        // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds (will activate after boot)
        // Metric 2 Activation 2: 0 seconds
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // Third logged event for Metric 1.
        // Metric 2 event ignored, will activate after boot.
        // Third logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 200 seconds
        // Metric 2 Activation 2: 0 seconds
        rebootDeviceAndWaitUntilReady();

        // Fourth logged event for Metric 1.
        // First logged event for Metric 2.
        // Fourth logged event for Metric 3.
        logAllMetrics();

        // Trigger Metric 1 Activation 1 and Metric 2 Activation 1.
        // Time remaining:
        // Metric 1 Activation 1: 200 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 200 seconds
        // Metric 2 Activation 2: 0 seconds
        doAppBreadcrumbReported(act1MatcherId);
        Thread.sleep(10L);

        // Fifth logged event for Metric 1.
        // Second logged event for Metric 2.
        // Fifth logged event for Metric 3.
        logAllMetrics();

        // Expire all activations.
        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds
        // Metric 2 Activation 2: 0 seconds
        Thread.sleep(act1TtlSecs * 1000L);

        // Metric 1 event ignored.
        // Metric 2 event ignored.
        // Sixth logged event for Metric 3.
        logAllMetrics();

        // Time remaining:
        // Metric 1 Activation 1: 0 seconds
        // Metric 1 Activation 2: 0 seconds
        // Metric 2 Activation 1: 0 seconds
        // Metric 2 Activation 2: 0 seconds
        rebootDeviceAndWaitUntilReady();
        Thread.sleep(3_000L);

        // Metric 1 event ignored.
        // Metric 2 event ignored.
        // Seventh logged event for Metric 3.
        logAllMetrics();

        ConfigMetricsReportList reportList = getReportList();
        List<ConfigMetricsReport> reports = getSortedConfigMetricsReports(reportList);
        assertThat(reports).hasSize(3);

        // Report before restart.
        ConfigMetricsReport report = reports.get(0);
        verifyMetrics(report, 3, 0, 3);

        // Report after first restart.
        report = reports.get(1);
        verifyMetrics(report, 2, 2, 3);

        // Report after second restart.
        report = reports.get(2);
        verifyMetrics(report, 0, 0, 1);
    }

    private void logAllMetrics() throws Exception {
        doAppBreadcrumbReported(metric1MatcherId);
        Thread.sleep(10L);

        doAppBreadcrumbReported(metric2MatcherId);
        Thread.sleep(10L);

        doAppBreadcrumbReported(metric3MatcherId);
        Thread.sleep(10L);
    }

    private void verifyMetrics(ConfigMetricsReport report, int metric1Count, int metric2Count,
            int metric3Count) throws Exception {
        assertThat(report.getMetricsCount()).isEqualTo(3);

        verifyMetric(
                report.getMetrics(0),   // StatsLogReport
                1,                      // Metric Id
                1,                      // Metric what atom matcher label
                metric1Count            // Data count
        );
        verifyMetric(
                report.getMetrics(1),   // StatsLogReport
                2,                      // Metric Id
                2,                      // Metric what atom matcher label
                metric2Count            // Data count
        );
        verifyMetric(
                report.getMetrics(2),   // StatsLogReport
                3,                      // Metric Id
                3,                      // Metric what atom matcher label
                metric3Count            // Data count
        );
    }

    private void verifyMetric(StatsLogReport metricReport, long metricId, int metricMatcherLabel,
            int dataCount) {
        LogUtil.CLog.d("Got the following event metric data: " + metricReport.toString());
        assertThat(metricReport.getMetricId()).isEqualTo(metricId);
        assertThat(metricReport.hasEventMetrics()).isEqualTo(dataCount > 0);

        StatsLogReport.EventMetricDataWrapper eventData = metricReport.getEventMetrics();
        List<EventMetricData> eventMetricDataList = new ArrayList<>();
        for (EventMetricData eventMetricData : eventData.getDataList()) {
            eventMetricDataList.addAll(backfillAggregatedAtomsInEventMetric(eventMetricData));
        }
        assertThat(eventMetricDataList).hasSize(dataCount);
        for (EventMetricData eventMetricData : eventMetricDataList) {
            AppBreadcrumbReported atom = eventMetricData.getAtom().getAppBreadcrumbReported();
            assertThat(atom.getLabel()).isEqualTo(metricMatcherLabel);
        }
    }
}