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 org.opencms.main.CmsLog;
031
032import java.io.FileWriter;
033import java.io.IOException;
034import java.util.Properties;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Appends benchmark samples to a file.
040 */
041public class CmsFileBenchmarkReceiver implements CmsBenchmarkTable.Receiver {
042
043    /** Logger instance for this class. */
044    private static final Log LOG = CmsLog.getLog(CmsFileBenchmarkReceiver.class);
045
046    /** The target file path. */
047    private String m_path;
048
049    /** The benchmark group. */
050    private String m_group;
051
052    /** The benchmark name. */
053    private String m_benchmark;
054
055    /**
056     * Creates a new instance and configures it via the Java system properties.<p>
057     *
058     * The following properties are used:
059     * <ul>
060     * <li>opencms.benchmark.file: the output file
061     * <li>opencms.benchmark.name: the benchmark name
062     * <li>opencms.benchmark.group: the benchmark group
063     * </ul>
064     */
065    public CmsFileBenchmarkReceiver() {
066
067        Properties prop = System.getProperties();
068        String prefix = "opencms.benchmark.";
069        m_path = prop.getProperty(prefix + "file");
070        m_benchmark = prop.getProperty(prefix + "name");
071        m_group = prop.getProperty(prefix + "group");
072    }
073
074    /**
075     * Creates a new instance.
076     *
077     * @param path the output file path
078     * @param benchmark the benchmark name
079     * @param group the benchmark group
080     */
081    public CmsFileBenchmarkReceiver(String path, String benchmark, String group) {
082
083        m_path = path;
084        m_group = group;
085        m_benchmark = benchmark;
086    }
087
088    /**
089     * @see org.opencms.util.benchmark.CmsBenchmarkTable.Receiver#receiveSample(java.lang.String, long)
090     */
091    public synchronized void receiveSample(String sampleName, long sampleTime) {
092
093        try (FileWriter fw = new FileWriter(m_path, true)) {
094            String line = System.currentTimeMillis()
095                + " "
096                + m_benchmark
097                + " "
098                + m_group
099                + " "
100                + sampleName
101                + " "
102                + sampleTime
103                + "\n";
104            fw.write(line);
105        } catch (IOException e) {
106            LOG.error(e.getLocalizedMessage(), e);
107            e.printStackTrace();
108        }
109
110    }
111
112}