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.cts.util.CtsAndroidTestCase; 20 21 import com.android.compatibility.common.util.DeviceReportLog; 22 import com.android.compatibility.common.util.MeasureRun; 23 import com.android.compatibility.common.util.MeasureTime; 24 import com.android.compatibility.common.util.ResultType; 25 import com.android.compatibility.common.util.ResultUnit; 26 import com.android.compatibility.common.util.Stat; 27 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.IOException; 31 32 public class SequentialRWTest extends CtsAndroidTestCase { 33 private static final String DIR_SEQ_WR = "SEQ_WR"; 34 private static final String DIR_SEQ_UPDATE = "SEQ_UPDATE"; 35 private static final String DIR_SEQ_RD = "SEQ_RD"; 36 private static final String REPORT_LOG_NAME = "CtsFileSystemTestCases"; 37 private static final int BUFFER_SIZE = 10 * 1024 * 1024; 38 39 @Override 40 protected void tearDown() throws Exception { 41 FileUtil.removeFileOrDir(getContext(), DIR_SEQ_WR); 42 FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE); 43 FileUtil.removeFileOrDir(getContext(), DIR_SEQ_RD); 44 super.tearDown(); 45 } 46 47 public void testSingleSequentialWrite() throws Exception { 48 final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE); 49 if (fileSize == 0) { // not enough space, give up 50 return; 51 } 52 final int numberOfFiles =(int)(fileSize / BUFFER_SIZE); 53 String streamName = "test_single_sequential_write"; 54 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 55 report.addValue("files", numberOfFiles, ResultType.NEUTRAL, ResultUnit.COUNT); 56 final byte[] data = FileUtil.generateRandomData(BUFFER_SIZE); 57 final File[] files = FileUtil.createNewFiles(getContext(), DIR_SEQ_WR, 58 numberOfFiles); 59 double[] rdAmount = new double[numberOfFiles]; 60 double[] wrAmount = new double[numberOfFiles]; 61 double[] times = FileUtil.measureIO(numberOfFiles, rdAmount, wrAmount, new MeasureRun() { 62 63 @Override 64 public void run(int i) throws IOException { 65 FileUtil.writeFile(files[i], data, false); 66 } 67 }); 68 double[] mbps = Stat.calcRatePerSecArray((double)BUFFER_SIZE / 1024 / 1024, times); 69 report.addValues("write_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS); 70 report.addValues("write_amount", wrAmount, ResultType.NEUTRAL, ResultUnit.BYTE); 71 Stat.StatResult stat = Stat.getStat(mbps); 72 report.setSummary("write_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER, 73 ResultUnit.MBPS); 74 report.submit(getInstrumentation()); 75 } 76 77 public void testSingleSequentialUpdate() throws Exception { 78 final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE); 79 if (fileSize == 0) { // not enough space, give up 80 return; 81 } 82 final int NUMBER_REPETITION = 6; 83 String streamName = "test_single_sequential_update"; 84 FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, fileSize, BUFFER_SIZE, 85 NUMBER_REPETITION, REPORT_LOG_NAME, streamName); 86 } 87 88 public void testSingleSequentialRead() throws Exception { 89 final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE); 90 if (fileSize == 0) { // not enough space, give up 91 return; 92 } 93 long start = System.currentTimeMillis(); 94 final File file = FileUtil.createNewFilledFile(getContext(), 95 DIR_SEQ_RD, fileSize); 96 long finish = System.currentTimeMillis(); 97 String streamName = "test_single_sequential_read"; 98 DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName); 99 report.addValue("file_size", fileSize, ResultType.NEUTRAL, ResultUnit.NONE); 100 report.addValue("write_throughput", 101 Stat.calcRatePerSec((double)fileSize / 1024 / 1024, finish - start), 102 ResultType.HIGHER_BETTER, ResultUnit.MBPS); 103 104 final int NUMBER_READ = 10; 105 106 final byte[] data = new byte[BUFFER_SIZE]; 107 double[] times = MeasureTime.measure(NUMBER_READ, new MeasureRun() { 108 109 @Override 110 public void run(int i) throws IOException { 111 final FileInputStream in = new FileInputStream(file); 112 long read = 0; 113 while (read < fileSize) { 114 in.read(data); 115 read += BUFFER_SIZE; 116 } 117 in.close(); 118 } 119 }); 120 double[] mbps = Stat.calcRatePerSecArray((double)fileSize / 1024 / 1024, times); 121 report.addValues("read_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS); 122 Stat.StatResult stat = Stat.getStat(mbps); 123 report.setSummary("read_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER, 124 ResultUnit.MBPS); 125 report.submit(getInstrumentation()); 126 } 127 } 128