• Home
  • 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.clearForTesting();
86             for (JobStatus job : jobList) {
87                 sJobStore.addForTesting(job);
88             }
89 
90             final long startTime = SystemClock.elapsedRealtimeNanos();
91             sJobStore.writeStatusToDiskForTesting();
92             final long endTime = SystemClock.elapsedRealtimeNanos();
93             elapsedTimeNs = endTime - startTime;
94         }
95     }
96 
97     @Test
testPersistedJobWriting_fewJobs()98     public void testPersistedJobWriting_fewJobs() {
99         runPersistedJobWriting(sFewJobs);
100     }
101 
102     @Test
testPersistedJobWriting_manyJobs()103     public void testPersistedJobWriting_manyJobs() {
104         runPersistedJobWriting(sManyJobs);
105     }
106 
runPersistedJobReading(List<JobStatus> jobList, boolean rtcIsGood)107     private void runPersistedJobReading(List<JobStatus> jobList, boolean rtcIsGood) {
108         final ManualBenchmarkState benchmarkState = mPerfManualStatusReporter.getBenchmarkState();
109 
110         long elapsedTimeNs = 0;
111         while (benchmarkState.keepRunning(elapsedTimeNs)) {
112             sJobStore.clearForTesting();
113             for (JobStatus job : jobList) {
114                 sJobStore.addForTesting(job);
115             }
116             sJobStore.writeStatusToDiskForTesting();
117 
118             JobSet jobSet = new JobSet();
119 
120             final long startTime = SystemClock.elapsedRealtimeNanos();
121             sJobStore.readJobMapFromDisk(jobSet, rtcIsGood);
122             final long endTime = SystemClock.elapsedRealtimeNanos();
123             elapsedTimeNs = endTime - startTime;
124         }
125     }
126 
127     @Test
testPersistedJobReading_fewJobs_goodRTC()128     public void testPersistedJobReading_fewJobs_goodRTC() {
129         runPersistedJobReading(sFewJobs, true);
130     }
131 
132     @Test
testPersistedJobReading_fewJobs_badRTC()133     public void testPersistedJobReading_fewJobs_badRTC() {
134         runPersistedJobReading(sFewJobs, false);
135     }
136 
137     @Test
testPersistedJobReading_manyJobs_goodRTC()138     public void testPersistedJobReading_manyJobs_goodRTC() {
139         runPersistedJobReading(sManyJobs, true);
140     }
141 
142     @Test
testPersistedJobReading_manyJobs_badRTC()143     public void testPersistedJobReading_manyJobs_badRTC() {
144         runPersistedJobReading(sManyJobs, false);
145     }
146 
createJobStatus(String testTag, int jobId)147     private static JobStatus createJobStatus(String testTag, int jobId) {
148         JobInfo jobInfo = new JobInfo.Builder(jobId,
149                 new ComponentName(sContext, "JobStorePerfTestJobService"))
150                 .setPersisted(true)
151                 .build();
152         return JobStatus.createFromJobInfo(
153                 jobInfo, CALLING_UID, SOURCE_PACKAGE, SOURCE_USER_ID, testTag);
154     }
155 }
156