package com.paritytrading.parity.match.perf;

import com.paritytrading.parity.match.Market;
import com.paritytrading.parity.match.MarketListener;
import com.paritytrading.parity.match.Side;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(value = 3, jvmArgsAppend = {"-XX:+UseParallelGC", "-Xms1G", "-Xmx1G"})
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.SampleTime)
public class MarketBenchmark {

    private Market market;

    private long sequence;

    @Setup(Level.Iteration)
    public void prepare() {
        MarketListener listener = new MarketListener() {
            public void match(long restingOrderId, long incomingOrderId, Side incomingSide, long price,
                    long executedQuantity, long remainingQuantity) {
            }
            public void add(long orderId, Side side, long price, long size) {
            }
            public void cancel(long orderId, long canceledQuantity, long remainingQuantity) {
            }
        };

        market = new Market(listener);

        sequence = 0;
    }

    @Benchmark
    public void enter() {
        market.enter(sequence++, Side.BUY, 34090, 100);
    }

    @Benchmark
    public void enterAndCancel() {
        long id = sequence++;

        market.enter(id, Side.BUY, 34090, 100);
        market.cancel(id, 0);
    }

}

@@ -1089,24 +1089,27 @@
 te long 
-sequence
+nextOrderId
 ;%0A%0A    @
@@ -1171,34 +1171,28 @@
     
-M
+m
 arket
-Listener listener = 
+ = new Market(
 new 
@@ -1205,24 +1205,47 @@
 Listener() %7B
+%0A%0A            @Override
 %0A           
@@ -1411,32 +1411,55 @@
  %7B%0A            %7D
+%0A%0A            @Override
 %0A            pub
@@ -1526,32 +1526,55 @@
  %7B%0A            %7D
+%0A%0A            @Override
 %0A            pub
@@ -1666,16 +1666,17 @@
       %7D%0A
+%0A
         
@@ -1676,16 +1676,17 @@
        %7D
+)
 ;%0A%0A     
@@ -1692,56 +1692,19 @@
     
-market = new Market(listener);%0A%0A        sequence
+nextOrderId
  = 0
@@ -1774,24 +1774,27 @@
 t.enter(
-sequence
+nextOrderId
 ++, Side
@@ -1791,36 +1791,37 @@
 Id++, Side.BUY, 
-3409
+10000
 0, 100);%0A    %7D%0A%0A
@@ -1887,21 +1887,29 @@
 ong 
-id = sequence
+orderId = nextOrderId
 ++;%0A
@@ -1930,17 +1930,22 @@
 t.enter(
-i
+orderI
 d, Side.
@@ -1953,12 +1953,13 @@
 UY, 
-3409
+10000
 0, 1
@@ -1985,17 +1985,22 @@
 .cancel(
-i
+orderI
 d, 0);%0A 

Clean up state setup in matching engine performance test