001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.profiling;
021
022 import org.sonar.api.utils.System2;
023 import org.sonar.api.utils.TimeUtils;
024
025 import javax.annotation.Nullable;
026
027 import java.util.*;
028
029 public abstract class AbstractTimeProfiling {
030
031 private final long startTime;
032
033 private long totalTime;
034
035 private System2 system;
036
037 public AbstractTimeProfiling(System2 system) {
038 this.system = system;
039 this.startTime = system.now();
040 }
041
042 protected System2 system() {
043 return system;
044 }
045
046 public long startTime() {
047 return startTime;
048 }
049
050 public void stop() {
051 this.totalTime = system.now() - startTime;
052 }
053
054 public long totalTime() {
055 return totalTime;
056 }
057
058 public String totalTimeAsString() {
059 return TimeUtils.formatDuration(totalTime);
060 }
061
062 public void setTotalTime(long totalTime) {
063 this.totalTime = totalTime;
064 }
065
066 protected void add(AbstractTimeProfiling other) {
067 this.setTotalTime(this.totalTime() + other.totalTime());
068 }
069
070 static <G extends AbstractTimeProfiling> Map<Object, G> sortByDescendingTotalTime(Map<?, G> unsorted) {
071 List<Map.Entry<?, G>> entries =
072 new ArrayList<Map.Entry<?, G>>(unsorted.entrySet());
073 Collections.sort(entries, new Comparator<Map.Entry<?, G>>() {
074 @Override
075 public int compare(Map.Entry<?, G> o1, Map.Entry<?, G> o2) {
076 return Long.valueOf(o2.getValue().totalTime()).compareTo(o1.getValue().totalTime());
077 }
078 });
079 Map<Object, G> sortedMap = new LinkedHashMap<Object, G>();
080 for (Map.Entry<?, G> entry : entries) {
081 sortedMap.put(entry.getKey(), entry.getValue());
082 }
083 return sortedMap;
084 }
085
086 static <G extends AbstractTimeProfiling> List<G> truncate(Collection<G> sortedList) {
087 int maxSize = 10;
088 List<G> result = new ArrayList<G>(maxSize);
089 int i = 0;
090 for (G item : sortedList) {
091 if (i >= maxSize || item.totalTime() == 0) {
092 return result;
093 }
094 i++;
095 result.add(item);
096 }
097 return result;
098 }
099
100 protected void println(String msg) {
101 PhasesSumUpTimeProfiler.println(msg);
102 }
103
104 protected void println(String text, @Nullable Double percent, AbstractTimeProfiling phaseProfiling) {
105 PhasesSumUpTimeProfiler.println(text, percent, phaseProfiling);
106 }
107
108 protected void println(String text, AbstractTimeProfiling phaseProfiling) {
109 println(text, null, phaseProfiling);
110 }
111
112 }