• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.frameworks.perftests.job;
17 
18 
19 import android.app.job.JobInfo;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.os.SystemClock;
23 import android.perftests.utils.ManualBenchmarkState;
24 import android.perftests.utils.PerfManualStatusReporter;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.filters.LargeTest;
28 import androidx.test.runner.AndroidJUnit4;
29 
30 import com.android.server.job.JobStore;
31 import com.android.server.job.JobStore.JobSet;
32 import com.android.server.job.controllers.JobStatus;
33 
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 
40 import java.io.File;
41 import java.util.ArrayList;
42 import java.util.List;
43 
44 @RunWith(AndroidJUnit4.class)
45 @LargeTest
46 public class JobStorePerfTests {
47     private static final String SOURCE_PACKAGE = "com.android.frameworks.perftests.job";
48     private static final int SOURCE_USER_ID = 0;
49     private static final int CALLING_UID = 10079;
50 
51     private static Context sContext;
52     private static File sTestDir;
53     private static JobStore sJobStore;
54 
55     private static List<JobStatus> sFewJobs = new ArrayList<>();
56     private static List<JobStatus> sManyJobs = new ArrayList<>();
57 
58     @Rule
59     public PerfManualStatusReporter mPerfManualStatusReporter = new PerfManualStatusReporter();
60 
61     @BeforeClass
setUpOnce()62     public static void setUpOnce() {
63         sContext = InstrumentationRegistry.getTargetContext();
64         sTestDir = new File(sContext.getFilesDir(), "JobStorePerfTests");
65         sJobStore = JobStore.initAndGetForTesting(sContext, sTestDir);
66 
67         for (int i = 0; i < 50; i++) {
68             sFewJobs.add(createJobStatus("fewJobs", i));
69         }
70         for (int i = 0; i < 500; i++) {
71             sManyJobs.add(createJobStatus("manyJobs", i));
72         }
73     }
74 
75     @AfterClass
tearDownOnce()76     public static void tearDownOnce() {
77         sTestDir.deleteOnExit();
78     }
79 
runPersistedJobWriting(List<JobStatus> jobList)80     private void runPersistedJobWriting(List<JobStatus> jobList) {
81         final ManualBenchmarkState benchmarkState = mPerfManualStatusReporter.getBenchmarkState();
82 
83         long elapsedTimeNs = 0;
84         while (benchmarkState.keepRunning(elapsedTimeNs)) {
85             sJobStore.clear();
86             for (JobStatus job : jobList) {
87                 sJobStore.add(job);
88             }
89             sJobStore.waitForWriteToCompleteForTesting(10_000);
90 
91             final long startTime = SystemClock.elapsedRealtimeNanos();
92             sJobStore.writeStatusToDiskForTesting();
93             final long endTime = SystemClock.elapsedRealtimeNanos();
94             elapsedTimeNs = endTime - startTime;
95         }
96     }
97 
98     @Test
testPersistedJobWriting_fewJobs()99     public void testPersistedJobWriting_fewJobs() {
100         runPersistedJobWriting(sFewJobs);
101     }
102 
103     @Test
testPersistedJobWriting_manyJobs()104     public void testPersistedJobWriting_manyJobs() {
105         runPersistedJobWriting(sManyJobs);
106     }
107 
runPersistedJobReading(List<JobStatus> jobList, boolean rtcIsGood)108     private void runPersistedJobReading(List<JobStatus> jobList, boolean rtcIsGood) {
109         final ManualBenchmarkState benchmarkState = mPerfManualStatusReporter.getBenchmarkState();
110 
111         long elapsedTimeNs = 0;
112         while (benchmarkState.keepRunning(elapsedTimeNs)) {
113             sJobStore.clear();
114             for (JobStatus job : jobList) {
115                 sJobStore.add(job);
116             }
117             sJobStore.waitForWriteToCompleteForTesting(10_000);
118 
119             JobSet jobSet = new JobSet();
120 
121             final long startTime = SystemClock.elapsedRealtimeNanos();
122             sJobStore.readJobMapFromDisk(jobSet, rtcIsGood);
123             final long endTime = SystemClock.elapsedRealtimeNanos();
124             elapsedTimeNs = endTime - startTime;
125         }
126     }
127 
128     @Test
testPersistedJobReading_fewJobs_goodRTC()129     public void testPersistedJobReading_fewJobs_goodRTC() {
130         runPersistedJobReading(sFewJobs, true);
131     }
132 
133     @Test
testPersistedJobReading_fewJobs_badRTC()134     public void testPersistedJobReading_fewJobs_badRTC() {
135         runPersistedJobReading(sFewJobs, false);
136     }
137 
138     @Test
testPersistedJobReading_manyJobs_goodRTC()139     public void testPersistedJobReading_manyJobs_goodRTC() {
140         runPersistedJobReading(sManyJobs, true);
141     }
142 
143     @Test
testPersistedJobReading_manyJobs_badRTC()144     public void testPersistedJobReading_manyJobs_badRTC() {
145         runPersistedJobReading(sManyJobs, false);
146     }
147 
createJobStatus(String testTag, int jobId)148     private static JobStatus createJobStatus(String testTag, int jobId) {
149         JobInfo jobInfo = new JobInfo.Builder(jobId,
150                 new ComponentName(sContext, "JobStorePerfTestJobService"))
151                 .setPersisted(true)
152                 .build();
153         return JobStatus.createFromJobInfo(
154                 jobInfo, CALLING_UID, SOURCE_PACKAGE, SOURCE_USER_ID, testTag);
155     }
156 }
157