source: josm/trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/RenderBenchmarkCollector.java@ 12620

Last change on this file since 12620 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm.visitor.paint;
3
4import java.io.PrintStream;
5import java.util.List;
6import java.util.function.Supplier;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer.StyleRecord;
10import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
11import org.openstreetmap.josm.tools.Logging;
12import org.openstreetmap.josm.tools.Utils;
13
14/**
15 * This class is notified of the various stages of a render pass.
16 *
17 * @author Michael Zangl
18 * @since 10697
19 */
20public class RenderBenchmarkCollector {
21 /**
22 * Notified when the renderer method starts preparing the data
23 * @param circum The current circum of the view.
24 */
25 public void renderStart(double circum) {
26 // nop
27 }
28
29 /**
30 * Notified when the renderer method starts sorting the styles
31 * @return <code>true</code> if the renderer should continue to render
32 */
33 public boolean renderSort() {
34 // nop
35 return true;
36 }
37
38 /**
39 * Notified when the renderer method starts drawing
40 * @param allStyleElems All the elements that are painted. Unsorted
41 * @return <code>true</code> if the renderer should continue to render
42 */
43 public boolean renderDraw(List<StyleRecord> allStyleElems) {
44 // nop
45 return true;
46 }
47
48 /**
49 * Notified when the render method is done.
50 */
51 public void renderDone() {
52 // nop
53 }
54
55 /**
56 * A benchmark implementation that captures the times
57 * @author Michael Zangl
58 */
59 public static class CapturingBenchmark extends RenderBenchmarkCollector {
60 protected long timeStart;
61 protected long timeGenerateDone;
62 protected long timeSortingDone;
63 protected long timeFinished;
64
65 @Override
66 public void renderStart(double circum) {
67 timeStart = System.currentTimeMillis();
68 super.renderStart(circum);
69 }
70
71 @Override
72 public boolean renderSort() {
73 timeGenerateDone = System.currentTimeMillis();
74 return super.renderSort();
75 }
76
77 @Override
78 public boolean renderDraw(List<StyleRecord> allStyleElems) {
79 timeSortingDone = System.currentTimeMillis();
80 return super.renderDraw(allStyleElems);
81 }
82
83 /**
84 * Get the time needed for generating the styles
85 * @return The time in ms
86 */
87 public long getGenerateTime() {
88 return timeGenerateDone - timeStart;
89 }
90
91 /**
92 * Get the time needed for computing the draw order
93 * @return The time in ms
94 */
95 public long getSortTime() {
96 return timeSortingDone - timeGenerateDone;
97 }
98
99 @Override
100 public void renderDone() {
101 timeFinished = System.currentTimeMillis();
102 super.renderDone();
103 }
104
105 /**
106 * Get the draw time
107 * @return The time in ms
108 */
109 public long getDrawTime() {
110 return timeFinished - timeGenerateDone;
111 }
112 }
113
114 /**
115 * A special version of the benchmark class that logs the output to stderr.
116 * @author Michael Zangl
117 */
118 public static class LoggingBenchmark extends RenderBenchmarkCollector.CapturingBenchmark {
119 private final PrintStream outStream = System.err;
120 private double circum;
121
122 @Override
123 public void renderStart(double circum) {
124 this.circum = circum;
125 super.renderStart(circum);
126 outStream.print("BENCHMARK: rendering ");
127 }
128
129 @Override
130 public boolean renderDraw(List<StyleRecord> allStyleElems) {
131 boolean res = super.renderDraw(allStyleElems);
132 outStream.print("phase 1 (calculate styles): " + Utils.getDurationString(timeSortingDone - timeStart));
133 return res;
134 }
135
136 @Override
137 public void renderDone() {
138 super.renderDone();
139 outStream.println("; phase 2 (draw): " + Utils.getDurationString(timeFinished - timeGenerateDone) +
140 "; total: " + Utils.getDurationString(timeFinished - timeStart) +
141 " (scale: " + circum + " zoom level: " + Selector.GeneralSelector.scale2level(circum) + ')');
142 }
143 }
144
145 /**
146 * A supplier that gets the default benchmark class.
147 * @return A supplier that returns a nop or a logging benchmark.
148 */
149 public static Supplier<RenderBenchmarkCollector> defaultBenchmarkSupplier() {
150 return () -> Logging.isTraceEnabled() || Main.pref.getBoolean("mappaint.render.benchmark", false)
151 ? new LoggingBenchmark() : new RenderBenchmarkCollector();
152 }
153}
Note: See TracBrowser for help on using the repository browser.