001/* 002 * JKNIV, utils - Helper utilities for jdk code. 003 * 004 * Copyright (C) 2017, the original author or authors. 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software Foundation, Inc., 018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 019 */ 020package net.sf.jkniv.experimental; 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.Collections; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029 030/** 031 * Multi-thread chronometer for multiple tags. 032 * 033 * @author Alisson Gomes 034 * @since 0.6.0 035 * 036 */ 037public class Chronometer 038{ 039 private static final Map<String, Chrono> chronos = new HashMap<String, Chrono>(); 040 private static int padLeft = 0; 041 private static final Chrono EMPTY_CHRONO = new Chrono(""); 042 043 public synchronized static void timer(String name) 044 { 045 if (name.length() > padLeft) 046 padLeft = name.length() + 1; 047 Chrono chrono = chronos.get(name); 048 if (chrono == null) 049 { 050 chrono = new Chrono(name); 051 chronos.put(name, chrono); 052 } 053 chrono.changeTimer(false); 054 } 055 056 public static Chrono pause(String name) 057 { 058 Chrono chrono = chronos.get(name); 059 if (chrono != null) 060 { 061 chrono.changeTimer(true); 062 return chrono; 063 } 064 return EMPTY_CHRONO; 065 } 066 067 public static Chrono remove(String name) 068 { 069 Chrono chrono = chronos.remove(name); 070 chrono.changeTimer(true); 071 return chrono; 072 } 073 074 public static String getTimer(String name) 075 { 076 Chrono chrono = chronos.get(name); 077 if (chrono != null) 078 return chrono.toString(); 079 080 return null; 081 } 082 083 public static Chrono getChronoTimer(String name) 084 { 085 return chronos.get(name); 086 } 087 088 public static long milliseconds(String name) 089 { 090 Chrono chrono = chronos.get(name); 091 if (chrono != null) 092 return chrono.time(); 093 094 return 0L; 095 } 096 097 public static double avg(String name) 098 { 099 Chrono chrono = chronos.get(name); 100 if (chrono != null) 101 return chrono.avg(); 102 103 return 0D; 104 } 105 106 public static long min(String name) 107 { 108 Chrono chrono = chronos.get(name); 109 if (chrono != null) 110 return chrono.min(); 111 112 return 0L; 113 } 114 115 public static long max(String name) 116 { 117 Chrono chrono = chronos.get(name); 118 if (chrono != null) 119 return chrono.max(); 120 121 return 0L; 122 } 123 124 public static String log() 125 { 126 StringBuilder sb = new StringBuilder(); 127 List<Chrono> list = new ArrayList<Chrono>(chronos.values()); 128 Collections.sort(list); 129 for (Chrono c : list) 130 sb.append("\n" + String.format("%1$" + padLeft + "s", c.toString())); 131 return sb.toString(); 132 } 133 134 public static void clear() 135 { 136 Collection<Chrono> list = chronos.values(); 137 for (Chrono c : list) 138 c.clear(); 139 140 } 141}