001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.util.benchmark;
029
030import java.util.concurrent.ConcurrentHashMap;
031
032/**
033 * Manages a set of benchmark timers.<p>
034 *
035 * Each benchmark timer is started with start(name) and stopped with stop(name). When stop() is called,
036 * the time between the start and stop calls for that name, in milliseconds, are sent to the configured benchmark
037 * receiver instance.
038 */
039public class CmsBenchmarkTable {
040
041    /**
042     * Handler for benchmark samples.
043     */
044    interface Receiver {
045
046        /**
047         * Processes a sample.
048         *
049         * @param sampleName the sample name
050         * @param sampleTime the sample time
051         */
052        void receiveSample(String sampleName, long sampleTime);
053    }
054
055    /** The benchmark sample receiver. */
056    private Receiver m_receiver;
057
058    /** Records start times for each benchmark timer. */
059    private ConcurrentHashMap<String, Long> m_startTimes = new ConcurrentHashMap<>();
060
061    /**
062     * Creates a new instance.
063     *
064     * @param receiver the benchmark receiver to use
065     */
066    public CmsBenchmarkTable(Receiver receiver) {
067
068        m_receiver = receiver;
069    }
070
071    /**
072     * Starts the timer with the given name.<p>
073     *
074     * The name is just an arbitrary string.
075     *
076     * @param name the name of the timer
077     */
078    public void start(String name) {
079
080        if (m_startTimes.containsKey(name)) {
081            throw new IllegalStateException("Can't start timer for given key twice: " + name);
082        }
083        m_startTimes.put(name, Long.valueOf(System.currentTimeMillis()));
084    }
085
086    /**
087     * Stops the timer with the given name, and sends the value of the timer to the benchmark receiver.
088     *
089     * @param name the name of the timer
090     */
091    public void stop(String name) {
092
093        if (!m_startTimes.containsKey(name)) {
094            throw new IllegalStateException("Can't stop a timer that wasn't started: " + name);
095        }
096        long duration = System.currentTimeMillis() - m_startTimes.get(name).longValue();
097        m_receiver.receiveSample(name, duration);
098        m_startTimes.remove(name);
099    }
100
101}