View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.yaml.snakeyaml.stress;
    18
    19  import java.io.IOException;
    20
    21  import junit.framework.Test;
    22  import junit.framework.TestCase;
    23  import junit.framework.TestSuite;
    24
    25  import org.yaml.snakeyaml.Invoice;
    26  import org.yaml.snakeyaml.Util;
    27  import org.yaml.snakeyaml.Yaml;
    28
    29  public class StressEmitterTest extends TestCase {
    30
    31      public static void main(String args[]) {
    32          junit.textui.TestRunner.run(suite());
    33      }
    34
    35      public static Test suite() {
    36          return new TestSuite(StressEmitterTest.class);
    37      }
    38
    39      public void testPerformance() throws IOException {
    40          Yaml loader = new Yaml();
    41          Invoice invoice = loader.loadAs(Util.getLocalResource("specification/example2_27.yaml"),
    42                  Invoice.class);
    43          Yaml dumper = new Yaml();
    44          long time1 = System.nanoTime();
    45          dumper.dumpAsMap(invoice);
    46          long time2 = System.nanoTime();
    47          float duration = (time2 - time1) / 1000000;
    48          System.out.println("\nSingle dump was " + duration + " ms.");
    49
    50          int[] range = new int[] { 1000, 2000 /* , 8000 */};
    51          System.out.println("\nOne instance.");
    52          for (int number : range) {
    53              time1 = System.nanoTime();
    54              for (int i = 0; i < number; i++) {
    55                  dumper.dump(invoice);
    56              }
    57              time2 = System.nanoTime();
    58              duration = ((time2 - time1) / 1000000) / (float) number;
    59              System.out.println("Duration for r=" + number + " was " + duration + " ms/dump.");
    60              // cobertura may make it very slow
    61              if (duration > 3) {
    62                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
    63              }
    64          }
    65
    66          System.out.println("\nMany instances.");
    67          for (int number : range) {
    68              time1 = System.nanoTime();
    69              for (int i = 0; i < number; i++) {
    70                  dumper = new Yaml();
    71                  dumper.dumpAsMap(invoice);
    72              }
    73              time2 = System.nanoTime();
    74              duration = ((time2 - time1) / 1000000) / (float) number;
    75              System.out.println("Duration for r=" + number + " was " + duration + " ms/dump.");
    76              // cobertura may make it very slow
    77              if (duration > 3) {
    78                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
    79              }
    80              // assertTrue("duration=" + duration, duration < 3);
    81          }
    82      }
    83  }