All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.cuni.mff.d3s.spl.data.readers.NumbersOnlyRevisionReader Maven / Gradle / Ivy

Go to download

Stochastice Performance Logic is a formalism for capturing performance assumptions. It is, for example, possible to capture assumption that newer version of a function bar is faster than the previous version or that library foobar is faster than library barfoo when rendering antialiased text. The purpose of this framework is to allow evaluation of SPL formulas inside Java applications.

There is a newer version: 1.0.4
Show newest version
package cz.cuni.mff.d3s.spl.data.readers;

import cz.cuni.mff.d3s.spl.data.*;
import cz.cuni.mff.d3s.spl.data.readers.DataReader.ReaderException;
import cz.cuni.mff.d3s.spl.utils.Factory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Reader for text files with one integer number per line.
 *
 * This reader expects multiple files (each is separate benchmark run)
 * with long numbers in binary form. There're no data about naming,
 * so it's expected that all data are from the same benchmark
 * and results are returned with the key "default".
 */
public class NumbersOnlyRevisionReader implements RevisionReader {
	/**
	 * Read one revision of data from given files. It's expected that
	 * each file is separate run and all data are from the same benchmark.
	 *
	 * @param files Input files with raw data
	 * @return Processed data with key "default"
	 * @throws ReaderException On reading or parsing error
	 */
	@Override
	public Map readRevision(File... files) throws ReaderException {
		DataSnapshotBuilder snapshotBuilder = new DataSnapshotBuilder();
		for (File file : files) {
			try {
				BenchmarkRun run = readNumbersOnlyData(new FileInputStream(file)).create();
				snapshotBuilder.addRun(run);
			} catch (FileNotFoundException e) {
				throw new ReaderException("File not found: " + e.getMessage());
			}
		}

		HashMap result = new HashMap<>();
		result.put(DataInfo.defaultInstance, new BuilderDataSource(snapshotBuilder));
		return result;
	}

	public static class RevisionFactory implements Factory {
		@Override
		public NumbersOnlyRevisionReader getInstance() {
			return new NumbersOnlyRevisionReader();
		}
	}

	/** Reads samples from a data stream.
	 *
	 * Expects samples as numbers of long type in input stream. Reading ends when
	 * there's no next number in the stream.
	 *
	 * @param is Input stream with data.
	 * @return Benchmark run with the samples.
	 * @throws ReaderException on reading failure.
	 */
	private BenchmarkRunBuilder readNumbersOnlyData(InputStream is) throws ReaderException {
		BenchmarkRunBuilder run = new BenchmarkRunBuilder();

		Scanner sc = new Scanner(is);
		while (sc.hasNextLong()) {
			run.addSamples(sc.nextLong());
		}
		sc.close();

		return run;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy