android中写入文件时的延迟峰值
我正在运行一些性能测试,并在测试结果中看到一些峰值。 这是我用来测量写入文件时间的代码android中写入文件时的延迟峰值,android,performance,testing,file-io,latency,Android,Performance,Testing,File Io,Latency,我正在运行一些性能测试,并在测试结果中看到一些峰值。 这是我用来测量写入文件时间的代码 public class Test extends AndroidTestCase { private DecimalFormat decimalFormat = new DecimalFormat("#.##"); private final Boolean isInternal = true; public void testWrite() { Thread thread = new Thre
public class Test extends AndroidTestCase {
private DecimalFormat decimalFormat = new DecimalFormat("#.##");
private final Boolean isInternal = true;
public void testWrite() {
Thread thread = new Thread() {
public void run() {
File dummy_file = createFile("dummy2", true);
File file = createFile("normal_write", isInternal);
for (int i = 0; i < 12500; i++) {
long start = System.nanoTime();
write(dummy_file, String.valueOf(i));
long stop = System.nanoTime();
double mSec = ((double) (stop - start) / 1000000.0);
write(file, decimalFormat.format(mSec));
}
}
};
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public File createFile(String fileName, Boolean isInternal) {
deleteFile(fileName, isInternal);
try {
String file_path = "";
if(isInternal == true) {
file_path = getContext().getFilesDir().getAbsolutePath() + "/" + fileName + ".dat";
} else {
file_path = Environment.getExternalStorageDirectory() + "/" + fileName + ".dat";
}
File file = new File(file_path);
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("##;##\n" +
"@LiveGraph demo file.\n" +
"Time");
bw.newLine();
bw.close();
return file;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void deleteFile(String fileName, Boolean isInternal) {
String file_path = "";
if ( isInternal == true) {
file_path = getContext().getFilesDir().getAbsolutePath() + "/" + fileName + ".dat";
} else {
file_path = Environment.getExternalStorageDirectory() + "/" + fileName + ".dat";
}
File file = new File(file_path);
file.delete();
}
public void write(File file, String content) {
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
大多数情况下,写入所需的时间约为0.1-0.2ms,但也存在需要10+ms的峰值,无法找出原因。我现在不能在这里链接图表,因为我是这里的新用户
我真的被这件事缠住了有什么想法吗
这是时间测量的一部分
0.16
0.16
0.17
0.15
0.15
2.5
0.17
0.16
0.16
0.16
0.15
0.17
0.17
0.17
0.19
0.19
0.17
0.2
0
0
4.79
0.24
0.23
0.28
0.23
0.28
0.03
0
11.23
0.16
0.15
0.18
0.16
0.16
0.17
0.16
5.84这可能与Android的存储访问细节有关: 手机磁盘上的背景等等,点击 磁盘Android设备都运行闪存,对吗?那就像 没有移动部件的超高速SSD?我不该在乎? 不幸的是,你知道 您不能依赖大多数应用程序中使用的闪存组件或文件系统 安卓设备必须始终保持快速。上使用的YAFFS文件系统 例如,许多安卓设备的所有功能都有一个全局锁 操作。整个系统中只能执行一个磁盘操作 装置即使是一个简单的“stat”操作,如果您 你运气不好。其他具有更传统的基于块设备的设备 当块旋转层 决定进行垃圾收集并执行一些缓慢的内部闪存擦除 操作。有关一些好的极客背景阅读,请参阅 lwn.net/Articles/353411 需要注意的是,移动设备上的“磁盘”或文件系统 通常很快,但第90百分位潜伏期通常很差。 而且,大多数文件系统的速度会随着它们的使用量的增加而减慢。 查看谷歌I/O Zippy Android应用程序对话的幻灯片,链接关闭 code.google.com/p/zippy-android
这难道不包括在真实设备上运行的场景吗?我在模拟器上做这件事,我的笔记本电脑只有一个普通的硬盘。也许是垃圾收集?您正在创建相当多的对象,而GC可能需要一些CPU时间,这在模拟器上非常昂贵。检查logcat,它应该包含关于GC的信息。如果不是这样,请在标准VM上运行测试并比较结果-可能是您的笔记本电脑的FS/OS问题。由于使用了java nanotime,这可能是问题的原因吗?GC呢?你检查过了吗?是的,我检查过GC是否是原因,但似乎不是,因为在没有运行它的情况下存在峰值。