1 package io.jawk.backend;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.PrintStream;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Map;
32 import io.jawk.intermediate.Opcode;
33
34
35
36
37
38 public final class ProfilingReport {
39
40 private final List<Entry> tupleEntries;
41 private final List<Entry> functionEntries;
42
43
44
45
46
47
48 public static ProfilingReport empty() {
49 return new ProfilingReport(
50 Collections.<Opcode, Accumulator>emptyMap(),
51 Collections.<String, Accumulator>emptyMap());
52 }
53
54 ProfilingReport(Map<Opcode, Accumulator> tupleStats, Map<String, Accumulator> functionStats) {
55 this.tupleEntries = Collections.unmodifiableList(toTupleEntries(tupleStats));
56 this.functionEntries = Collections.unmodifiableList(toFunctionEntries(functionStats));
57 }
58
59
60
61
62
63
64 public List<Entry> getTupleEntries() {
65 return tupleEntries;
66 }
67
68
69
70
71
72
73 public List<Entry> getFunctionEntries() {
74 return functionEntries;
75 }
76
77
78
79
80
81
82 public void print(PrintStream out) {
83 out.println("Jawk profiling report");
84 out.println();
85 printSection(out, "Tuple execution", tupleEntries);
86 out.println();
87 printSection(out, "Function execution", functionEntries);
88 }
89
90 private static void printSection(PrintStream out, String title, List<Entry> entries) {
91 out.println(title + ":");
92 if (entries.isEmpty()) {
93 out.println(" (none)");
94 return;
95 }
96 out.printf(Locale.ROOT, " %-32s %12s %14s %14s%n", "Name", "Count", "Time (ms)", "Avg (ns)");
97 for (Entry entry : entries) {
98 out.printf(
99 Locale.ROOT,
100 " %-32s %12d %14.3f %14.0f%n",
101 entry.getName(),
102 entry.getCount(),
103 entry.getTotalNanos() / 1_000_000.0d,
104 entry.getAverageNanos());
105 }
106 }
107
108 private static List<Entry> toTupleEntries(Map<Opcode, Accumulator> stats) {
109 List<Entry> entries = new ArrayList<Entry>(stats.size());
110 for (Map.Entry<Opcode, Accumulator> entry : stats.entrySet()) {
111 entries.add(new Entry(entry.getKey().name(), entry.getValue().count, entry.getValue().totalNanos));
112 }
113 sort(entries);
114 return entries;
115 }
116
117 private static List<Entry> toFunctionEntries(Map<String, Accumulator> stats) {
118 List<Entry> entries = new ArrayList<Entry>(stats.size());
119 for (Map.Entry<String, Accumulator> entry : stats.entrySet()) {
120 entries.add(new Entry(entry.getKey(), entry.getValue().count, entry.getValue().totalNanos));
121 }
122 sort(entries);
123 return entries;
124 }
125
126 private static void sort(List<Entry> entries) {
127 Collections.sort(
128 entries,
129 Comparator.comparingLong(Entry::getTotalNanos)
130 .reversed()
131 .thenComparing(Comparator.comparingLong(Entry::getCount).reversed())
132 .thenComparing(Entry::getName));
133 }
134
135 static final class Accumulator {
136 private long count;
137 private long totalNanos;
138
139 void add(long elapsedNanos) {
140 count++;
141 totalNanos += elapsedNanos;
142 }
143 }
144
145
146
147
148 public static final class Entry {
149 private final String name;
150 private final long count;
151 private final long totalNanos;
152
153 private Entry(String name, long count, long totalNanos) {
154 this.name = name;
155 this.count = count;
156 this.totalNanos = totalNanos;
157 }
158
159
160
161
162
163
164 public String getName() {
165 return name;
166 }
167
168
169
170
171
172
173 public long getCount() {
174 return count;
175 }
176
177
178
179
180
181
182 public long getTotalNanos() {
183 return totalNanos;
184 }
185
186
187
188
189
190
191 public double getAverageNanos() {
192 return count == 0 ? 0.0d : (double) totalNanos / count;
193 }
194 }
195 }