aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java
blob: e1b48fcba697388cfedf70fac27bd0c8305f34e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.batch.profiling;

import org.sonar.api.resources.Project;
import org.sonar.api.utils.TimeUtils;
import org.sonar.batch.phases.Phases;
import org.sonar.batch.phases.Phases.Phase;

import javax.annotation.CheckForNull;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ModuleProfiling extends AbstractTimeProfiling {

  private Map<Phases.Phase, PhaseProfiling> profilingPerPhase = new HashMap<Phases.Phase, PhaseProfiling>();
  private Clock clock;
  private Project module;

  public ModuleProfiling(@CheckForNull Project module, Clock clock) {
    super(clock);
    this.module = module;
    this.clock = clock;
  }

  public String moduleName() {
    if (module != null) {
      return module.getName();
    }
    return null;
  }

  public PhaseProfiling getProfilingPerPhase(Phase phase) {
    return profilingPerPhase.get(phase);
  }

  public void addPhaseProfiling(Phase phase) {
    profilingPerPhase.put(phase, PhaseProfiling.create(clock, phase));
  }

  public void dump() {
    double percent = this.totalTime() / 100.0;
    for (PhaseProfiling phaseProfiling : sortByDescendingTotalTime(profilingPerPhase.values())) {
      StringBuilder sb = new StringBuilder();
      sb.append(" * ").append(phaseProfiling.phase()).append(" execution time: ").append(phaseProfiling.totalTimeAsString())
          .append(" (").append((int) (phaseProfiling.totalTime() / percent)).append("%)");
      println(sb.toString());
    }
    for (Phase phase : Phases.Phase.values()) {
      if (profilingPerPhase.containsKey(phase)) {
        StringBuilder sb = new StringBuilder();
        sb.append("\n * ").append(phase).append(" execution time breakdown: ")
            .append(TimeUtils.formatDuration(getProfilingPerPhase(phase).totalTime()));
        println(sb.toString());
        getProfilingPerPhase(phase).dump();
      }
    }
  }

  public void merge(ModuleProfiling other) {
    super.add(other);
    for (Entry<Phases.Phase, PhaseProfiling> entry : other.profilingPerPhase.entrySet()) {
      if (!this.profilingPerPhase.containsKey(entry.getKey())) {
        this.addPhaseProfiling(entry.getKey());
      }
      this.getProfilingPerPhase(entry.getKey()).merge(entry.getValue());
    }
  }

}