/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.xml;

import java.io.ByteArrayOutputStream;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
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 org.jenetics.DoubleChromosome;
import org.jenetics.DoubleGene;
import org.jenetics.Genotype;
import org.jenetics.util.IO;
import org.jenetics.xml.stream.AutoCloseableXMLStreamWriter;
import org.jenetics.xml.stream.Writer;
import org.jenetics.xml.stream.XML;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public class GenotypesXMLPerf {

	final Genotype<DoubleGene> gt = Genotype
		.of(DoubleChromosome.of(0.0, 1.0, 100), 10000);

	final Writer<Genotype<DoubleGene>> writer = Writers.Genotype
		.writer(Writers.DoubleChromosome.writer());

	@Benchmark
	public Object object() throws Exception {
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		IO.object.write(gt, out);

		return out.toByteArray();
	}

	@Benchmark
	public Object jaxb() throws Exception {
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		IO.jaxb.write(gt, out);

		return out.toByteArray();
	}

	@Benchmark
	public Object stream() throws Exception {
		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		try (AutoCloseableXMLStreamWriter xml = XML.writer(out)) {
			writer.write(gt, xml);
		}

		return out.toByteArray();
	}

	public static void main(String[] args) throws RunnerException {
		final Options opt = new OptionsBuilder()
			.include(".*" + GenotypesXMLPerf.class.getSimpleName() + ".*")
			.warmupIterations(10)
			.measurementIterations(15)
			.threads(1)
			.forks(1)
			.build();

		new Runner(opt).run();
	}

}

@@ -930,24 +930,66 @@
 chmarkMode;%0A
+import org.openjdk.jmh.annotations.Level;%0A
 import org.o
@@ -1103,21 +1103,105 @@
 tations.
-Scope
+Param;%0Aimport org.openjdk.jmh.annotations.Scope;%0Aimport org.openjdk.jmh.annotations.Setup
 ;%0Aimport
@@ -1915,13 +1915,12 @@
 nit.
-MILLI
+NANO
 SECO
@@ -1960,209 +1960,442 @@
 f %7B%0A
+%09
 %0A%09
-final Genotype%3CDoubleGene%3E gt = Genotype%0A%09%09.of(DoubleChromosome.of(0.0, 1.0
+public final Writer%3CGenotype%3CDoubleGene%3E%3E writer = Writers.Genotype%0A%09%09.writer(Writers.DoubleChromosome.writer());%0A%0A%09@State(Scope.Thread)%0A%09public static class IOState %7B%0A%0A%09%09@Param(%7B%221%22, %2210%22, %22100%22, %221000%22
 , 
+%22
 100
-)
+00%22
 , 
+%22
 10000
-);%0A%0A%09final Writer%3CGenotype%3CDoubleGene%3E%3E writer = Writers.Genotype%0A%09%09.writer(Writers.DoubleChromosome.writer());
+0%22%7D)%0A%09%09public int chromosomeCount;%0A%0A%09%09public Genotype%3CDoubleGene%3E genotype;%0A%0A%09%09@Setup%0A%09%09public void setup() %7B%0A%09%09%09genotype = Genotype.of(%0A%09%09%09%09DoubleChromosome.of(0.0, 1.0, 100),%0A%09%09%09%09chromosomeCount%0A%09%09%09);%0A%09%09%7D%0A%0A%09%7D%0A
 %0A%0A%09@
@@ -2426,16 +2426,35 @@
  object(
+final IOState state
 ) throws
@@ -2545,34 +2545,46 @@
 bject.write(
-gt
+state.genotype
 , out);%0A
 %0A%09%09return ou
@@ -2563,33 +2563,32 @@
 genotype, out);%0A
-%0A
 %09%09return out.toB
@@ -2635,16 +2635,35 @@
 ct jaxb(
+final IOState state
 ) throws
@@ -2756,18 +2756,30 @@
 b.write(
-gt
+state.genotype
 , out);%0A
@@ -2845,16 +2845,35 @@
  stream(
+final IOState state
 ) throws
@@ -3031,10 +3031,22 @@
 ite(
-gt
+state.genotype
 , xm

Optimize JMH test.