/*
 * Copyright 2017 Matt Sicker
 *
 * 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 org.musigma.logging.jmh;

import org.musigma.logging.layout.Layout;
import org.musigma.logging.impl.LogEvent;
import org.musigma.logging.layout.SimpleAsciiLayout;
import org.musigma.logging.layout.SimpleLayout;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 *
 */
@State(Scope.Benchmark)
public class LayoutBenchmark {

    private Layout simpleLayout = new SimpleLayout(StandardCharsets.ISO_8859_1);
    private Layout asciiLayout = new SimpleAsciiLayout();
    private LogEvent event = new LogEvent("Test message", System.currentTimeMillis());

    @State(Scope.Thread)
    public static class ReusableBuffer {
        private ByteBuffer buf = ByteBuffer.allocateDirect(8192);
    }

    @Benchmark
    public ByteBuffer simpleLayoutEncode1() {
        return simpleLayout.encode(event);
    }

    @Benchmark
    public void simpleLayoutEncode2(ReusableBuffer buffer) {
        simpleLayout.encode(event, buffer.buf);
        buffer.buf.clear();
    }

    @Benchmark
    public ByteBuffer asciiLayoutEncode1() {
        return asciiLayout.encode(event);
    }

    @Benchmark
    public void asciiLayoutEncode2(ReusableBuffer buffer) {
        asciiLayout.encode(event, buffer.buf);
        buffer.buf.clear();
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
            .include(LayoutBenchmark.class.getName())
            .warmupIterations(10)
            .measurementIterations(10)
            .forks(1)
            .build();

        new Runner(options).run();
    }
}

@@ -799,32 +799,74 @@
 t.SimpleLayout;%0A
+import org.musigma.logging.util.Buffered;%0A
 import org.openj
@@ -895,16 +895,16 @@
 chmark;%0A
-
 import o
@@ -1662,32 +1662,433 @@
 t(8192);%0A    %7D%0A%0A
+    @State(Scope.Thread)%0A    public static class ExtensibleBuffer implements Buffered%3CByteBuffer%3E %7B%0A        private ByteBuffer buf = ByteBuffer.allocateDirect(8192);%0A%0A        @Override%0A        public ByteBuffer buffer() %7B%0A            return buf;%0A        %7D%0A%0A        @Override%0A        public ByteBuffer drain() %7B%0A            buf.clear(); // nothing to drain to!%0A            return buf;%0A        %7D%0A    %7D%0A%0A
     @Benchmark%0A 
@@ -2564,32 +2564,32 @@
 t, buffer.buf);%0A
-
         buffer.b
@@ -2603,24 +2603,175 @@
 r();%0A    %7D%0A%0A
+    @Benchmark%0A    public void asciiLayoutEncode3(ExtensibleBuffer buffer) %7B%0A        asciiLayout.encode(event, buffer);%0A        buffer.drain();%0A    %7D%0A%0A
     public s

Add benchmark for new Layout encode method