8000 Benchmark file copy · NagarajJB/Java-Coding-Problems@b272fd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b272fd5

Browse files
committed
Benchmark file copy
1 parent 0d378ee commit b272fd5

File tree

6 files changed

+341
-0
lines changed

6 files changed

+341
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/Chapter01/P01_CountDuplicateCharacters/target/
2+
/Chapter01/P03_ReverseWords/target/
3+
/Chapter01/P05_CountVowelsAndConsonants/target/
4+
/Chapter01/P05_CountVowelsAndConsonants1/target/
5+
/Chapter01/P02_TextBlockDelimiters/target/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<netbeans.compile.on.save>none</netbeans.compile.on.save>
17+
</properties>
18+
</project-shared-configuration>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>java.modern.challenge</groupId>
5+
<artifactId>BONUS_2_CopyFileBenchmark</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>20</maven.compiler.source>
11+
<maven.compiler.target>20</maven.compiler.target>
12+
<mavem.compiler.plugin.version>3.8.0</mavem.compiler.plugin.version>
13+
<jmh.version>1.35</jmh.version>
14+
<oamp.version>3.10.1</oamp.version>
15+
<ocm.version>3.1.0</ocm.version>
16+
</properties>
17+
<name>BONUS_2_CopyFileBenchmark</name>
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.openjdk.jmh</groupId>
21+
<artifactId>jmh-core</artifactId>
22+
<version>${jmh.version}</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.openjdk.jmh</groupId>
26+
<artifactId>jmh-generator-annprocess</artifactId>
27+
<version>${jmh.version}</version>
28+
</dependency>
29+
</dependencies>
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.apache.maven.plugins</groupId>
34+
<artifactId>maven-compiler-plugin</artifactId>
35+
<version>${oamp.version}</version>
36+
<configuration>
37+
<annotationProcessorPaths>
38+
<path>
39+
<groupId>org.openjdk.jmh</groupId>
40+
<artifactId>jmh-generator-annprocess</artifactId>
41+
<version>${jmh.version}</version>
42+
</path>
43+
</annotationProcessorPaths>
44+
</configuration>
45+
</plugin>
46+
<plugin>
47+
<groupId>org.codehaus.mojo</groupId>
48+
<artifactId>exec-maven-plugin</artifactId>
49+
<version>${ocm.version}</version>
50+
<executions>
51+
<execution>
52+
<id>run-benchmarks</id>
53+
<phase>integration-test</phase>
54+
<goals>
55+
<goal>exec</goal>
56+
</goals>
57+
<configuration>
58+
<classpathScope>test</classpathScope>
59+
<executable>java</executable>
60+
<arguments>
61+
<argument>-classpath</argument>
62+
<classpath />
63+
<argument>org.openjdk.jmh.Main</argument>
64+
<argument>.*</argument>
65+
</arguments>
66+
</configuration>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
</project>
Binary file not shown.
Loading
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
package modern.challenge;
2+
3+
import java.nio.MappedByteBuffer;
4+
import java.io.OutputStream;
5+
import java.io.InputStream;
6+
import java.io.BufferedInputStream;
7+
import java.io.BufferedOutputStream;
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
import java.nio.ByteBuffer;
13+
import java.nio.channels.FileChannel;
14+
import java.nio.file.Files;
15+
import java.nio.file.Path;
16+
import java.nio.file.Paths;
17+
import java.nio.file.StandardOpenOption;
18+
import java.util.EnumSet;
19+
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
20+
import java.util.Random;
21+
import java.util.concurrent.TimeUnit;
22+
import org.openjdk.jmh.annotations.Benchmark;
23+
import org.openjdk.jmh.annotations.BenchmarkMode;
24+
import org.openjdk.jmh.annotations.Fork;
25+
import org.openjdk.jmh.annotations.Measurement;
26+
import org.openjdk.jmh.annotations.Mode;
27+
import org.openjdk.jmh.annotations.OutputTimeUnit;
28+
import org.openjdk.jmh.annotations.Scope;
29+
import org.openjdk.jmh.annotations.State;
30+
31+
@OutputTimeUnit(TimeUnit.SECONDS)
32+
@BenchmarkMode(Mode.AverageTime)
33+
@Fork(value = 1, warmups = 1) //, jvmArgsPrepend = {"-Djdk.net.usePlainSocketImpl=true"})
34+
@Measurement(iterations = 1, time = 1)
35+
@State(Scope.Benchmark)
36+
public class Main {
37+
38+
private static final Path COPY_FROM = Paths.get("rafa_org/Rafa Best Shots.mp4");
39+
private static final Path COPY_TO = Paths.get("rafa_copy/");
40+
private static final int BUFFER_SIZE_KB = 4;
41+
private static final int BUFFER_SIZE = BUFFER_SIZE_KB * 1024;
42+
43+
private static final Random rnd = new Random();
44+
45+
@Benchmark
46+
public static void fileChannelIndirectBuffer() {
47+
48+
System.out.println("Using FileChannel and non-direct buffer ...");
49+
50+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
51+
try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
52+
FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
53+
54+
// Allocate an non-direct ByteBuffer
55+
ByteBuffer bytebuffer = ByteBuffer.allocate(BUFFER_SIZE);
56+
57+
// Read data from file into ByteBuffer
58+
while ((fileChannel_from.read(bytebuffer)) > 0) {
59+
60+
//flip the buffer which set the limit to current position, and position to 0
61+
bytebuffer.flip();
62+
63+
//write data from ByteBuffer to file
64+
fileChannel_to.write(bytebuffer);
65+
66+
//for the next read
67+
bytebuffer.clear();
68+
}
69+
} catch (IOException ex) {
70+
System.err.println(ex);
71+
}
72+
}
73+
74+
@Benchmark
75+
public static void fileChannelDirectBuffer() {
76+
77+
System.out.println("Using FileChannel and direct buffer ...");
78+
79+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
80+
try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
81+
FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
82+
83+
// Allocate an direct ByteBuffer
84+
ByteBuffer bytebuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
85+
86+
// Read data from file into ByteBuffer
87+
while ((fileChannel_from.read(bytebuffer)) > 0) {
88+
89+
//flip the buffer which set the limit to current position, and position to 0
90+
bytebuffer.flip();
91+
92+
//write data from ByteBuffer to file
93+
fileChannel_to.write(bytebuffer);
94+
95+
//for the next read
96+
bytebuffer.clear();
97+
}
98+
} catch (IOException ex) {
99+
System.err.println(ex);
100+
}
101+
}
102+
103+
@Benchmark
104+
public static void fileChannelTransferTo() {
105+
106+
System.out.println("Using FileChannel.transferTo method ...");
107+
108+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
109+
try (FileChannel fileChannel_from = (FileChannel.open(
110+
COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
111+
FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(
112+
StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
113+
114+
fileChannel_from.transferTo(0L, fileChannel_from.size(), fileChannel_to);
115+
116+
} catch (IOException ex) {
117+
System.err.println(ex);
118+
}
119+
}
120+
121+
@Benchmark
122+
public static void fileChannelTransferFrom() {
123+
124+
System.out.println("Using FileChannel.transferFrom method ...");
125+
126+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
127+
try (FileChannel fileChannel_from = (FileChannel.open(
128+
COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
129+
FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(
130+
StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
131+
132+
fileChannel_to.transferFrom(fileChannel_from, 0L, (int) fileChannel_from.size());
133+
} catch (IOException ex) {
134+
System.err.println(ex);
135+
}
136+
}
137+
138+
@Benchmark
139+
public static void fileChannelMap() {
140+
141+
System.out.println("Using FileChannel.map method ...");
142+
143+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
144+
try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
145+
FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
146+
147+
MappedByteBuffer buffer = fileChannel_from.map(
148+
FileChannel.MapMode.READ_ONLY, 0, fileChannel_from.size());
149+
150+
fileChannel_to.write(buffer);
151+
buffer.clear();
152+
153+
} catch (IOException ex) {
154+
System.err.println(ex);
155+
}
156+
}
157+
158+
@Benchmark
159+
public static void bufferedStreamIO() {
160+
161+
System.out.println("Using buffered streams and byte array ...");
162+
163+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
164+
File inFileStr = COPY_FROM.toFile();
165+
File outFileStr = copyTo.toFile();
166+
167+
try (BufferedInputStream in = new BufferedInputStream(
168+
new FileInputStream(inFileStr)); BufferedOutputStream out
169+
= new BufferedOutputStream(new FileOutputStream(outFileStr))) {
170+
171+
byte[] byteArray = new byte[BUFFER_SIZE];
172+
int bytesCount;
173+
while ((bytesCount = in.read(byteArray)) != -1) {
174+
out.write(byteArray, 0, bytesCount);
175+
}
176+
} catch (IOException ex) {
177+
System.err.println(ex);
178+
}
179+
}
180+
181+
@Benchmark
182+
public static void bufferedStreamByteArray() {
183+
184+
System.out.println("Using un-buffered streams and byte array ...");
185+
186+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
187+
File inFileStr = COPY_FROM.toFile();
188+
File outFileStr = copyTo.toFile();
189+
190+
try (FileInputStream in = new FileInputStream(inFileStr);
191+
FileOutputStream out = new FileOutputStream(outFileStr)) {
192+
193+
byte[] byteArray = new byte[BUFFER_SIZE];
194+
int bytesCount;
195+
while ((bytesCount = in.read(byteArray)) != -1) {
196+
out.write(byteArray, 0, bytesCount);
197+
}
198+
} catch (IOException ex) {
199+
System.err.println(ex);
200+
}
201+
}
202+
203+
@Benchmark
204+
public static void filesCopyPathToPath() {
205+
206+
System.out.println("Using Files.copy (Path to Path) method ...");
207+
208+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
209+
try {
210+
Files.copy(COPY_FROM, copyTo, NOFOLLOW_LINKS);
211+
} catch (IOException e) {
212+
System.err.println(e);
213+
}
214+
}
215+
216+
@Benchmark
217+
public static void filesCopyIStreamToPath() {
218+
219+
System.out.println("Using Files.copy (InputStream to Path) ...");
220+
221+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
222+
try (InputStream is = new FileInputStream(COPY_FROM.toFile())) {
223+
Files.copy(is, copyTo);
224+
} catch (IOException e) {
225+
System.err.println(e);
226+
}
227+
}
228+
229+
@Benchmark
230+
public static void filesCopyPathToOStream() {
231+
232+
System.out.println("Using Files.copy (Path to OutputStream) ...");
233+
234+
Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
235+
try (OutputStream os = new FileOutputStream(copyTo.toFile())) {
236+
Files.copy(COPY_FROM, os);
237+
} catch (IOException e) {
238+
System.err.println(e);
239+
}
240+
}
241+
242+
public static void main(String[] args) throws IOException {
243+
244+
org.openjdk.jmh.Main.main(args);
245+
}
246+
}

0 commit comments

Comments
 (0)
0