Last active
April 11, 2017 08:54
-
-
Save vladimirdolzhenko/f04c31788fdaf45c825b34bf9fb1bbb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write 1G file to disk using diff approaches: | |
Classic IO (and BufferedOutputsteam 1M buffer) | |
/tmp/test-classicio-8407672220140435592.tmp took 6286.606 ms | |
Classic Java7 IO (and BufferedOutputsteam 1M buffer) | |
/tmp/test-classicio7-8294681779321435770.tmp took 3125.067 ms | |
Mapped file of FileChannel | |
/tmp/test-channel-1999197077234588867.tmp took 14549.853 ms | |
Direct ByteBuffer (1Mb as well) on top of FileChannel | |
/tmp/test-channel-bb-2945554397403680634.tmp took 15481.81 ms | |
ByteBuffer (1Mb as well) on top of FileChannel | |
/tmp/test-channel-heap-bb-7561008927137103024.tmp took 15687.895 ms | |
*/ | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.OutputStream; | |
import java.nio.ByteBuffer; | |
import java.nio.MappedByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardOpenOption; | |
import sun.nio.ch.DirectBuffer; | |
public class MappedTest { | |
final long size = 1024L * 1024L * 1024L; | |
public static void unmap(MappedByteBuffer buffer) { | |
sun.misc.Cleaner cleaner = ((DirectBuffer) buffer).cleaner(); | |
cleaner.clean(); | |
} | |
public void mapped() throws Exception { | |
final Path tempFile = Files.createTempFile("test-channel-", ".tmp"); | |
System.out.println(tempFile.toAbsolutePath()); | |
final long start = System.nanoTime(); | |
try (final FileChannel channel = FileChannel.open(tempFile, | |
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ, | |
StandardOpenOption.TRUNCATE_EXISTING | |
)) { | |
long pos = 0; | |
final byte[] bs = "0123456789".getBytes("UTF-8"); | |
for (int k = 0; k < 2; k++) { | |
final MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, pos, size); | |
for (int i = 0; i < (size / bs.length); i++) { | |
map.put(bs, 0, bs.length); | |
} | |
unmap(map); | |
pos += size; | |
} | |
channel.force(true); | |
channel.close(); | |
} | |
final long end = System.nanoTime(); | |
tempFile.toFile().delete(); | |
System.out.println(tempFile.toAbsolutePath() + " took " + ((end - start) / 1000 / 1e3) + " ms"); | |
} | |
public void heapchannel() throws Exception { | |
final Path tempFile = Files.createTempFile("test-channel-heap-bb-", ".tmp"); | |
System.out.println(tempFile.toAbsolutePath()); | |
final long start = System.nanoTime(); | |
try (final FileChannel channel = FileChannel.open(tempFile, | |
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ, | |
StandardOpenOption.TRUNCATE_EXISTING)) { | |
long pos = 0; | |
final byte[] bs = "0123456789".getBytes("UTF-8"); | |
final ByteBuffer buffer = ByteBuffer.allocate(1 << 20); | |
for (int k = 0; k < 2; k++) { | |
for (int i = 0; i < (size / bs.length); i++) { | |
if (buffer.remaining() < bs.length) { | |
buffer.flip(); | |
channel.write(buffer); | |
buffer.compact(); | |
} | |
buffer.put(bs); | |
} | |
pos += size; | |
} | |
buffer.flip(); | |
channel.write(buffer); | |
buffer.compact(); | |
channel.force(true); | |
channel.close(); | |
} | |
final long end = System.nanoTime(); | |
tempFile.toFile().delete(); | |
System.out.println(tempFile.toAbsolutePath() + " took " + ((end - start) / 1000 / 1e3) + " ms"); | |
} | |
public void channel() throws Exception { | |
final Path tempFile = Files.createTempFile("test-channel-bb-", ".tmp"); | |
System.out.println(tempFile.toAbsolutePath()); | |
final long start = System.nanoTime(); | |
try (final FileChannel channel = FileChannel.open(tempFile, | |
StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.READ, | |
StandardOpenOption.TRUNCATE_EXISTING)) { | |
long pos = 0; | |
final byte[] bs = "0123456789".getBytes("UTF-8"); | |
final ByteBuffer buffer = ByteBuffer.allocateDirect(1 << 20); | |
for (int k = 0; k < 2; k++) { | |
for (int i = 0; i < (size / bs.length); i++) { | |
if (buffer.remaining() < bs.length) { | |
buffer.flip(); | |
channel.write(buffer); | |
buffer.compact(); | |
} | |
buffer.put(bs); | |
} | |
pos += size; | |
} | |
buffer.flip(); | |
channel.write(buffer); | |
buffer.compact(); | |
channel.force(true); | |
channel.close(); | |
} | |
final long end = System.nanoTime(); | |
tempFile.toFile().delete(); | |
System.out.println(tempFile.toAbsolutePath() + " took " + ((end - start) / 1000 / 1e3) + " ms"); | |
} | |
public void classicio() throws Exception { | |
final Path tempFile = Files.createTempFile("test-classicio-", ".tmp"); | |
System.out.println(tempFile.toAbsolutePath()); | |
final File file = tempFile.toFile(); | |
final long start = System.nanoTime(); | |
try (final FileOutputStream out = new FileOutputStream(file)) { | |
final BufferedOutputStream bos = new BufferedOutputStream(out, 1 << 20); | |
long pos = 0; | |
final byte[] bs = "0123456789".getBytes("UTF-8"); | |
for (int k = 0; k < 2; k++) { | |
for (int i = 0; i < (size / bs.length); i++) { | |
bos.write(bs, 0, bs.length); | |
} | |
pos += size; | |
} | |
bos.flush(); | |
bos.close(); | |
} | |
final long end = System.nanoTime(); | |
tempFile.toFile().delete(); | |
System.out.println(tempFile.toAbsolutePath() + " took " + ((end - start) / 1000 / 1e3) + " ms"); | |
} | |
public void classicio7() throws Exception { | |
final Path tempFile = Files.createTempFile("test-classicio7-", ".tmp"); | |
System.out.println(tempFile.toAbsolutePath()); | |
final File file = tempFile.toFile(); | |
final long start = System.nanoTime(); | |
try (final OutputStream outputStream = Files.newOutputStream(file.toPath(), | |
StandardOpenOption.CREATE, StandardOpenOption.WRITE, | |
StandardOpenOption.TRUNCATE_EXISTING)) { | |
final BufferedOutputStream bos = new BufferedOutputStream(outputStream, 1 << 20); | |
long pos = 0; | |
final byte[] bs = "0123456789".getBytes("UTF-8"); | |
for (int k = 0; k < 2; k++) { | |
for (int i = 0; i < (size / bs.length); i++) { | |
bos.write(bs, 0, bs.length); | |
} | |
pos += size; | |
} | |
bos.flush(); | |
bos.close(); | |
} | |
final long end = System.nanoTime(); | |
tempFile.toFile().delete(); | |
System.out.println(tempFile.toAbsolutePath() + " took " + ((end - start) / 1000 / 1e3) + " ms"); | |
} | |
public static void main(String[] args) throws Exception { | |
final MappedTest mappedTest = new MappedTest(); | |
mappedTest.classicio(); | |
mappedTest.classicio7(); | |
mappedTest.mapped(); | |
mappedTest.channel(); | |
mappedTest.heapchannel(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment