有关存储吞吐量计算的Android CTS代码

时间:2017-07-20 12:26:08

标签: java android ram throughput cts

我需要计算Android设备中的存储吞吐量,并且我找到了有关计算Android CTS中存储顺序RW吞吐量的源代码。

FileUtil.java

public static long getFileSizeExceedingMemory(Context context, int bufferSize) {
    long freeDisk = SystemUtil.getFreeDiskSize(context);
    long memSize = SystemUtil.getTotalMemory(context);
    long diskSizeTarget = (2 * memSize / bufferSize) * bufferSize;
    final long minimumDiskSize = (512L * 1024L * 1024L / bufferSize) * bufferSize;
    final long reservedDiskSize = (50L * 1024L * 1024L / bufferSize) * bufferSize;
    if ( diskSizeTarget < minimumDiskSize ) {
        diskSizeTarget = minimumDiskSize;
    }
    if (diskSizeTarget > freeDisk) {
        Log.i(TAG, "Free disk size " + freeDisk + " too small");
        return 0;
    }
    if ((freeDisk - diskSizeTarget) < reservedDiskSize) {
        diskSizeTarget -= reservedDiskSize;
    }
    return diskSizeTarget;
}

此函数即将创建一个从RAM到存储(写入)的文件,然后再回读。

我只是想知道:

long diskSizeTarget = (2 * memSize / bufferSize) * bufferSize;

为什么他们需要准备一个大约双公羊尺寸的文件?

我曾经尝试过文件大小是ram大小的一半(我的设备ram大小= 2GB),写入吞吐量看起来正常,但读取吞吐量太快(大约200MB / s)。

但是当我使用大约4GB文件(双ram大小)和2GB时,结果看起来很好。

(buffersize参数为10MB,用于读写)

以下是读写代码:

SequentialRWTest.java

public void testSingleSequentialRead() throws Exception {
    final long fileSize = FileUtil.getFileSizeExceedingMemory(getContext(), BUFFER_SIZE);
    if (fileSize == 0) { // not enough space, give up
        return;
    }
    long start = System.currentTimeMillis();
    final File file = FileUtil.createNewFilledFile(getContext(),
            DIR_SEQ_RD, fileSize);
    long finish = System.currentTimeMillis();
    String streamName = "test_single_sequential_read";
    DeviceReportLog report = new DeviceReportLog(REPORT_LOG_NAME, streamName);
    report.addValue("file_size", fileSize, ResultType.NEUTRAL, ResultUnit.NONE);
    report.addValue("write_throughput",
            Stat.calcRatePerSec((double)fileSize / 1024 / 1024, finish - start),
            ResultType.HIGHER_BETTER, ResultUnit.MBPS);
    final int NUMBER_READ = 10;
    final byte[] data = new byte[BUFFER_SIZE];
    double[] times = MeasureTime.measure(NUMBER_READ, new MeasureRun() {
        @Override
        public void run(int i) throws IOException {
            final FileInputStream in = new FileInputStream(file);
            long read = 0;
            while (read < fileSize) {
                in.read(data);
                read += BUFFER_SIZE;
            }
            in.close();
        }
    });
    double[] mbps = Stat.calcRatePerSecArray((double)fileSize / 1024 / 1024, times);
    report.addValues("read_throughput", mbps, ResultType.HIGHER_BETTER, ResultUnit.MBPS);
    Stat.StatResult stat = Stat.getStat(mbps);
    report.setSummary("read_throughput_average", stat.mAverage, ResultType.HIGHER_BETTER,
            ResultUnit.MBPS);
    report.submit(getInstrumentation());
}

FileUtil.java中的createNewFilledFile函数

public static File createNewFilledFile(Context context, String dirName, long length)
        throws IOException {
    final int BUFFER_SIZE = 10 * 1024 * 1024;
    File file = createNewFile(context, dirName);
    FileOutputStream out = new FileOutputStream(file);
    byte[] data = generateRandomData(BUFFER_SIZE);
    long written = 0;
    while (written < length) {
        out.write(data);
        written += BUFFER_SIZE;
    }
    out.flush();
    out.close();
    return file;
}

0 个答案:

没有答案