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 android.util.Log;
20 import android.mediapc.cts.common.Utils;
21 import android.mediapc.cts.common.PerformanceClassEvaluator;
22 
23 import static androidx.test.InstrumentationRegistry.getContext;
24 import static androidx.test.InstrumentationRegistry.getInstrumentation;
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 import com.android.compatibility.common.util.MeasureRun;
31 import com.android.compatibility.common.util.MeasureTime;
32 import com.android.compatibility.common.util.ResultType;
33 import com.android.compatibility.common.util.ResultUnit;
34 import com.android.compatibility.common.util.Stat;
35 
36 import static org.junit.Assert.assertTrue;
37 
38 import org.junit.Before;
39 import org.junit.After;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.rules.TestName;
43 import org.junit.runner.RunWith;
44 
45 import java.io.File;
46 import java.io.FileInputStream;
47 import java.io.IOException;
48 
49 @RunWith(AndroidJUnit4.class)
50 public class SequentialRWTest {
51     private static final String TAG = "SequentialRWTest";
52 
53     private static final String DIR_SEQ_WR = "SEQ_WR";
54     private static final String DIR_SEQ_UPDATE = "SEQ_UPDATE";
55     private static final String DIR_SEQ_RD = "SEQ_RD";
56     private static final String REPORT_LOG_NAME = "CtsFileSystemTestCases";
57     private static final int BUFFER_SIZE = 100 * 1024 * 1024;
58 
59     @Rule
60     public final TestName mTestName = new TestName();
61 
62     @Before
setUp()63     public void setUp() throws Exception {
64         CarTestUtil.getInstance().setUp();
65     }
66 
67     @After
tearDown()68     public void tearDown() throws Exception {
69         CarTestUtil.getInstance().tearDown();
70         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_WR);
71         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE);
72         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_RD);
73     }
74 
75     @CddTest(requirements = {"8.2/H-1-1"})
76     @Test
testSingleSequentialWrite()77     public void testSingleSequentialWrite() throws Exception {
78         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
79         if (fileSize == 0) { // not enough space, give up
80             return;
81         }
82         FileActivity.startFileActivity(getContext());
83         final int numberOfFiles =(int)(fileSize / BUFFER_SIZE);
84         String streamName = "test_single_sequential_write";
85         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
86         report.addValue("files", numberOfFiles, ResultType.NEUTRAL, ResultUnit.COUNT);
87         final byte[] data = FileUtil.generateRandomData(BUFFER_SIZE);
88         final File[] files = FileUtil.createNewFiles(getContext(), DIR_SEQ_WR,
89                 numberOfFiles);
90         double[] rdAmount = new double[numberOfFiles];
91         double[] wrAmount = new double[numberOfFiles];
92         double[] times = FileUtil.measureIO(numberOfFiles, rdAmount, wrAmount, new MeasureRun() {
93 
94             @Override
95             public void run(int i) throws IOException {
96                 FileUtil.writeFile(files[i], data, false);
97             }
98         });
99         double[] mbps = Stat.calcRatePerSecArray((double)BUFFER_SIZE / 1024 / 1024, times);
100         report.addValues("write_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
101         report.addValues("write_amount", wrAmount, ResultType.NEUTRAL, ResultUnit.BYTE);
102         Stat.StatResult stat = Stat.getStat(mbps);
103         report.setSummary("write_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER,
104                 ResultUnit.MBPS);
105         Log.v(TAG, "sequential write " + stat.mAverage + " MBPS");
106         report.submit(getInstrumentation());
107 
108         PerformanceClassEvaluator pce = new PerformanceClassEvaluator(this.mTestName);
109         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_1_1 = pce.addR8_2__H_1_1();
110         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_2_1 = pce.addR8_2__H_2_1();
111         r8_2__H_1_1.setFilesystemIoRate(stat.mAverage);
112         r8_2__H_2_1.setFilesystemIoRate(stat.mAverage);
113 
114         pce.submitAndCheck();
115     }
116 
117     @Test
testSingleSequentialUpdate()118     public void testSingleSequentialUpdate() throws Exception {
119         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
120         if (fileSize == 0) { // not enough space, give up
121             return;
122         }
123         FileActivity.startFileActivity(getContext());
124         final int NUMBER_REPETITION = 3;
125         String streamName = "test_single_sequential_update";
126         FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, fileSize, BUFFER_SIZE,
127                 NUMBER_REPETITION, REPORT_LOG_NAME, streamName);
128     }
129 
130     @CddTest(requirements = {"8.2/H-1-3"})
131     @Test
testSingleSequentialRead()132     public void testSingleSequentialRead() throws Exception {
133         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
134         if (fileSize == 0) { // not enough space, give up
135             return;
136         }
137         FileActivity.startFileActivity(getContext());
138         long start = System.currentTimeMillis();
139         final File file = FileUtil.createNewFilledFile(getContext(),
140                 DIR_SEQ_RD, fileSize);
141         long finish = System.currentTimeMillis();
142         String streamName = "test_single_sequential_read";
143         DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
144         report.addValue("file_size", fileSize, ResultType.NEUTRAL, ResultUnit.NONE);
145         report.addValue("write_throughput",
146                 Stat.calcRatePerSec((double)fileSize / 1024 / 1024, finish - start),
147                 ResultType.HIGHER_BETTER, ResultUnit.MBPS);
148 
149         final int NUMBER_READ = 10;
150 
151         final byte[] data = new byte[BUFFER_SIZE];
152         double[] times = MeasureTime.measure(NUMBER_READ, new MeasureRun() {
153 
154             @Override
155             public void run(int i) throws IOException {
156                 final FileInputStream in = new FileInputStream(file);
157                 long read = 0;
158                 while (read < fileSize) {
159                     in.read(data);
160                     read += BUFFER_SIZE;
161                 }
162                 in.close();
163             }
164         });
165         double[] mbps = Stat.calcRatePerSecArray((double)fileSize / 1024 / 1024, times);
166         report.addValues("read_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
167         Stat.StatResult stat = Stat.getStat(mbps);
168         report.setSummary("read_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER,
169                 ResultUnit.MBPS);
170         Log.v(TAG, "sequential read " + stat.mAverage + " MBPS");
171         report.submit(getInstrumentation());
172 
173         PerformanceClassEvaluator pce = new PerformanceClassEvaluator(this.mTestName);
174         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_1_3 = pce.addR8_2__H_1_3();
175         PerformanceClassEvaluator.FileSystemRequirement r8_2__H_2_3 = pce.addR8_2__H_2_3();
176         r8_2__H_1_3.setFilesystemIoRate(stat.mAverage);
177         r8_2__H_2_3.setFilesystemIoRate(stat.mAverage);
178 
179         pce.submitAndCheck();
180     }
181 }
182