Home | History | Annotate | Download | only in filesystemperf
      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 com.android.cts.filesystemperf;
     18 
     19 import android.cts.util.CtsAndroidTestCase;
     20 import com.android.cts.util.MeasureRun;
     21 import com.android.cts.util.MeasureTime;
     22 import com.android.cts.util.ResultType;
     23 import com.android.cts.util.ResultUnit;
     24 import com.android.cts.util.ReportLog;
     25 import com.android.cts.util.Stat;
     26 import com.android.cts.util.TimeoutReq;
     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 int BUFFER_SIZE = 10 * 1024 * 1024;
     37 
     38     @Override
     39     protected void tearDown() throws Exception {
     40         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_WR);
     41         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_UPDATE);
     42         FileUtil.removeFileOrDir(getContext(), DIR_SEQ_RD);
     43         super.tearDown();
     44     }
     45 
     46     @TimeoutReq(minutes = 30)
     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         getReportLog().printValue("files", numberOfFiles, ResultType.NEUTRAL,
     54                 ResultUnit.COUNT);
     55         final byte[] data = FileUtil.generateRandomData(BUFFER_SIZE);
     56         final File[] files = FileUtil.createNewFiles(getContext(), DIR_SEQ_WR,
     57                 numberOfFiles);
     58         double[] rdAmount = new double[numberOfFiles];
     59         double[] wrAmount = new double[numberOfFiles];
     60         double[] times = FileUtil.measureIO(numberOfFiles, rdAmount, wrAmount, new MeasureRun() {
     61 
     62             @Override
     63             public void run(int i) throws IOException {
     64                 FileUtil.writeFile(files[i], data, false);
     65             }
     66         });
     67         double[] mbps = ReportLog.calcRatePerSecArray((double)BUFFER_SIZE / 1024 / 1024, times);
     68         getReportLog().printArray("write throughput",
     69                 mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
     70         getReportLog().printArray("write amount", wrAmount, ResultType.NEUTRAL,
     71                 ResultUnit.BYTE);
     72         Stat.StatResult stat = Stat.getStat(mbps);
     73         getReportLog().printSummary("write throughput", stat.mAverage, ResultType.HIGHER_BETTER,
     74                 ResultUnit.MBPS);
     75     }
     76 
     77     @TimeoutReq(minutes = 60)
     78     public void testSingleSequentialUpdate() throws Exception {
     79         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
     80         if (fileSize == 0) { // not enough space, give up
     81             return;
     82         }
     83         final int NUMBER_REPETITION = 6;
     84         FileUtil.doSequentialUpdateTest(getContext(), DIR_SEQ_UPDATE, getReportLog(), fileSize,
     85                 BUFFER_SIZE, NUMBER_REPETITION);
     86     }
     87 
     88     @TimeoutReq(minutes = 30)
     89     public void testSingleSequentialRead() throws Exception {
     90         final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
     91         if (fileSize == 0) { // not enough space, give up
     92             return;
     93         }
     94         long start = System.currentTimeMillis();
     95         final File file = FileUtil.createNewFilledFile(getContext(),
     96                 DIR_SEQ_RD, fileSize);
     97         long finish = System.currentTimeMillis();
     98         getReportLog().printValue("write throughput for test file of length " + fileSize,
     99                 ReportLog.calcRatePerSec((double)fileSize / 1024 / 1024, finish - start),
    100                 ResultType.HIGHER_BETTER, ResultUnit.MBPS);
    101 
    102         final int NUMBER_READ = 10;
    103 
    104         final byte[] data = new byte[BUFFER_SIZE];
    105         double[] times = MeasureTime.measure(NUMBER_READ, new MeasureRun() {
    106 
    107             @Override
    108             public void run(int i) throws IOException {
    109                 final FileInputStream in = new FileInputStream(file);
    110                 long read = 0;
    111                 while (read < fileSize) {
    112                     in.read(data);
    113                     read += BUFFER_SIZE;
    114                 }
    115                 in.close();
    116             }
    117         });
    118         double[] mbps = ReportLog.calcRatePerSecArray((double)fileSize / 1024 / 1024, times);
    119         getReportLog().printArray("read throughput",
    120                 mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
    121         Stat.StatResult stat = Stat.getStat(mbps);
    122         getReportLog().printSummary("read throughput", stat.mAverage, ResultType.HIGHER_BETTER,
    123                 ResultUnit.MBPS);
    124     }
    125 }
    126