Created
May 16, 2014 20:27
-
-
Save torao/47d8e6e6dea2ff3a50bf to your computer and use it in GitHub Desktop.
Which faster SSD or HDD using gathering write and scattering read?
This file contains hidden or 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
| import java.nio.*; | |
| import java.nio.channels.*; | |
| import java.nio.file.*; | |
| import java.util.*; | |
| import java.util.function.*; | |
| import java.util.stream.*; | |
| import java.io.*; | |
| public class GatherWriteScatterRead { | |
| public static void main(String[] args) throws Exception { | |
| Random r = new Random(); | |
| Path path = FileSystems.getDefault().getPath("sample"); | |
| byte[] rbuf = new byte[1024 * 1024]; | |
| ByteBuffer[] buffers = new ByteBuffer[256]; | |
| for(int i=0; i<buffers.length; i++){ | |
| buffers[i] = ByteBuffer.allocateDirect(rbuf.length); | |
| while(buffers[i].remaining() > 0){ | |
| r.nextBytes(rbuf); | |
| buffers[i].put(rbuf); | |
| } | |
| buffers[i].flip(); | |
| } | |
| for(int i=0; i<=10; i++){ | |
| path.toFile().delete(); | |
| Stream.of(buffers).forEach( b -> b.clear() ); | |
| double gtw = write(path, fc -> { | |
| try{ fc.write(buffers); }catch(Exception e){ e.printStackTrace(); } | |
| }); | |
| verify(path, buffers); | |
| path.toFile().delete(); | |
| Stream.of(buffers).forEach( b -> b.clear() ); | |
| double sqw = write(path, fc -> { | |
| try{ | |
| for(int j=0; j<buffers.length; j++){ | |
| fc.write(buffers[j]); | |
| } | |
| }catch(Exception e){ e.printStackTrace(); } | |
| }); | |
| verify(path, buffers); | |
| Stream.of(buffers).forEach( b -> b.clear() ); | |
| double scr = read(path, fc -> { | |
| try{ | |
| fc.read(buffers); | |
| }catch(Exception e){ e.printStackTrace(); } | |
| }); | |
| verify(path, buffers); | |
| Stream.of(buffers).forEach( b -> b.clear() ); | |
| double sqr = read(path, fc -> { | |
| try{ | |
| for(int j=0; j<buffers.length; j++){ | |
| fc.read(buffers[j]); | |
| } | |
| }catch(Exception e){ e.printStackTrace(); } | |
| }); | |
| verify(path, buffers); | |
| if(i > 0){ | |
| System.out.printf("%.3f\t%.3f\t%.3f\t%.3f%n", gtw, sqw, scr, sqr); | |
| } | |
| } | |
| } | |
| private static double read(Path path, Consumer<FileChannel> reader) throws IOException { | |
| FileChannel fc = FileChannel.open(path, StandardOpenOption.READ); | |
| long t0 = System.nanoTime(); | |
| reader.accept(fc); | |
| long t1 = System.nanoTime(); | |
| fc.close(); | |
| return (t1 - t0) / 1000.0 / 1000.0; | |
| } | |
| private static double write(Path path, Consumer<FileChannel> writer) throws IOException { | |
| FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE, StandardOpenOption.WRITE); | |
| long t0 = System.nanoTime(); | |
| writer.accept(fc); | |
| long t1 = System.nanoTime(); | |
| fc.close(); | |
| return (t1 - t0) / 1000.0 / 1000.0; | |
| } | |
| private static void verify(Path path, ByteBuffer[] buffers) throws IOException { | |
| long size = Stream.of(buffers).mapToLong( b -> b.capacity() ).sum(); | |
| if(path.toFile().length() != size) throw new IllegalStateException(); | |
| InputStream in = new BufferedInputStream(new FileInputStream(path.toFile())); | |
| Stream.of(buffers).forEach( b -> { | |
| b.clear(); | |
| try { | |
| while(b.remaining() > 0){ | |
| int b0 = b.get() & 0xFF; | |
| int b1 = in.read(); | |
| if(b0 != b1) throw new IllegalStateException(); | |
| } | |
| } catch(IOException ex){ throw new UncheckedIOException(ex); } | |
| }); | |
| in.close(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment