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

cz.cuni.mff.d3s.spl.data.DataSnapshotBuilder 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
/*
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * 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 cz.cuni.mff.d3s.spl.data;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/** Helper class for creating data snapshots.
 *
 */
public class DataSnapshotBuilder {
	private List runs = new LinkedList<>();
	private DataSnapshot prevEpoch = null;
		
	public DataSnapshotBuilder() {
	}
	
	public synchronized DataSnapshot create() {
		return create(runs, prevEpoch);
	}

	public synchronized DataSnapshot create(int skip) {
		// skip 'skip' elements from the beginning
		List skippedRuns = new LinkedList<>();
		for (BenchmarkRun run : runs) {
			skippedRuns.add(new ImmutableBenchmarkRun(run, skip));
		}
		return create(skippedRuns, prevEpoch);
	}

	public synchronized DataSnapshot create(double skip) {
		// skip 'skip' percent of elements from the beginning
		List skippedRuns = new LinkedList<>();
		for (BenchmarkRun run : runs) {
			skippedRuns.add(new ImmutableBenchmarkRun(run, (int) (run.getSampleCount() * skip)));
		}
		return create(skippedRuns, prevEpoch);
	}
	
	public synchronized DataSnapshotBuilder setPreviousEpoch(DataSnapshot snapshot) {
		prevEpoch = snapshot;
		return this;
	}
	
	public synchronized DataSnapshotBuilder addRun(BenchmarkRun run) {
		runs.add(new ImmutableBenchmarkRun(run));
		return this;
	}

	private DataSnapshot create(List runs, DataSnapshot prevEpoch) {
		return new Snapshot(runs, prevEpoch);
	}
	
	private static class Snapshot implements DataSnapshot {
		private List runs;
		private DataSnapshot prevEpoch;
		
		public Snapshot(List data, DataSnapshot prev) {
			synchronized (data) {
				runs = new ArrayList<>(data.size());
				runs.addAll(data);
			}
			prevEpoch = prev;
		}

		@Override
		public int getRunCount() {
			return runs.size();
		}

		@Override
		public BenchmarkRun getRun(int index) {
			return runs.get(index);
		}

		@Override
		public Iterable getRuns() {
			return runs;
		}

		@Override
		public DataSnapshot getPreviousEpoch() {
			return prevEpoch;
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy