001/*
002 * Copyright 2019-2021 M. Sean Gilligan.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package app.supernaut.test;
017
018import java.util.concurrent.ConcurrentLinkedQueue;
019
020/**
021 * Simple class for recording timestamps for benchmarking
022 */
023public class TimingMeasurements {
024    /** starting time */
025    public final long startTime = System.currentTimeMillis();
026    /** measurements data */
027    public final ConcurrentLinkedQueue<Measurement> measurements = new ConcurrentLinkedQueue<>();
028
029    /**
030     * Add a measurement
031     * @param desc description of the measurement
032     */
033    public void add(String desc) {
034        measurements.add(new Measurement(desc));
035    }
036
037    /**
038     * print all measurements
039     */
040    public void dump() {
041        measurements.forEach(this::printOne);
042    }
043
044    private void printOne(Measurement m) {
045        System.out.println(m.timestamp + " " + m.description);
046    }
047
048    /**
049     * One measurement
050     */
051    public class Measurement {
052        /** timestamp in milliseconds */
053        public final long timestamp;
054        /** measurement description */
055        public final String description;
056
057        /**
058         * Create a measurement with the given description and the current time
059         * @param desc measurement description
060         */
061        public Measurement(String desc) {
062            timestamp = System.currentTimeMillis() - startTime;
063            description = desc;
064        }
065    }
066}