blob: bb952f3ea47952ce85a7d37bad098b8987b177c4 (
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
|
/*
* Copyright (C) 2022 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.helpers;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Map;
/**
* To run, use atest CollectorsHelperAospTest:com.android.helpers.JSScriptEngineLatencyHelperTest
*/
public class JSScriptEngineLatencyHelperTest {
@Mock private JSScriptEngineLatencyHelper.MetricsEventStreamReader mMetricsEventStreamReader;
@Mock private Clock mClock;
private JSScriptEngineLatencyHelper mJSScriptEngineLatencyHelper;
private Instant mInstant = Clock.systemUTC().instant();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mClock.getZone()).thenReturn(ZoneId.of("UTC")); // Used by DateTimeFormatter.
when(mClock.instant()).thenReturn(mInstant);
mJSScriptEngineLatencyHelper =
new JSScriptEngineLatencyHelper(() -> mMetricsEventStreamReader, mClock);
mJSScriptEngineLatencyHelper.startCollecting();
}
@Test
public void testInitTimeInLogcat() throws IOException {
String logcatOutput =
"06-13 18:09:24.022 20765 D JSScriptEngine: (SANDBOX_INIT_TIME: 102)\n"
+ "06-29 02:47:32.030 31075 D JSScriptEngine: (ISOLATE_CREATE_TIME: 1)\n"
+ "06-13 18:09:24.058 20765 D JSScriptEngine: (JAVA_EXECUTION_TIME: 43)\n"
+ "06-13 18:09:24.058 31075 D JSScriptEngine: (WEBVIEW_EXECUTION_TIME: 23)\n"
+ "06-13 18:09:24.129 20765 D JSScriptEngine: (SANDBOX_INIT_TIME: 45)\n"
+ "06-13 18:09:24.130 31075 D JSScriptEngine: (ISOLATE_CREATE_TIME: 1)\n"
+ "06-13 18:09:24.152 20765 D JSScriptEngine: (JAVA_EXECUTION_TIME: 19)\n"
+ "06-29 02:47:31.824 31075 D JSScriptEngine: (WEBVIEW_EXECUTION_TIME: 29)";
when(mMetricsEventStreamReader.getMetricsEvents(mInstant))
.thenReturn(new ByteArrayInputStream(logcatOutput.getBytes()));
Map<String, Long> actual = mJSScriptEngineLatencyHelper.getMetrics();
assertThat(actual.get(JSScriptEngineLatencyHelper.SANDBOX_INIT_TIME_AVG))
.isEqualTo((102 + 45) / 2);
assertThat(actual.get(JSScriptEngineLatencyHelper.ISOLATE_CREATE_TIME_AVG))
.isEqualTo((1 + 1) / 2);
assertThat(actual.get(JSScriptEngineLatencyHelper.JAVA_EXECUTION_TIME_AVG))
.isEqualTo((43 + 19) / 2);
assertThat(actual.get(JSScriptEngineLatencyHelper.WEBVIEW_EXECUTION_TIME_AVG))
.isEqualTo((23 + 29) / 2);
assertThat(actual.get(JSScriptEngineLatencyHelper.NUM_ITERATIONS)).isEqualTo(2);
}
@Test
public void testWithEmptyLogcat() throws IOException {
when(mMetricsEventStreamReader.getMetricsEvents(mInstant))
.thenReturn(new ByteArrayInputStream("".getBytes()));
Map<String, Long> actual = mJSScriptEngineLatencyHelper.getMetrics();
for (Long val : actual.values()) {
assertThat(val).isEqualTo(0);
}
}
@Test
public void testInputStreamThrowsException() throws IOException {
when(mMetricsEventStreamReader.getMetricsEvents(mInstant)).thenThrow(new IOException());
Map<String, Long> actual = mJSScriptEngineLatencyHelper.getMetrics();
for (Long val : actual.values()) {
assertThat(val).isEqualTo(0);
}
}
}
|