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  import org.yaml.snakeyaml.constructor.Constructor;
29  
30  public class StressTest extends TestCase {
31      String doc;
32  
33      public static void main(String args[]) {
34          junit.textui.TestRunner.run(suite());
35      }
36  
37      public static Test suite() {
38          return new TestSuite(StressTest.class);
39      }
40  
41      public void setUp() throws IOException {
42          doc = Util.getLocalResource("specification/example2_27.yaml");
43      }
44  
45      public void testPerformance() throws IOException {
46          long time1 = System.nanoTime();
47          new Yaml(new Constructor(Invoice.class));
48          long time2 = System.nanoTime();
49          float duration = (time2 - time1) / 1000000;
50          System.out.println("Init was " + duration + " ms.");
51  
52          Yaml loader = new Yaml();
53          time1 = System.nanoTime();
54          loader.loadAs(doc, Invoice.class);
55          time2 = System.nanoTime();
56          duration = (time2 - time1) / 1000000;
57          System.out.println("\nSingle load was " + duration + " ms.");
58  
59          loader = new Yaml();
60          int[] range = new int[] { 1000, 2000 /* , 4000, 8000 */};
61          System.out.println("\nOne instance.");
62          for (int number : range) {
63              time1 = System.nanoTime();
64              for (int i = 0; i < number; i++) {
65                  loader.loadAs(doc, Invoice.class);
66              }
67              time2 = System.nanoTime();
68              duration = ((time2 - time1) / 1000000) / (float) number;
69              System.out.println("Duration for r=" + number + " was " + duration + " ms/load.");
70              // cobertura may make it very slow
71              if (duration > 3) {
72                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
73              }
74              // assertTrue("duration=" + duration, duration < 3);
75          }
76  
77          System.out.println("\nMany instances.");
78          for (int number : range) {
79              time1 = System.nanoTime();
80              for (int i = 0; i < number; i++) {
81                  loader = new Yaml();
82                  loader.loadAs(doc, Invoice.class);
83              }
84              time2 = System.nanoTime();
85              duration = ((time2 - time1) / 1000000) / (float) number;
86              System.out.println("Duration for r=" + number + " was " + duration + " ms/load.");
87              // cobertura may make it very slow
88              if (duration > 3) {
89                  System.err.println("!!!!!! Too long. Expected <1 but was " + duration);
90              }
91              // assertTrue("duration=" + duration, duration < 3);
92          }
93      }
94  }