<commit_before>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     <NAME> (<EMAIL>)
 *     
 */
package org.jenetics.performance;

import static java.util.FormattableFlags.LEFT_JUSTIFY;

import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formattable;
import java.util.Formatter;
import java.util.List;

import javax.measure.Measurable;
import javax.measure.Measure;
import javax.measure.quantity.Duration;
import javax.measure.unit.SI;

import org.jenetics.stat.Variance;
import org.jenetics.util.Accumulator;
import org.jenetics.util.Accumulators.MinMax;
import org.jenetics.util.Timer;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public abstract class PerfTest {
	
	private final String _group;
	
	private final List<Timer> _timers = new ArrayList<Timer>();
	private final List<Variance<Long>> _variances = new ArrayList<Variance<Long>>();
	private final List<MinMax<Long>> _minmax = new ArrayList<MinMax<Long>>();
	
	public PerfTest(final String group) {
		_group = group;
	}
	
	protected abstract int calls();
	
	protected Timer newTimer(final String name) {
		final Variance<Long> variance = new Variance<Long>();
		final MinMax<Long> minmax = new MinMax<Long>();
		_variances.add(variance);
		_minmax.add(minmax);
		
		final Timer timer = new Timer(name);
		timer.setAccumulator(new Accumulator<Long>() {
			@Override public void accumulate(final Long value) {
				variance.accumulate(value);
				minmax.accumulate(value);
			}
		});
		
		_timers.add(timer);
		
		return timer;
	}
	
	protected abstract PerfTest measure();	
	
	public void print(final PrintStream out) {
		out.print(this);
	}
	
	@Override
	public String toString() {
		final int[] columns = new int[]{37, 16, 16, 16}; 
		final String hhline = hline(columns, '=');
		final String hline = hline(columns, '-');
		
		final String header = String.format(
				"| %%-%ds | %%-%ds | %%-%ds | %%-%ds |",
				columns[0] - 2, columns[1] - 2, columns[2] - 2, columns[3] - 2
			);
		final String row = String.format(
				"| %%-%ds | %%%d.5s | %%%d.5s | %%%d.5s |",
				columns[0] - 2, columns[1] - 2, columns[2] - 2, columns[3] - 2
			);
		
		final StringBuilder out = new StringBuilder();
		out.append(hhline).append('\n');
		out.append(String.format(header, _group, "Mean", "Min", "Max")).append("\n");
		out.append(hhline).append('\n');
		
		for (int i = 0, n = _timers.size(); i < n; ++i) {
			final Timer timer = _timers.get(i);
			final Variance<Long> variance = _variances.get(i);
			final MinMax<Long> minmax = _minmax.get(i);
			
			out.append(String.format(
					row, 
					timer.getLabel(), 
					new FormattableDuration(variance.getMean()),
					new FormattableDuration(minmax.getMin().doubleValue()/calls()),
					new FormattableDuration(minmax.getMax().doubleValue()/calls())
				));
			out.append("\n");
			out.append(hline).append('\n');
		}
<commit_msg>Incomplete - # 25: Add reproducible performance tests https://sourceforge.net/apps/trac/jenetics/ticket/25<commit_after>/*
 * Java Genetic Algorithm Library (@!identifier!@).
 * Copyright (c) @!year!@ <NAME>
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author:
 *     <NAME> (<EMAIL>)
 *     
 */
package org.jenetics.performance;

import static java.util.FormattableFlags.LEFT_JUSTIFY;

import java.io.PrintStream;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formattable;
import java.util.Formatter;
import java.util.List;

import javax.measure.Measurable;
import javax.measure.Measure;
import javax.measure.quantity.Duration;
import javax.measure.unit.SI;

import org.jenetics.stat.Variance;
import org.jenetics.util.Accumulator;
import org.jenetics.util.Accumulators.MinMax;
import org.jenetics.util.Timer;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 */
public abstract class PerfTest {
	
	private final String _group;
	
	private final List<Timer> _timers = new ArrayList<Timer>();
	private final List<Variance<Long>> _variances = new ArrayList<Variance<Long>>();
	private final List<MinMax<Long>> _minmax = new ArrayList<MinMax<Long>>();
	
	public PerfTest(final String group) {
		_group = group;
	}
	
	protected abstract int calls();
	
	protected Timer newTimer(final String name) {
		final Variance<Long> variance = new Variance<Long>();
		final MinMax<Long> minmax = new MinMax<Long>();
		_variances.add(variance);
		_minmax.add(minmax);
		
		final Timer timer = new Timer(name);
		timer.setAccumulator(new Accumulator<Long>() {
			@Override public void accumulate(final Long value) {
				variance.accumulate(value);
				minmax.accumulate(value);
			}
		});
		
		_timers.add(timer);
		
		return timer;
	}
	
	protected abstract PerfTest measure();	
	
	public void print(final PrintStream out) {
		out.print(this);
	}
	
	@Override
	public String toString() {
		final int[] columns = new int[]{37, 16, 16, 16}; 
		final String hhline = hline(columns, '=');
		final String hline = hline(columns, '-');
		
		final String header = String.format(
				"| %%-%ds | %%-%ds | %%-%ds | %%-%ds |",
				columns[0] - 2, columns[1] - 2, columns[2] - 2, columns[3] - 2
			);
		final String row = String.format(
				"| %%-%ds | %%%d.5s | %%%d.5s | %%%d.5s |",
				columns[0] - 2, columns[1] - 2, columns[2] - 2, columns[3] - 2
			);
		
		final StringBuilder out = new StringBuilder();
		out.append(hhline).append('\n');
		out.append(String.format(header, _group, "Mean", "Min", "Max")).append("\n");
		out.append(hhline).append('\n');
		
		for (int i = 0, n = _timers.size(); i < n; ++i) {
			final Timer timer = _timers.get(i);
			final Variance<Long> variance = _variances.get(i);
			final MinMax<Long> minmax = _minmax.get(i);
			
			out.append(String.format(
					row, 
					timer.getLabel(), 
					new FormattableDuration(variance.getMean()),
					new FormattableDuration(minmax.getMin()),
					new FormattableDuration(minmax.getMax())
				));
			out.append("\n");
			out.append(hline).append('\n');
		}

{'fraction_non_alphanumeric': 0.13957197926359166, 'fraction_numerical': 0.01143160973016084, 'mean_word_length': 3.873056994818653, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 51, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3746305', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2379, 'n_words': 698}