1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package test.com.sun.javafx.geom;
  24 
  25 import com.sun.javafx.geom.Path2D;
  26 import org.junit.Test;
  27 
  28 /**
  29  * @test
  30  * @bug 8169294
  31  * @summary Check the growth algorithm (needRoom) in JavaFX Path2D
  32  */
  33 /*
  34 Before Patch:
  35  - Test(Path2D[0]) ---
  36 testAddMoves[1000000] duration= 16.319813 ms.
  37 testAddLines[1000000] duration= 1685.904265 ms.
  38 testAddQuads[1000000] duration= 6435.015055999999 ms.
  39 testAddCubics[1000000] duration= 14643.259248999999 ms.
  40 testAddMoveAndCloses[1000000] duration= 2269.6810179999998 ms.
  41 
  42  - Test(Path2D) ---
  43 testAddMoves[1000000] duration= 4.645376 ms.
  44 testAddLines[1000000] duration= 1673.896613 ms.
  45 testAddQuads[1000000] duration= 6448.857066 ms.
  46 testAddCubics[1000000] duration= 14679.410602999998 ms.
  47 testAddMoveAndCloses[1000000] duration= 2278.352159 ms.
  48 
  49 After patch:
  50  - Test(Path2D[0]) ---
  51 testAddMoves[1000000] duration= 15.889125 ms.
  52 testAddLines[1000000] duration= 37.788070999999995 ms.
  53 testAddQuads[1000000] duration= 57.228248 ms.
  54 testAddCubics[1000000] duration= 62.25714 ms.
  55 testAddMoveAndCloses[1000000] duration= 41.76611 ms.
  56 
  57  - Test(Path2D) ---
  58 testAddMoves[1000000] duration= 15.857171999999998 ms.
  59 testAddLines[1000000] duration= 28.228354999999997 ms.
  60 testAddQuads[1000000] duration= 38.190948 ms.
  61 testAddCubics[1000000] duration= 52.453748999999995 ms.
  62 testAddMoveAndCloses[1000000] duration= 26.837844 ms.
  63  */
  64 public class Path2DGrowTest {
  65 
  66     public static final int N = 1000 * 1000;
  67 
  68     private static boolean verbose = false;
  69     private static boolean force = false;
  70 
  71     static void echo(String msg) {
  72         System.out.println(msg);
  73     }
  74 
  75     static void log(String msg) {
  76         if (verbose || force) {
  77             echo(msg);
  78         }
  79     }
  80 
  81     @Test
  82     public void testEmptyFloatPaths() {
  83         echo("\n - Test: new Path2D(0) ---");
  84         test(() -> new Path2D(Path2D.WIND_NON_ZERO, 0));
  85     }
  86 
  87     @Test
  88     public void testFloatPaths() {
  89         echo("\n - Test: new Path2D() ---");
  90         test(() -> new Path2D());
  91     }
  92 
  93     interface PathFactory {
  94         Path2D makePath();
  95     }
  96 
  97     static void test(PathFactory pf) {
  98         long start, end;
  99 
 100         for (int n = 1; n <= N; n *= 10) {
 101             force = (n == N);
 102 
 103             start = System.nanoTime();
 104             testAddMoves(pf.makePath(), n);
 105             end = System.nanoTime();
 106             log("testAddMoves[" + n + "] duration= "
 107                 + (1e-6 * (end - start)) + " ms.");
 108 
 109             start = System.nanoTime();
 110             testAddLines(pf.makePath(), n);
 111             end = System.nanoTime();
 112             log("testAddLines[" + n + "] duration= "
 113                 + (1e-6 * (end - start)) + " ms.");
 114 
 115             start = System.nanoTime();
 116             testAddQuads(pf.makePath(), n);
 117             end = System.nanoTime();
 118             log("testAddQuads[" + n + "] duration= "
 119                 + (1e-6 * (end - start)) + " ms.");
 120 
 121             start = System.nanoTime();
 122             testAddCubics(pf.makePath(), n);
 123             end = System.nanoTime();
 124             log("testAddCubics[" + n + "] duration= "
 125                 + (1e-6 * (end - start)) + " ms.");
 126 
 127             start = System.nanoTime();
 128             testAddMoveAndCloses(pf.makePath(), n);
 129             end = System.nanoTime();
 130             log("testAddMoveAndCloses[" + n + "] duration= "
 131                 + (1e-6 * (end - start)) + " ms.");
 132         }
 133     }
 134 
 135     static void addMove(Path2D p2d, int i) {
 136         p2d.moveTo(1.0f * i, 0.5f * i);
 137     }
 138 
 139     static void addLine(Path2D p2d, int i) {
 140         p2d.lineTo(1.1f * i, 2.3f * i);
 141     }
 142 
 143     static void addCubic(Path2D p2d, int i) {
 144         p2d.curveTo(1.1f * i, 1.2f * i, 1.3f * i, 1.4f * i, 1.5f * i, 1.6f * i);
 145     }
 146 
 147     static void addQuad(Path2D p2d, int i) {
 148         p2d.quadTo(1.1f * i, 1.2f * i, 1.3f * i, 1.4f * i);
 149     }
 150 
 151     static void addClose(Path2D p2d) {
 152         p2d.closePath();
 153     }
 154 
 155     static void testAddMoves(Path2D pathA, int n) {
 156         for (int i = 0; i < n; i++) {
 157             addMove(pathA, i);
 158         }
 159     }
 160 
 161     static void testAddLines(Path2D pathA, int n) {
 162         addMove(pathA, 0);
 163         for (int i = 0; i < n; i++) {
 164             addLine(pathA, i);
 165         }
 166     }
 167 
 168     static void testAddQuads(Path2D pathA, int n) {
 169         addMove(pathA, 0);
 170         for (int i = 0; i < n; i++) {
 171             addQuad(pathA, i);
 172         }
 173     }
 174 
 175     static void testAddCubics(Path2D pathA, int n) {
 176         addMove(pathA, 0);
 177         for (int i = 0; i < n; i++) {
 178             addCubic(pathA, i);
 179         }
 180     }
 181 
 182     static void testAddMoveAndCloses(Path2D pathA, int n) {
 183         for (int i = 0; i < n; i++) {
 184             addMove(pathA, i);
 185             addClose(pathA);
 186         }
 187     }
 188 }