Newer
Older
democall3 / tests / StressTest.java
geoma48p on 20 Jul 2015 1 KB Started transition to JMS.

import java.util.ArrayList;
import java.util.List;
import network.MessageGenerator;
import network.sockets.RequestSender;

/**
 * Simulates everyone toggling their help status at the same time.
 *
 *
 * Tests concurrency, and ability to handle load.
 *
 */
public class StressTest {

   private StressTest() {
   }

   public static void main(final String[] args) throws InterruptedException {

      final RequestSender requestSender = new RequestSender("127.0.0.1");
      final MessageGenerator generator = new MessageGenerator();

      List<Thread> threads = new ArrayList<Thread>();

      for (int i = 0; i < 50; i++) {

         // using 2 to 30 since those are numbers that all lab layouts have
         for (int j = 2; j <= 31; j++) {

            final int x = j;

            Thread t = new Thread(new Runnable() {

               public void run() {
                  requestSender.sendRequest(generator.requestHelp(String.valueOf(x)));
               }
            });

            t.start();
            threads.add(t);

         }

         for (Thread thread : threads) {
            thread.join();
         }

         // give server a chance to sort things out, and so we can visually examine the map
         Thread.sleep(250);

         for (int j = 2; j <= 31; j++) {

            final int x = j;

            new Thread(new Runnable() {

               public void run() {
                  requestSender.sendRequest(generator.cancelHelp(String.valueOf(x)));
               }
            }).start();
         }

         // give server a chance to sort things out, and so we can visually examine the map
         Thread.sleep(250);
      }
   }
}