Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 714e871700b210b9cc12e20db99eabcda9340de9 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*******************************************************************************
 * Copyright (c) 2009,2015 QNX Software Systems
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    QNX Software Systems (Alena Laskavaia)  - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.codan.internal.core;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Class to collect time states for checkers runs
 */
public class CheckersTimeStats {
	public static final String ALL = "ALL"; //$NON-NLS-1$
	public static final String ELAPSED = "ELAPSED"; //$NON-NLS-1$
	private static CheckersTimeStats instance = new CheckersTimeStats();
	private boolean enableStats = false;

	/**
	 * @return global instance of stats
	 */
	public static CheckersTimeStats getInstance() {
		return instance;
	}

	private static class TimeRecord {
		private long duration;
		private long current;
		private int count;

		public void start() {
			current = System.currentTimeMillis();
		}

		public void stop() {
			count++;
			duration += System.currentTimeMillis() - current;
			current = 0;
		}

		@Override
		public String toString() {
			return String.format("%4d %4d %4.2f", duration, count, count == 0 ? count : (duration / (float) count)); //$NON-NLS-1$
		}

		public String toString(long total) {
			float ave = count == 0 ? count : (duration / (float) count);
			float per = total == 0 ? 100f : (duration * 100 / (float) total);
			return String.format("%4d %4d %4.2f %4.2f%%", duration, count, ave, per); //$NON-NLS-1$
		}
	}
	private Map<String, TimeRecord> records = new HashMap<String, TimeRecord>();

	/**
	 * @param id - checker id
	 * @return
	 */
	private TimeRecord getTimeRecord(String id) {
		TimeRecord record = records.get(id);
		if (record == null) {
			record = new TimeRecord();
			records.put(id, record);
		}
		return record;
	}

	/**
	 * Start measuring elapsed time for checker with given id
	 *
	 * @param id
	 */
	public void checkerStart(String id) {
		checkerStart(id, ELAPSED);
	}

	/**
	 * Start measuring elapsed time for checker with given id and counter
	 *
	 * @param id
	 * @param counter
	 */
	public void checkerStart(String id, String counter) {
		if (enableStats) {
			TimeRecord record = getTimeRecord(getKey(id, counter));
			record.start();
		}
	}

	private String getKey(String id, String counter) {
		return id + ":" + counter; //$NON-NLS-1$
	}

	/**
	 * @param id
	 * @param counter
	 */
	public void checkerStop(String id, String counter) {
		if (enableStats) {
			getTimeRecord(getKey(id, counter)).stop();
		}
	}

	/**
	 * @param id
	 */
	public void checkerStop(String id) {
		checkerStop(id, ELAPSED);
	}

	/**
	 * Print checker stats to stdout if tracing enabled
	 */
	public void traceStats() {
		if (enableStats) {
			printStats();
		}
	}

	/**
	 *
	 */
	public void printStats() {
		System.out.println("---"); //$NON-NLS-1$
		String totalId = getKey(ALL, ELAPSED);
		TimeRecord all = records.get(totalId);
		for (Iterator<String> iterator = records.keySet().iterator(); iterator.hasNext();) {
			String id = iterator.next();
			if (id.equals(totalId))
				continue;
			TimeRecord timeRecord = getTimeRecord(id);
			System.out.println(timeRecord.toString(all.duration) + " " + id); //$NON-NLS-1$
		}
		System.out.println(all.toString() + " " + totalId); //$NON-NLS-1$
	}

	/**
	 *
	 */
	public void reset() {
		records.clear();
	}

	/**
	 * @return true is stats collection is enabled
	 */
	public boolean isEnabled() {
		return enableStats;
	}

	/**
	 * @param set to true to enable stats collection and false to disable
	 */
	public void setEnabled(boolean enableStats) {
		this.enableStats = enableStats;
	}
}

Back to the top