/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package test;

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.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;


public class FormatterBenchmark {

    static final Integer i23 = 23;
    static final Object[] args = { "Bob", i23 };

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormat(Blackhole b) {
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
        b.consume(String.format("%s: %d ", "Age", 23));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatBoxed(Blackhole b) {
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
        b.consume(String.format("%s: %d ", "Age", i23));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatBoxedArray(Blackhole b) {
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
        b.consume(String.format("%s: %d ", args));
    }
    
    @Benchmark @BenchmarkMode(Mode.SingleShotTime) 
    @Fork(value = 10) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatSingleShot(Blackhole b) {
        b.consume(String.format("%s %d %s %d %s %d ", "", 1, "", 2, "", 3));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatHashCode(Blackhole b) {
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
        b.consume(String.format("%h", ""));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatHashCodeUpper(Blackhole b) {
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
        b.consume(String.format("%H", ""));
    }


    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatHashMulti(Blackhole b) {
        b.consume(String.format("%h %h %h %h %h %h", "a", "b", "c", 1, 2, 3));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatOctal(Blackhole b) {
        b.consume(String.format("%o %o %o %o %o %o", 1, 2, 3, 4, 5, 6));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatHex(Blackhole b) {
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
        b.consume(String.format("%x", 1));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatFloat(Blackhole b) {
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
        b.consume(String.format("%g", 1.0));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatChar(Blackhole b) {
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
        b.consume(String.format("%c", 'a'));
    }

    @Benchmark 
    @BenchmarkMode(Mode.Throughput) 
    @Fork(value = 1) 
    @Warmup(iterations=3)
    @Measurement(iterations=5) 
    @OutputTimeUnit(TimeUnit.MILLISECONDS)
    public void testStringFormatBoolean(Blackhole b) {
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
        b.consume(String.format("%b", true));
    }
}


