My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright (c) 2011. Peter Lawrey
*
* "THE BEER-WARE LICENSE" (Revision 128)
* As long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy me a beer in return
* There is no warranty.
*/

package com.google.code.java.core.files;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import static junit.framework.Assert.assertTrue;

public class OpenCloseFilesTest {
public static final String TMPFS_DIR = System.getProperty("tmpfs.dir", "/tmp/");
private static final String LOCAL_FS_DIR = System.getProperty("user.home");
public static final String NFS_DIR = System.getProperty("nfs.dir");

@Test
public void tmpfsPerfTest() throws IOException {
int files = 100 * 1000;

// must be a tmpfs file system.
File dir = new File(TMPFS_DIR + "/deleteme");
assertTrue(dir.mkdir());
System.out.println("Created " + dir.getAbsolutePath());

byte[] bytes = new byte[256];
long start = 0;
for (int i = -files / 10; i < files; i++) {
if (i == 0) start = System.nanoTime();
File file = new File(dir, Integer.toString(i));
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
}
long time = System.nanoTime() - start;
System.out.printf("Took an average of %.1f us to write to TMPFS%n", time / 1e3 / files);

long start2 = System.nanoTime();
for (int i = -files / 10; i < files; i++) {
File file = new File(dir, Integer.toString(i));
file.delete();
}
assertTrue(dir.delete());
long time2 = System.nanoTime() - start2;
System.out.println("Removed " + dir.getAbsolutePath());

System.out.printf("Took an average of %.1f us to delete files from TMPFS%n", time2 / 1e3 / (files + files / 10 + 1));

}

@Test
public void localfsPerfTest() throws IOException {
int files = 10 * 1000;

// must be a tmpfs file system.
File dir = new File(LOCAL_FS_DIR + "/deleteme");
assertTrue(dir.mkdir());
System.out.println("Created " + dir.getAbsolutePath());

byte[] bytes = new byte[256];
long start = System.nanoTime();
for (int i = 0; i < files; i++) {
File file = new File(dir, Integer.toString(i));
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
}
long time = System.nanoTime() - start;
System.out.printf("Took an average of %.1f us to write to LOCAL filesystem%n", time / 1e3 / files);

long start2 = System.nanoTime();
for (int i = 0; i < files; i++) {
File file = new File(dir, Integer.toString(i));
file.delete();
}
assertTrue(dir.delete());
long time2 = System.nanoTime() - start2;
System.out.println("Removed " + dir.getAbsolutePath());

System.out.printf("Took an average of %.1f us to delete files from LOCAL filesystem%n", time2 / 1e3 / (files + 1));
}

@Test
public void nfsPerfTest() throws IOException {
int files = 250;
if (NFS_DIR == null) {
System.err.println("You must set -Dnfs.dir= to the location of a writable NFS drive to run this test");
return;
}

// must be a tmpfs file system.
File dir = new File(NFS_DIR + "/deleteme");
assertTrue(dir.mkdir());
System.out.println("Created " + dir.getAbsolutePath());

byte[] bytes = new byte[256];
long start = 0;
for (int i = 0; i < files; i++) {
if (i == 0) start = System.nanoTime();
File file = new File(dir, Integer.toString(i));
FileOutputStream fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
}
long time = System.nanoTime() - start;
System.out.printf("Took an average of %,d us to write to the NFS drive%n", time / 1000 / files);

long start2 = System.nanoTime();
for (int i = 0; i < files; i++) {
File file = new File(dir, Integer.toString(i));
file.delete();
}
assertTrue(dir.delete());
long time2 = System.nanoTime() - start2;
System.out.println("Removed " + dir.getAbsolutePath());

System.out.printf("Took an average of %,d us to delete files from the NFS drive%n", time2 / 1000 / (files + 1));
}
}

Change log

r58 by peter.lawrey on Aug 30, 2011   Diff
Beerware licensing.
Go to: 
Project members, sign in to write a code review

Older revisions

r44 by peter.lawrey on Aug 1, 2011   Diff
OpenClosing files test
r43 by peter.lawrey on Aug 1, 2011   Diff
OpenClosing files test
All revisions of this file

File info

Size: 4263 bytes, 127 lines
Powered by Google Project Hosting