package net.openhft.chronicle.queue;

import net.openhft.chronicle.wire.WireIn;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.function.Function;

import static org.junit.Assert.assertTrue;

public class ChronicleTest {

    public static final int RUNS = 2000000;

    @Test
    public void testCreateAppender() throws Exception {
        for (int t = 0; t < 10; t++) {
            String name = "/tmp/single" + System.nanoTime() + ".q";
            new File(name).deleteOnExit();
            Chronicle chronicle = new ChronicleQueueBuilder(name).build();
            long start = System.nanoTime();
            writeSome(chronicle);
            long mid = System.nanoTime();
            readSome(chronicle);
            long end = System.nanoTime();
            System.out.printf("Write time %,d - Read time %,d %n", (mid - start) / RUNS, (end - mid) / RUNS);
        }
    }

    private void readSome(Chronicle chronicle) throws IOException {
        ExcerptTailer tailer = chronicle.createTailer();
        StringBuilder sb = new StringBuilder();
        // TODO check this is still needed in future versions.
        Function<WireIn, WireIn> reader = wire -> wire.read().text(sb);
        for (int i = 0; i < RUNS; i++) {
            assertTrue(tailer.readDocument(reader));
        }
    }

    private void writeSome(Chronicle chronicle) throws IOException {
        ExcerptAppender appender = chronicle.createAppender();
        for (int i = 0; i < RUNS; i++) {
            appender.writeDocument(wire -> wire.write().text("Hello World"));
        }
    }
}
@@ -73,16 +73,59 @@
 WireIn;%0A
+import net.openhft.chronicle.wire.WireKey;%0A
 import o
@@ -189,16 +189,145 @@
 eption;%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.concurrent.ForkJoinPool;%0Aimport java.util.concurrent.Future;%0A
 import j
@@ -468,9 +468,9 @@
 S = 
-2
+1
 0000
@@ -532,32 +532,74 @@
 ows Exception %7B%0A
+        for (int r = 0; r %3C 2; r++) %7B%0A    
         for (int
@@ -607,17 +607,18 @@
 t = 
-0
+1
 ; t %3C
+=
  1
-0
+2
 ; t+
@@ -638,55 +638,214 @@
     
+ 
-String name = %22/tmp/single%22 + System.nanoTime()
+   List%3CFuture%3C?%3E%3E futureList = new ArrayList%3C%3E();%0A                long start = System.nanoTime();%0A                for (int j = 0; j %3C 4; j++) %7B%0A                    String name = %22single%22 + start + %22-%22 + j
  + %22
@@ -861,16 +861,24 @@
         
+  
+      
 new File
@@ -896,24 +896,32 @@
 teOnExit();%0A
+        
             
@@ -979,24 +979,25 @@
 e).build();%0A
+%0A
             
@@ -1000,40 +1000,80 @@
     
-long start = System.nanoTime();%0A
+        futureList.add(ForkJoinPool.commonPool().submit(() -%3E %7B%0A        
     
@@ -1080,16 +1080,20 @@
         
+    
 writeSom
@@ -1122,71 +1122,825 @@
     
-long mid = System.nanoTime();%0A            readSome(chronicle);%0A
+            return null;%0A                    %7D));%0A                %7D%0A                for (Future%3C?%3E future : futureList) %7B%0A                    future.get();%0A                %7D%0A                futureList.clear();%0A                long mid = System.nanoTime();%0A                for (int j = 0; j %3C 4; j++) %7B%0A                    String name = %22single%22 + start + %22-%22 + j + %22.q%22;%0A                    new File(name).deleteOnExit();%0A                    Chronicle chronicle = new ChronicleQueueBuilder(name).build();%0A%0A                    futureList.add(ForkJoinPool.commonPool().submit(() -%3E %7B%0A                        readSome(chronicle);%0A                        return null;%0A                    %7D));%0A                %7D%0A                for (Future%3C?%3E future : futureList) %7B%0A                    future.get();%0A                %7D%0A    
     
@@ -1989,16 +1989,20 @@
         
+    
 System.o
@@ -2016,86 +2016,147 @@
 tf(%22
-Write time %25,d - Read time %25,d %25n%22, (mid - start) / RUNS, (end - mid) / RUNS);
+Threads: %25,d - Write rate %25.1f M/s - Read rate %25.1f M/s%25n%22, t, t * RUNS * 1e3 / (mid - start), t * RUNS * 1e3 / (end - mid));%0A            %7D
 %0A   
@@ -2465,16 +2465,28 @@
 re.read(
+TestKey.test
 ).text(s
@@ -2827,16 +2827,28 @@
 e.write(
+TestKey.test
 ).text(%22
@@ -2862,22 +2862,109 @@
 orld
-%22));%0A        %7D
+23456789012345678901234567890%22));%0A        %7D%0A    %7D%0A%0A    enum TestKey implements WireKey %7B%0A        test
 %0A   

Fix a positioning bug.