Source code

    001/**
    002 * Copyright (c) 2004-2011 QOS.ch
    003 * All rights reserved.
    004 *
    005 * Permission is hereby granted, free  of charge, to any person obtaining
    006 * a  copy  of this  software  and  associated  documentation files  (the
    007 * "Software"), to  deal in  the Software without  restriction, including
    008 * without limitation  the rights to  use, copy, modify,  merge, publish,
    009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
    010 * permit persons to whom the Software  is furnished to do so, subject to
    011 * the following conditions:
    012 *
    013 * The  above  copyright  notice  and  this permission  notice  shall  be
    014 * included in all copies or substantial portions of the Software.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
    019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
    022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    023 *
    024 */
    025package org.slf4j.profiler;
    026
    027/**
    028 *
    029 * This demo illustrates usage of SLF4J profilers.
    030 *
    031 * <p>
    032 * We have been given the task of generating a large number, say N, of random
    033 * integers. We need to transform that array into a smaller array containing
    034 * only prime numbers. The new array has to be sorted.
    035 *
    036 * <p>
    037 * While tackling this problem, we would like to measure the time spent in each
    038 * subtask.
    039 *
    040 * <p>
    041 * A typical output for this demo would be:
    042 *
    043 * <pre>
    044   + Profiler [BASIC]
    045   |-- elapsed time                      [A]   213.186 milliseconds.
    046   |-- elapsed time                      [B]  2499.107 milliseconds.
    047   |-- elapsed time                  [OTHER]  3300.752 milliseconds.
    048   |-- Total                         [BASIC]  6014.161 milliseconds.
    049  </pre>
    050 *
    051 * @author Ceki Gulcu
    052 */
    053public class BasicProfilerDemo {
    054
    055    public static void main(String[] args) {
    056        // create a profiler called "BASIC"
    057        Profiler profiler = new Profiler("BASIC");
    058        profiler.start("A");
    059        doA();
    060
    061        profiler.start("B");
    062        doB();
    063
    064        profiler.start("OTHER");
    065        doOther();
    066        profiler.stop().print();
    067    }
    068
    069    static private void doA() {
    070        delay(200);
    071    }
    072
    073    static private void doB() {
    074        delay(2500);
    075    }
    076
    077    static private void doOther() {
    078        delay(3300);
    079    }
    080
    081    static private void delay(int millis) {
    082        try {
    083            Thread.sleep(millis);
    084        } catch (InterruptedException e) {
    085        }
    086    }
    087}