package performance; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Random; import javafx.animation.AnimationTimer; import javafx.animation.Timeline; import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.stage.Stage; /** * JavaFX 2.0 version of the Fire test. * * @author bchristi */ public class FX_FireStatic extends Application { private static String[] mainArgs; private final Random random = new Random(0); private Image image1; public static final int DEFAULT_SIZE = 100; public static final int WINDOW_SIZE = 800; public static int NUMELEM = 50000; public static String TEST_TYPE = "move"; public static int DURATION = 20 * 1000; Stage stage; AnimationTimer PerfRunner; Timeline autoTL = new Timeline(); public FX_FireStatic() { } static int iter=0; static List images; void createImages() { images = new ArrayList(NUMELEM); for (int i = 1; i <= NUMELEM; i++) { images.add(createImageView()); } } Group rootGroup; public Stage createTestGUI(Stage primaryStage) { stage = new Stage(); String imagePath = "hawaii-palmtrees.jpg"; //java.net.URL url = ; InputStream in = FX_FireStatic.class.getClassLoader().getResourceAsStream(imagePath); image1 = new Image(this.getClass().getResource(imagePath).toString(), DEFAULT_SIZE, DEFAULT_SIZE, false, // preserve ratio true); // smooth if (image1.isError()) { throw new Error("Error loading image: " + imagePath); } System.out.println("Creating nodes before creating scene.."); createImages(); rootGroup = new Group(); rootGroup.getChildren().addAll(images); Scene scene = new Scene(rootGroup, WINDOW_SIZE, WINDOW_SIZE); // scene.setFill(Color.BLACK); stage.setScene(scene); stage.setX(0.0); stage.setY(0.0); stage.show(); PerfRunner = new AnimationTimer() { long lTime, sTime; int nFrames = -16, nTotal; static final long oneSecond = 1000000000; void updateFPS(long l) { if (++nFrames > 0) { nTotal ++; double fpsAvrg = oneSecond * (double)nTotal / (l - sTime); if (l - lTime > oneSecond) { double fps = oneSecond * (double)nFrames / (l - lTime); lTime = l; nFrames = 0; System.out.println(String.format("FPS: %.2f", fps) + ", averagePFS: " + String.format("%.2f", fpsAvrg)); } if (l - sTime > oneSecond*(DURATION/1000)) { String fpsText = String.format("TEST DONE: fps %.2f", fpsAvrg); System.out.println(fpsText); Platform.exit(); } } else { if (nFrames==-15) System.out.println("Warming up ..."); if (nFrames==0) System.out.println("Warmup done"); sTime = lTime = l; } } @Override public void handle(long l) { updateFPS(l); if (TEST_TYPE.equals("move")) { for (int i=0; i 0) { //System.out.println("Size=" + rootGroup.getChildren().size()); rootGroup.getChildren().clear(); } else { //createImages(); rootGroup.getChildren().addAll(images); } } //System.out.println("Iter: " + iter); iter ++; } }; return stage; } public void runTest() { System.out.println("System info:"); PerfRunner.start(); autoTL.play(); } // For benchmark mode public void stopTest() { PerfRunner.stop(); autoTL.stop(); } private ImageView createImageView() { double myRotationOffset = random.nextDouble() * 90; ImageView node = null; node = new ImageView(image1); ((ImageView)node).setX(random.nextDouble() * WINDOW_SIZE); ((ImageView)node).setY(random.nextDouble() * WINDOW_SIZE); return node; } @Override public void start(Stage stage) { parseOptions(mainArgs); createTestGUI(stage); runTest(); } public void parseOptions(String[] mainArgs) { } public static void main(String[] args) { for (int index = 0; index < args.length; index++) { String arg = args[index]; if ("-numelem".equals(arg)) { if (index+1 >= args.length) { System.err.println("-numelem option requires an integer argument"); System.exit(1); } try { NUMELEM = Integer.parseInt(args[index+1]); } catch (IllegalArgumentException e) { System.err.println("-numelem option must be integer]"); System.exit(1); } index++; } else if ("-dur".equals(arg)) { if (index+1 >= args.length) { System.err.println("-dur option requires an integer argument (seconds))"); System.exit(1); } try { DURATION = Integer.parseInt(args[index+1]) * 1000; } catch (IllegalArgumentException e) { System.err.println("-dur option must be integer]"); System.exit(1); } index++; } else if ("-type".equals(arg)) { if (index+1 >= args.length) { System.err.println("-type option requires an string argument: move, create"); System.exit(1); } TEST_TYPE = args[index+1]; index++; } } setGlobalOptions(); Application.launch(FX_FireStatic.class, args); } static public int statsFrequency = 100; private static final String[][] GLOBAL_SETTINGS = { { "prism.verbose", "true" }, { "javafx.animation.fullspeed", "true" }, { "javafx.vsync", "false" }, { "prism.printallocs", "true" }, { "prism.dirtyopts", "false" }, { "prism.printStats", Integer.toString(statsFrequency) }, }; public static void setGlobalOptions() { for (String [] sw : GLOBAL_SETTINGS) System.setProperty( sw[0], sw[1] ); } }