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