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