1 /*
2  * Copyright (C) 2012 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 
17 package android.filesystem.cts;
18 
19 import static androidx.test.InstrumentationRegistry.getContext;
20 import static androidx.test.InstrumentationRegistry.getInstrumentation;
21 
22 import android.os.Environment;
23 import android.mediapc.cts.common.Utils;
24 import android.mediapc.cts.common.PerformanceClassEvaluator;
25 
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import com.android.compatibility.common.util.CddTest;
29 import com.android.compatibility.common.util.DeviceReportLog;
30 
31 import static org.junit.Assert.assertTrue;
32 
33 import org.junit.Before;
34 import org.junit.After;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.TestName;
38 import org.junit.runner.RunWith;
39 
40 @RunWith(AndroidJUnit4.class)
41 public class RandomRWTest {
42     private static final String DIR_RANDOM_WR = "RANDOM_WR";
43     private static final String DIR_RANDOM_RD = "RANDOM_RD";
44     private static final String REPORT_LOG_NAME = "CtsFileSystemTestCases";
45 
46     @Rule
47     public final TestName mTestName = new TestName();
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         CarTestUtil.getInstance().setUp();
52     }
53 
54     @After
tearDown()55     public void tearDown() throws Exception {
56         CarTestUtil.getInstance().tearDown();
57         FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_WR);
58         FileUtil.removeFileOrDir(getContext(), DIR_RANDOM_RD);
59     }
60 
61     @CddTest(requirements = {"8.2/H-1-4"})
62     @Test
testRandomRead()63     public void testRandomRead() throws Exception {
64         final int READ_BUFFER_SIZE = 4 * 1024;
65         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), READ_BUFFER_SIZE);
66         if (fileSize == 0) { // not enough space, give up
67             return;
68         }
69         FileActivity.startFileActivity(getContext());
70         String streamName = "test_random_read";
71         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
72         double mbps = FileUtil.doRandomReadTest(getContext(), DIR_RANDOM_RD, report, fileSize,
73                 READ_BUFFER_SIZE);
74         report.submit(getInstrumentation());
75 
76         PerformanceClassEvaluator pce = new PerformanceClassEvaluator(this.mTestName);
77         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_1_4 = pce.addR8_2__H_1_4();
78         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_2_4 = pce.addR8_2__H_2_4();
79         r8_2__H_1_4.setFilesystemIoRate(mbps);
80         r8_2__H_2_4.setFilesystemIoRate(mbps);
81 
82         pce.submitAndCheck();
83     }
84 
85     // It is taking too long in some device, and thus cannot run multiple times
86     @CddTest(requirements = {"8.2/H-1-2"})
87     @Test
testRandomUpdate()88     public void testRandomUpdate() throws Exception {
89         final int WRITE_BUFFER_SIZE = 4 * 1024;
90         final long usableSpace = Environment.getDataDirectory().getUsableSpace();
91         long fileSize = 256 * 1024 * 1024;
92         while (usableSpace < fileSize) {
93             fileSize = fileSize / 2;
94         }
95         FileActivity.startFileActivity(getContext());
96         String streamName = "test_random_update";
97         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
98         double mbps = -1;
99         // this is in-fact true
100         if (fileSize > FileUtil.BUFFER_SIZE) {
101             mbps = FileUtil.doRandomWriteTest(getContext(), DIR_RANDOM_WR, report, fileSize,
102                 WRITE_BUFFER_SIZE);
103         }
104         report.submit(getInstrumentation());
105 
106         PerformanceClassEvaluator pce = new PerformanceClassEvaluator(this.mTestName);
107         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_1_2 = pce.addR8_2__H_1_2();
108         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_2_2 = pce.addR8_2__H_2_2();
109         r8_2__H_1_2.setFilesystemIoRate(mbps);
110         r8_2__H_2_2.setFilesystemIoRate(mbps);
111 
112         pce.submitAndCheck();
113     }
114 }
115