Advertisement
Guest User

Untitled

a guest
Feb 4th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.36 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.io.RandomAccessFile;
  4. import java.nio.ByteBuffer;
  5. import java.nio.MappedByteBuffer;
  6. import java.nio.channels.FileChannel;
  7. import java.nio.channels.FileChannel.MapMode;
  8. import java.util.concurrent.TimeUnit;
  9.  
  10. import sun.nio.ch.DirectBuffer;
  11.  
  12.  
  13. public class SillyBenchmark {
  14.     static boolean syncPeriodically = false;
  15.     public static void main(String[] args) throws Exception {
  16.         System.out.println("Testing with sync at end");
  17.         tests();
  18.         syncPeriodically = true;
  19.         System.out.println("Testing with periodic syncing");
  20.         tests();
  21.  
  22.     }
  23.    
  24.     static void tests() throws Exception {
  25.         long size =  Integer.MAX_VALUE;
  26.        
  27.         File f = getFile(null);
  28.         try (FileOutputStream fos = new FileOutputStream(f)) {
  29.             measureChannel(fos.getChannel(), size, false);
  30.         } finally {
  31.             f.delete();
  32.         }
  33.        
  34.         f = getFile(size);
  35.         try (FileOutputStream fos = new FileOutputStream(f)) {
  36.             measureChannel(fos.getChannel(), size, true);
  37.         } finally {
  38.             f.delete();
  39.         }
  40.        
  41.         f = getFile(null);
  42.         try (RandomAccessFile fos = new RandomAccessFile(f, "rw")) {
  43.             MappedByteBuffer mbb = fos.getChannel().map(MapMode.READ_WRITE, 0, size);
  44.             try {
  45.                 measureMapped(mbb, false);
  46.             } finally {
  47.                 ((DirectBuffer)mbb).cleaner().clean();
  48.             }
  49.         } finally {
  50.             f.delete();
  51.         }
  52.        
  53.         f = getFile(size);
  54.         try (RandomAccessFile fos = new RandomAccessFile(f, "rw")) {
  55.             MappedByteBuffer mbb = fos.getChannel().map(MapMode.READ_WRITE, 0, size);
  56.             try {
  57.                 measureMapped(mbb, true);
  58.             } finally {
  59.                 ((DirectBuffer)mbb).cleaner().clean();
  60.             }       } finally {
  61.             f.delete();
  62.         }
  63.     }
  64.    
  65.     static void measureChannel(FileChannel fc, long size, boolean preallocated) throws Exception {
  66.         long start = System.nanoTime();
  67.  
  68.         ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024 * 2);
  69.        
  70.         long startSize = size;
  71.         int counter = 0;
  72.         while (startSize > 0) {
  73.             buf.clear();
  74.             startSize -= buf.remaining();
  75.             fc.write(buf);     
  76.            
  77.             counter++;
  78.             if (syncPeriodically && counter % 16 == 0) {
  79.                 fc.force(true);
  80.             }
  81.         }
  82.         fc.force(true);
  83.         long delta = System.nanoTime() - start;
  84.         System.out.println((preallocated ? "Preallocated " : "") +"Channel took " + TimeUnit.NANOSECONDS.toMillis(delta));
  85.     }
  86.    
  87.     static void measureMapped(MappedByteBuffer b, boolean preallocated) throws Exception {
  88.         long start = System.nanoTime();
  89.         ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024 * 2);
  90.         int counter = 0;
  91.  
  92.         while (b.hasRemaining()) {
  93.             buf.clear();
  94.             buf.limit(Math.min(b.remaining(), buf.remaining()));
  95.             b.put(buf);
  96.            
  97.             counter++;
  98.             if (syncPeriodically && counter % 16 == 0) {
  99.                 b.force();
  100.             }
  101.         }
  102.         b.force();
  103.         long delta = System.nanoTime() - start;
  104.         System.out.println((preallocated ? "Preallocated " : "") + "Mapped took " + TimeUnit.NANOSECONDS.toMillis(delta));
  105.     }
  106.    
  107.     static File getFile(Long size) throws Exception {
  108.         File f = File.createTempFile("bar", "foo");
  109. //      System.out.println("File path is " + f.getPath());
  110.         f.deleteOnExit();
  111.         if (size != null) {
  112.             try (FileOutputStream fos = new FileOutputStream(f)) {
  113.                 ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024);
  114.                 long start = size;
  115.                 while (start > 0) {
  116.                     buf.clear();
  117.                     start -= buf.remaining();
  118.                     fos.getChannel().write(buf);
  119.                 }
  120.                 fos.getChannel().force(false);
  121.             }
  122.         }
  123.         return f;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement