java - Android 内部存储基准测试提供了难以置信的快速读取速度

标签 java android io benchmarking

所以我正在为 Android 创建一个基准测试应用程序。现在我正在尝试添加测试内部存储读/写速度的功能。为了测试读取速度,我首先创建一个文件(几兆字节)。接下来,我会在 5 秒内尽可能多地读回该文件,然后计算速度。

下面是示例代码,演示了我的代码的简化版本。我从代码(在 Galaxy S4 上)得到的输出是:

File Size (should be 4096kb): 4096kb
Total Read: 3137792 MB
Read Rate: 612.85 MB/sec

这显然太快了,不太真实(我预计它在 30-60MB 范围内)。

测试代码:

private void readTest()
{
    final int FILE_SIZE = 1024 * 1024 * 4;
    final int CHUNK_SIZE = 1024 * 128;
    final int CHUNK_NUM = FILE_SIZE / CHUNK_SIZE;
    final int READ_DURATION = 5000;

    File outputDir = Globals.getContext().getCacheDir();
    try
    {
        File tempFile = File.createTempFile("InternalStorageRead", "tmp", outputDir);

        // Generate some data to write to temp file
        byte[] buffer = new byte[CHUNK_SIZE];
        for (int i = 0; i < CHUNK_SIZE; i++)
        {
            buffer[i] = (byte)((i % 256) - 128);
        }

        // Write generated data into file
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile));
        for (int i = 0; i < CHUNK_NUM; i++)
        {
            bos.write(buffer);
            bos.flush();
        }
        bos.close();

        System.out.println("File Size (should be " + (FILE_SIZE / 1024) + "kb): " + (tempFile.length() / 1024) + "kb");

        long startTimeMS = System.currentTimeMillis();

        FileInputStream is = new FileInputStream(tempFile);
        long bytesRead = 0;
        while (System.currentTimeMillis() - startTimeMS < READ_DURATION)
        {
            for (int i = 0; i < 10; i++)
            {
                int read = is.read(buffer);
                if (read > 0)
                {
                    bytesRead += read;
                }
                else
                {
                    // EOF - start reading again from beginning
                    is.close();
                    is = new FileInputStream(tempFile);
                }
            }
        }
        is.close();

        double mb = bytesRead / (1024.0 * 1024.0);
        System.out.println("Total Read: " + (bytesRead / 1024) + " MB");

        double readRate = mb / (READ_DURATION / 1000.0);
        System.out.println("Read Rate: " + readRate + " MB/sec");
    }
    catch (IOException e1)
    {
        e1.printStackTrace();
        return;
    }
}

我的代码是否有什么问题(我错过了什么,或者编译器优化),还是 Android 的问题?就像内存缓存或类似的东西。

最佳答案

您的数据缓存在 RAM 中,因此您可以从那里测量速度。您需要打开文件进行直接 I/O 以避免缓存。然后,某些更高版本的 Android 版本或硬件设备不允许在内部驱动器上执行此操作。此外,驱动器的路径不是标准的。请参阅我的 DriveSpeed 部分:

http://www.roylongbottom.org.uk/android%20benchmarks.htm#anchor17

您可以在那里获取我的 DriveSpeed.apk,其中包含内部和外部驱动器按钮以及不删除测试文件的选项。关闭电源然后再打开会清除缓存,并且可以从驱动器测量读取(一次)。您可以安排 3 个程序来完成此操作,其中一个用于写入,一个用于读取,一个用于删除。

还有DriveSpd2.apk,您可以在其中输入要使用的路径。

带有 Java 和 JNI/C 代码的 Eclipse 项目位于

http://www.roylongbottom.org.uk/Android%20Benchmarks.zip

关于java - Android 内部存储基准测试提供了难以置信的快速读取速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18197634/

相关文章:

c - 将用户输入的字符串写入 C 文件

Java 正则表达式 : find a match only at the beginnings of words

java - Storm 观察服务有什么吗?

Java 缓冲编写器不会写入所有数据

java - JAXB - 复合模式和@XmlValue

android - 更改运行时权限的语言

android - ArrayAdapter 使用 Kotlin android

Android 应用程序仅在发布 APK 时崩溃

java - 如何从java中的文本文件中跳过特定行?

haskell - 在 Haskell 中组合 monad