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.TestCase;
22  
23  import org.yaml.snakeyaml.Invoice;
24  import org.yaml.snakeyaml.Util;
25  import org.yaml.snakeyaml.Yaml;
26  
27  /**
28   * Test that Yaml instances are independent and can be used in multiple threads.
29   */
30  public class ParallelTest extends TestCase {
31      private int progress = 0;
32      private int MAX = 5;
33  
34      public void testPerfomance() throws IOException {
35          String doc = Util.getLocalResource("specification/example2_27.yaml");
36          for (int i = 0; i < MAX; i++) {
37              Worker worker = new Worker(i, doc);
38              Thread thread = new Thread(worker);
39              thread.start();
40          }
41          while (progress < MAX - 1) {
42              try {
43                  Thread.sleep(1000);
44              } catch (InterruptedException e) {
45                  fail(e.getMessage());
46              }
47          }
48      }
49  
50      private class Worker implements Runnable {
51          private int id;
52          private String doc;
53  
54          public Worker(int id, String doc) {
55              this.id = id;
56              this.doc = doc;
57          }
58  
59          public void run() {
60              System.out.println("Started: " + id);
61              Yaml loader = new Yaml();
62              long time1 = System.nanoTime();
63              int cycles = 200;
64              for (int i = 0; i < cycles; i++) {
65                  Invoice invoice = loader.loadAs(doc, Invoice.class);
66                  assertNotNull(invoice);
67              }
68              long time2 = System.nanoTime();
69              float duration = ((time2 - time1) / 1000000) / (float) cycles;
70              System.out.println("Duration of " + id + " was " + duration + " ms/load.");
71              progress++;
72          }
73      }
74  }