tests/libjava-mauve/src/gnu/testlet/org/omg/CORBA/ORB/parallelRunTest.java
branchjk_new_structure
changeset 1541 75c2e24dea9a
parent 1540 92ac284961c1
child 1542 be11db817bcf
equal deleted inserted replaced
1540:92ac284961c1 1541:75c2e24dea9a
     1 // Tags: JDK1.2
       
     2 
       
     3 // Copyright (C) 2005 Audrius Meskauskas (AudriusA@Bioinformatics.org)
       
     4 
       
     5 // Mauve is free software; you can redistribute it and/or modify
       
     6 // it under the terms of the GNU General Public License as published by
       
     7 // the Free Software Foundation; either version 2, or (at your option)
       
     8 // any later version.
       
     9 
       
    10 // Mauve is distributed in the hope that it will be useful,
       
    11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 // GNU General Public License for more details.
       
    14 
       
    15 // You should have received a copy of the GNU General Public License
       
    16 // along with Mauve; see the file COPYING.  If not, write to
       
    17 // the Free Software Foundation, 59 Temple Place - Suite 330,
       
    18 // Boston, MA 02111-1307, USA.  */
       
    19 
       
    20 package gnu.testlet.org.omg.CORBA.ORB;
       
    21 
       
    22 import gnu.testlet.TestHarness;
       
    23 import gnu.testlet.Testlet;
       
    24 import gnu.testlet.org.omg.CORBA.ORB.Asynchron.assServer;
       
    25 
       
    26 import org.omg.CORBA.ORB;
       
    27 import org.omg.CORBA.Request;
       
    28 import org.omg.CORBA.TCKind;
       
    29 
       
    30 import java.util.BitSet;
       
    31 import java.util.Random;
       
    32 import gnu.testlet.org.omg.CORBA.Asserter;
       
    33 
       
    34 /**
       
    35  * This test checks if the server is able to handle parallel
       
    36  * submissions that are sent by
       
    37  * orb.send_multiple_requests_deferred. The server should handle
       
    38  * requests to the different objects in parallel threads.
       
    39  *
       
    40  * As the "task" is just to wait for the given duration,
       
    41  * it is possible to simulate a "distributed computing" on a single
       
    42  * processor machine and get the "acceleration factor". This
       
    43  * accelerator factor is checked for the minimal allowed value.
       
    44  *
       
    45  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
       
    46  */
       
    47 public class parallelRunTest
       
    48   extends Asserter implements Testlet
       
    49 {
       
    50   public void test(TestHarness harness)
       
    51   {
       
    52       h = harness;
       
    53 
       
    54       // 1. Testing the ORB methods.
       
    55       // Number of serving objects.
       
    56       int servers = 20;
       
    57 
       
    58       // Number of tasks to serve.
       
    59       int requests = 30;
       
    60 
       
    61       // Max tasks per object.
       
    62       int max_tasks = 2;
       
    63 
       
    64       String[] iors = assServer.start_server(servers);
       
    65 
       
    66       BitSet served = new BitSet();
       
    67 
       
    68       ORB orb = org.omg.CORBA.ORB.init(new String[ 0 ], null);
       
    69 
       
    70       // Make pause for the processes to start.
       
    71       try
       
    72         {
       
    73           Thread.sleep(500);
       
    74         }
       
    75       catch (InterruptedException ex)
       
    76         {
       
    77         }
       
    78 
       
    79       org.omg.CORBA.Object[] objects = new org.omg.CORBA.Object[ servers ];
       
    80 
       
    81       for (int i = 0; i < iors.length; i++)
       
    82         {
       
    83           objects [ i ] = orb.string_to_object(iors [ i ]);
       
    84         }
       
    85 
       
    86       int[] times = new int[ requests ];
       
    87       Random r = new Random();
       
    88 
       
    89       for (int i = 0; i < times.length; i++)
       
    90         {
       
    91           times [ i ] = r.nextInt(200) + 200;
       
    92           served.set(times [ i ]);
       
    93         }
       
    94 
       
    95       Request[] reqs = new Request[ requests ];
       
    96 
       
    97       // The server may limit number of the queued requests per
       
    98       // socket. We allow no more than 10 tasks per socket.
       
    99       int[] tasks = new int[ servers ];
       
   100       int rn;
       
   101 
       
   102       for (int i = 0; i < reqs.length; i++)
       
   103         {
       
   104           do
       
   105             {
       
   106               rn = r.nextInt(objects.length);
       
   107             }
       
   108           while (tasks [ rn ] > max_tasks);
       
   109           tasks [ rn ]++;
       
   110 
       
   111           // The object to handle the requrest is selected randomly.
       
   112           // The acceleration factor will be lower than the number
       
   113           // of the "parallelized objects". This is sufficient
       
   114           // as this is just a test, not a real distribute computing task.
       
   115           Request rq =
       
   116             objects [ rn ]._create_request(null, "sleep_and_return",
       
   117                                            orb.create_list(1), null
       
   118                                           );
       
   119 
       
   120           rq.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
       
   121           rq.add_in_arg().insert_long(times [ i ]);
       
   122           reqs [ i ] = rq;
       
   123         }
       
   124 
       
   125       long started = System.currentTimeMillis();
       
   126 
       
   127       orb.send_multiple_requests_deferred(reqs);
       
   128 
       
   129       // Be sure the execution has returned correctly.
       
   130       assertTrue("Hangs on orb.send_multiple_requests_defferred",
       
   131                  System.currentTimeMillis() < started + 199
       
   132                 );
       
   133       assertTrue("orb.send_multiple_requests_defferred:"+
       
   134                  "Cannot be ready immediately",
       
   135                  !orb.poll_next_response());
       
   136 
       
   137       for (int i = 0; i < reqs.length; i++)
       
   138         {
       
   139           try
       
   140             {
       
   141               Request a = orb.get_next_response();
       
   142               served.clear(a.result().value().extract_long());
       
   143             }
       
   144           catch (Exception ex)
       
   145             {
       
   146               ex.printStackTrace();
       
   147             }
       
   148         }
       
   149 
       
   150       // Check the acceleration factor.
       
   151       long done_in = System.currentTimeMillis() - started;
       
   152 
       
   153       long required = 0;
       
   154       for (int i = 0; i < times.length; i++)
       
   155         {
       
   156           required += times [ i ];
       
   157         }
       
   158 
       
   159       // Compute the acceleration factor * 100 %.
       
   160       int acceleration = (int) ((100 * required) / done_in);
       
   161 
       
   162       // With 20 virtual servers and the used algorithm, we should
       
   163       // expect at least five-fold acceleration.
       
   164       assertTrue("Parallel work is broken :" + acceleration, acceleration > 500);
       
   165 
       
   166       assertEquals("Not all tasks served", served.cardinality(), 0);
       
   167 
       
   168       // 2. Testing the Request methods.
       
   169       Request rq =
       
   170         objects [ 0 ]._create_request(null, "sleep_and_return",
       
   171                                       orb.create_list(1), null
       
   172                                      );
       
   173 
       
   174       rq.set_return_type(orb.get_primitive_tc(TCKind.tk_long));
       
   175 
       
   176       rq.add_in_arg().insert_long(200);
       
   177 
       
   178       long t = System.currentTimeMillis();
       
   179       rq.send_deferred();
       
   180 
       
   181       // Be sure the execution has returned correctly.
       
   182       assertTrue("Hangs on Request.send_defferred",
       
   183                  System.currentTimeMillis() < t + 199
       
   184                 );
       
   185       assertTrue("Request.send_defferred:cannot be ready immediately",
       
   186                  !rq.poll_response());
       
   187 
       
   188       try
       
   189         {
       
   190           Thread.sleep(300);
       
   191         }
       
   192       catch (InterruptedException ex)
       
   193         {
       
   194         }
       
   195 
       
   196       assertTrue("Request.send_defferred:Must be ready now",
       
   197                  rq.poll_response());
       
   198       assertEquals("Request.send_defferred:Result must be ready",
       
   199                  rq.result().value().extract_long(), 200);
       
   200 
       
   201       orb.shutdown(true);
       
   202   }
       
   203 
       
   204 }