/**
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package rx.subjects;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import rx.Observer;

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
public class ReplaySubjectPerf {

    @State(Scope.Thread)
    public static class Input {
        @Param({ "1", "1000", "1000000" })
        public int nextRuns;
    }

    @Benchmark
    public void subscribeBeforeEvents(final Input input, final Blackhole bh) throws Exception {
        ReplaySubject<Object> subject = ReplaySubject.create();
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicLong sum = new AtomicLong();

        subject.subscribe(new Observer<Object>() {
            @Override
            public void onCompleted() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Object o) {
                sum.incrementAndGet();
            }
        });
        for (int i = 0; i < input.nextRuns; i++) {
            subject.onNext("Response");
        }
        subject.onCompleted();
        latch.await();
        bh.consume(sum);
    }

    @Benchmark
    public void subscribeAfterEvents(final Input input, final Blackhole bh) throws Exception {
        ReplaySubject<Object> subject = ReplaySubject.create();
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicLong sum = new AtomicLong();

        for (int i = 0; i < input.nextRuns; i++) {
            subject.onNext("Response");
        }
        subject.onCompleted();

        subject.subscribe(new Observer<Object>() {
            @Override
            public void onCompleted() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(Object o) {
                sum.incrementAndGet();
            }
        });
        latch.await();
        bh.consume(sum);
    }

}

@@ -783,20 +783,16 @@
 enchmark
-Mode
 ;%0Aimport
@@ -821,32 +821,36 @@
 ations.Benchmark
+Mode
 ;%0Aimport org.ope
@@ -1420,24 +1420,33 @@
 BeforeEvents
+Unbounded
 (final Input
@@ -1504,63 +1504,391 @@
     
-ReplaySubject%3CObject%3E subject = ReplaySubject.create();
+subscribeBeforeEvents(ReplaySubject.create(), input, bh);%0A    %7D%0A%0A    @Benchmark%0A    public void subscribeBeforeEventsCount1(final Input input, final Blackhole bh) throws Exception %7B%0A        subscribeBeforeEvents(ReplaySubject.create(1), input, bh);%0A    %7D%0A%0A    private void subscribeBeforeEvents(ReplaySubject%3CObject%3E subject, final Input input, final Blackhole bh) throws Exception %7B
 %0A   
@@ -2609,16 +2609,25 @@
 erEvents
+Unbounded
 (final I
@@ -2689,63 +2689,387 @@
     
-ReplaySubject%3CObject%3E subject = ReplaySubject.create();
+subscribeAfterEvents(ReplaySubject.create(), input, bh);%0A    %7D%0A%0A    @Benchmark%0A    public void subscribeAfterEventsCount1(final Input input, final Blackhole bh) throws Exception %7B%0A        subscribeAfterEvents(ReplaySubject.create(1), input, bh);%0A    %7D%0A%0A    private void subscribeAfterEvents(ReplaySubject%3CObject%3E subject, final Input input, final Blackhole bh) throws Exception %7B
 %0A   

Add perf tests for cache(1) variant