Skip to content

Instantly share code, notes, and snippets.

@amay077
Last active December 21, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amay077/6374470 to your computer and use it in GitHub Desktop.
Save amay077/6374470 to your computer and use it in GitHub Desktop.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import android.content.Context;
import android.os.Environment;
import android.test.InstrumentationTestCase;
import android.util.Log;
public class SDCardStressTest extends InstrumentationTestCase {
private static final String TAG = "SDCardStressTest";
public void testStress00100() throws Exception {
stressN(100);
}
public void testStress00500() throws Exception {
stressN(500);
}
public void testStress01000() throws Exception {
stressN(1000);
}
public void testStress05000() throws Exception {
stressN(5000);
}
public void testStress10000() throws Exception {
stressN(10000);
}
private void stressN(int count) throws Exception {
File dir = Environment.getExternalStorageDirectory();
File appDataDir = new File(dir.getAbsoluteFile() + "/" + getApplicationContext().getPackageName());
if (appDataDir.exists()) {
appDataDir.delete();
}
appDataDir.mkdirs();
long[] sums = new long[5];
for (int i = 0; i < sums.length; i++) { sums[i] = 0; }
final int times = 5;
for (int i = 0; i < times; i++) {
Long[] elapsesA = createRenameDelete(appDataDir, count);
Long[] elapsesB = createFolderDelete(appDataDir, count);
sums[0] = sums[0] + elapsesA[0];
sums[1] = sums[1] + elapsesA[1];
sums[2] = sums[2] + elapsesA[2];
sums[3] = sums[3] + elapsesA[3];
sums[4] = sums[4] + elapsesB[0];
}
Log.d(TAG, "Count:" + count +
",Create:" + sums[0]/times + "ms" +
",List:" + sums[1]/times + "ms" +
",Rename:" + sums[2]/times + "ms" +
",Delete:" + sums[3]/times + "ms" +
",DelDir:" + sums[4]/times + "ms");
}
private Long[] createRenameDelete(File appDataDir, int count) throws Exception {
Long[] elapses = new Long[4];
File[] files = new File[count];
File[] renames = new File[count];
for (int i = 0; i < files.length; i++) {
files[i] = new File(appDataDir.getAbsolutePath() + "/" + i + ".dat");
renames[i] = new File(appDataDir.getAbsolutePath() + "/" + i + ".ren");
}
// Create
long start = System.currentTimeMillis();
for (int i = 0; i < files.length; i++) {
createFile(files[i]);
}
elapses[0] = System.currentTimeMillis() - start;
// List
start = System.currentTimeMillis();
File[] enums = appDataDir.listFiles();
for (File file : enums) {
}
elapses[1] = System.currentTimeMillis() - start;
// Rename
start = System.currentTimeMillis();
for (int i = 0; i < files.length; i++) {
files[i].renameTo(renames[i]);
}
elapses[2] = System.currentTimeMillis() - start;
// Delete
start = System.currentTimeMillis();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
elapses[3] = System.currentTimeMillis() - start;
return elapses;
}
private Long[] createFolderDelete(File appDataDir, int count) throws Exception {
Long[] elapses = new Long[1];
File[] files = new File[count];
File[] renames = new File[count];
for (int i = 0; i < files.length; i++) {
files[i] = new File(appDataDir.getAbsolutePath() + "/" + i + ".dat");
renames[i] = new File(appDataDir.getAbsolutePath() + "/" + i + ".ren");
}
for (int i = 0; i < files.length; i++) {
createFile(files[i]);
}
// Delete Directory
long start = System.currentTimeMillis();
appDataDir.delete();
elapses[0] = System.currentTimeMillis() - start;
return elapses;
}
private void createFile(File file) throws Exception {
OutputStream fos = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("hoge");
bw.flush();
bw.close();
}
/** ApplicationContext を取得します */
public Context getApplicationContext() {
return this.getInstrumentation().getTargetContext().getApplicationContext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment