Last active
August 29, 2015 14:05
-
-
Save hastebrot/9968fefd47180b7ee831 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
onPulse() | |
onRendered() | |
event duration: 0 ms | |
pulse duration: 992 ms | |
pulse count: 1 | |
----- | |
onEvent() | |
onPulse() | |
onRendered() | |
event duration: 629 ms | |
pulse duration: 601 ms | |
pulse count: 28 | |
----- | |
onEvent() | |
onPulse() | |
onRendered() | |
event duration: 434 ms | |
pulse duration: 422 ms | |
pulse count: 27 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import javafx.application.Application; | |
import javafx.geometry.Point2D; | |
import javafx.geometry.Pos; | |
import javafx.scene.CacheHint; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.Region; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Ellipse; | |
import javafx.scene.transform.Scale; | |
import javafx.scene.transform.Transform; | |
import javafx.scene.transform.Translate; | |
import javafx.stage.Stage; | |
import com.google.common.base.Stopwatch; | |
import com.sun.javafx.perf.PerformanceTracker; | |
public class RenderPerformanceApp extends Application { | |
private StackPane container; | |
private StackPane viewport; | |
private Scene scene; | |
private Region eventRegion; | |
private StackPane vectorLayerPane; | |
private Group vectorLayer; | |
private Translate translate; | |
private Scale scale; | |
private PerformanceTracker sceneTracker; | |
private Stopwatch eventStopwatch; | |
private Stopwatch pulseStopwatch; | |
private AtomicInteger pulseCount; | |
@Override | |
public void start(Stage stage) throws Exception { | |
initContainer(); | |
initViewport(); | |
initScene(stage); | |
initVectorLayer(); | |
stage.setOnShown((event) -> { | |
initPerformanceTracker(); | |
initVectorLayerShapes(); | |
}); | |
stage.show(); | |
} | |
private void initContainer() { | |
container = new StackPane(); | |
container.setAlignment(Pos.CENTER); | |
} | |
private void initViewport() { | |
vectorLayerPane = new StackPane(); | |
eventRegion = new Region(); | |
viewport = new StackPane(); | |
viewport.setPrefSize(400, 400); | |
viewport.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE); | |
viewport.setStyle("-fx-border-color: gray; -fx-border-style: segments(2, 2);"); | |
viewport.getChildren().addAll(vectorLayerPane, eventRegion); | |
container.getChildren().addAll(viewport); | |
} | |
private void initScene(Stage stage) { | |
scene = new Scene(container, 500, 500); | |
stage.setScene(scene); | |
} | |
private void initVectorLayer() { | |
vectorLayer = new Group(); | |
vectorLayer.setCache(true); | |
vectorLayer.setCacheHint(CacheHint.QUALITY); | |
vectorLayer.setManaged(false); | |
translate = Transform.translate(0, 0); | |
scale = Transform.scale(1, 1); | |
vectorLayer.getTransforms().addAll(translate, scale); | |
vectorLayerPane.getChildren().add(vectorLayer); | |
} | |
private void initVectorLayerShapes() { | |
int numberOfShapes = 10000; | |
for (int shapeIndex = 0; shapeIndex < numberOfShapes; shapeIndex++) { | |
Point2D position = new Point2D( | |
Math.random() * viewport.getPrefWidth(), | |
Math.random() * viewport.getPrefHeight() | |
); | |
Ellipse ellipse = new Ellipse(position.getX(), position.getY(), 5, 5); | |
ellipse.setStroke(Color.BLACK); | |
ellipse.setStrokeWidth(1); | |
ellipse.setFill(Color.RED); | |
vectorLayer.getChildren().add(ellipse); | |
} | |
} | |
private void initPerformanceTracker() { | |
sceneTracker = PerformanceTracker.getSceneTracker(vectorLayer.getScene()); | |
eventStopwatch = Stopwatch.createUnstarted(); | |
pulseStopwatch = Stopwatch.createUnstarted(); | |
pulseCount = new AtomicInteger(0); | |
eventRegion.setOnMousePressed((event) -> { | |
onEvent(); | |
}); | |
sceneTracker.setOnPulse(() -> { | |
onPulse(); | |
}); | |
sceneTracker.setOnRenderedFrameTask(() -> { | |
onRendered(); | |
}); | |
} | |
private void onEvent() { | |
System.out.println("-----"); | |
System.out.println("onEvent()"); | |
eventStopwatch.reset().start(); | |
pulseStopwatch.reset(); | |
pulseCount.set(0); | |
translate.setX(translate.getX() == 0 ? 1 : 0); | |
translate.setY(translate.getY() == 0 ? 1 : 0); | |
scale.setX(scale.getX() == 1 ? 1.1 : 1); | |
scale.setY(scale.getY() == 1 ? 1.1 : 1); | |
} | |
private void onPulse() { | |
if (!pulseStopwatch.isRunning()) { | |
System.out.println("onPulse()"); | |
pulseStopwatch.start(); | |
} | |
pulseCount.incrementAndGet(); | |
} | |
private void onRendered() { | |
System.out.println("onRendered()"); | |
System.out.println(String.format("event duration: %s ms", | |
eventStopwatch.elapsed(TimeUnit.MILLISECONDS))); | |
System.out.println(String.format("pulse duration: %s ms", | |
pulseStopwatch.elapsed(TimeUnit.MILLISECONDS))); | |
System.out.println(String.format("pulse count: %s", | |
pulseCount.get())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment