View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.stress;
17  
18  import junit.framework.Test;
19  import junit.framework.TestCase;
20  import junit.framework.TestSuite;
21  
22  import org.yaml.snakeyaml.Invoice;
23  import org.yaml.snakeyaml.Util;
24  import org.yaml.snakeyaml.Yaml;
25  
26  public class StressEmitterTest extends TestCase {
27  
28      public static void main(String args[]) {
29          junit.textui.TestRunner.run(suite());
30      }
31  
32      public static Test suite() {
33          return new TestSuite(StressEmitterTest.class);
34      }
35  
36      public void testPerformance() {
37          Yaml loader = new Yaml();
38          Invoice invoice = loader.loadAs(Util.getLocalResource("specification/example2_27.yaml"),
39                  Invoice.class);
40          Yaml dumper = new Yaml();
41          long time1 = System.nanoTime();
42          dumper.dumpAsMap(invoice);
43          long time2 = System.nanoTime();
44          float duration = (time2 - time1) / 1000000;
45          System.out.println("\nSingle dump was " + duration + " ms.");
46  
47          int[] range = new int[] { 1000, 2000 /* , 8000 */};
48          System.out.println("\nOne instance.");
49          for (int number : range) {
50              time1 = System.nanoTime();
51              for (int i = 0; i < number; i++) {
52                  dumper.dump(invoice);
53              }
54              time2 = System.nanoTime();
55              duration = ((time2 - time1) / 1000000) / (float) number;
56              System.out.println("Duration for r=" + number + " was " + duration + " ms/dump.");
57              // cobertura may make it very slow
58              if (duration > 3) {
59                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
60              }
61          }
62  
63          System.out.println("\nMany instances.");
64          for (int number : range) {
65              time1 = System.nanoTime();
66              for (int i = 0; i < number; i++) {
67                  dumper = new Yaml();
68                  dumper.dumpAsMap(invoice);
69              }
70              time2 = System.nanoTime();
71              duration = ((time2 - time1) / 1000000) / (float) number;
72              System.out.println("Duration for r=" + number + " was " + duration + " ms/dump.");
73              // cobertura may make it very slow
74              if (duration > 3) {
75                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
76              }
77              // assertTrue("duration=" + duration, duration < 3);
78          }
79      }
80  }