View Javadoc
1   package io.jawk.backend;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * Snapshot of tuple and function execution statistics collected by an
36   * {@link AVM}.
37   */
38  public final class ProfilingReport {
39  
40  	private final List<Entry> tupleEntries;
41  	private final List<Entry> functionEntries;
42  
43  	/**
44  	 * Returns an empty profiling report.
45  	 *
46  	 * @return empty report
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  	 * Returns tuple execution statistics sorted by descending total time.
61  	 *
62  	 * @return tuple execution entries
63  	 */
64  	public List<Entry> getTupleEntries() {
65  		return tupleEntries;
66  	}
67  
68  	/**
69  	 * Returns function execution statistics sorted by descending total time.
70  	 *
71  	 * @return function execution entries
72  	 */
73  	public List<Entry> getFunctionEntries() {
74  		return functionEntries;
75  	}
76  
77  	/**
78  	 * Prints this profiling report to the supplied stream.
79  	 *
80  	 * @param out destination stream
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 	 * One profiling table row.
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 		 * Returns the tuple type or function name.
161 		 *
162 		 * @return entry name
163 		 */
164 		public String getName() {
165 			return name;
166 		}
167 
168 		/**
169 		 * Returns the number of observed executions.
170 		 *
171 		 * @return execution count
172 		 */
173 		public long getCount() {
174 			return count;
175 		}
176 
177 		/**
178 		 * Returns the total elapsed time in nanoseconds.
179 		 *
180 		 * @return total elapsed nanoseconds
181 		 */
182 		public long getTotalNanos() {
183 			return totalNanos;
184 		}
185 
186 		/**
187 		 * Returns the average elapsed time in nanoseconds.
188 		 *
189 		 * @return average elapsed nanoseconds
190 		 */
191 		public double getAverageNanos() {
192 			return count == 0 ? 0.0d : (double) totalNanos / count;
193 		}
194 	}
195 }