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