そこで、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 に何かありますか? メモリ キャッシュなどのようなものです。