001/**
002 * Copyright 2001 Sun Microsystems, Inc.
003 * 
004 * See the file "license.terms" for information on usage and
005 * redistribution of this file, and for a DISCLAIMER OF ALL 
006 * WARRANTIES.
007 */
008package com.sun.speech.freetts.util;
009
010import java.util.Collection;
011import java.util.LinkedHashMap;
012import java.util.Map;
013
014/**
015 * Provides a suite of timers that are used to collect and generate
016 * performance metrics for FreeTTS.
017 */
018public class BulkTimer {
019    /**
020     * A BulkTimer that can be used by classes that need to
021     * time their loading phase.
022     */
023    public final static BulkTimer LOAD = new BulkTimer();
024
025    private final static String SELF = "OverallTime";
026    private boolean verbose;
027    private Map<String, Timer> timers;
028    
029
030    /**
031     * Creates a bulk timer.
032     */
033    public BulkTimer() {
034        this.verbose = false;
035        timers = new LinkedHashMap<String, Timer>();
036    }
037
038    /**
039     * Starts the timer with the given name. A BulkTimer can manage
040     * any number of timers. The timers are referenced by name. A
041     * timer is created the first time it is referenced.
042     *
043     * @param name the name of the timer to start
044     */
045    public void start(String name) {
046        getTimer(name).start();
047    }
048
049    /**
050     * Stops the timer with the given name.
051     *
052     * @param name the name of the timer
053     */
054    public void stop(String name) {
055        getTimer(name).stop(verbose);
056    }
057
058    /**
059     * Starts the bulk timer.  The BulkTimer maintains a timer for
060     * itself (called SELF). This is used to measure the overall time
061     * for a bulk timer. When timing data is displayed, the percentage
062     * of total time is displayed. The total time is the time between
063     * <code> start </code> and <code> end </code>  calls on the
064     * <code> BulkTimer </code> .
065     */
066    public void start() {
067        getTimer(SELF).start();
068    }
069
070
071    /**
072     * Stops the bulk timer.
073     */
074    public void stop() {
075        getTimer(SELF).stop(verbose);
076    }
077
078    /**
079     * Sets verbose mode.
080     *
081     * @param verbose the verbose mode
082     */
083    public void setVerbose(boolean verbose) {
084        this.verbose = verbose;
085    }
086
087    /**
088     * Checks to see if we are in verbose mode.
089     *
090     * @return <code>true</code>  if verbose mode; otherwise
091     *     <code>false</code>.
092     */
093    public boolean isVerbose() {
094        return verbose;
095    }
096
097    /**
098     * Gets the timer with the given name.
099     *
100     * @param name the timer name
101     *
102     * @return the timer with that name
103     */
104    public Timer getTimer(String name) {
105        if (!timers.containsKey(name)) {
106            timers.put(name, new Timer(name));
107        }
108        return timers.get(name);
109    }
110
111    /**
112     * Shows all of the collected times.
113     *
114     * @param title the title for the display
115     */
116    public void show(String title) {
117        long overall = getTimer(SELF).getCurrentTime();
118        Collection<Timer>  values = timers.values();
119        Timer.showTimesShortTitle(title);
120        for (Timer timer : values) {
121            timer.showTimes(overall);
122        }
123    }
124}