FileDocCategorySizeDatePackage
LoadBalancing.javaAPI DocExample9035Thu Mar 02 13:06:46 GMT 2000tuning.threads

LoadBalancing

public class LoadBalancing extends Object implements Runnable

Fields Summary
public static final boolean
DEBUG
static int
parallelCount
static long
parallelStartTime
static int
parallelThreadCount
String
url
String
localfilename
Constructors Summary
public LoadBalancing(String f, String u)

    localfilename = f;
    url = u;
  
Methods Summary
public static synchronized intdecThreadCount()

return --parallelThreadCount;
public static voiddowload(java.lang.String file, java.lang.String url)

    URL u = new URL(url);
    InputStream in = null;
    //Try repeatedly to get the page opened. Note that catching
    //all exceptions is not such a good idea here. It would be
    //much better to catch individual execption types and handle
    //them separately. Some exceptions should not lead to a repeated
    //attempt to access the page. But this definition is okay for testing.
    while(in == null)
      try{in = u.openStream();}
      catch(Exception e){try {Thread.sleep(500);}catch(Exception e2){}}
    FileOutputStream out = new FileOutputStream(file);
    byte[] buffer = new byte[8192];

    //read until the connection terminates (this is not a
    //keep-alive connection), and write to the file.
    int len = in.read(buffer);
    while(len != -1)
    {
      out.write(buffer, 0, len);
      len = in.read(buffer);
    }
    out.close();
    in.close();
  
public static java.lang.String[]getFileNames(java.lang.String directory)


      
  
//    String[] names = {
//      "c:\\temp\\01.txt", "http://news.bbc.co.uk"}
    String[] names = new String[1500];
    String file1 = "c:\\temp\\";
    String file2 = ".txt";
    int count = 1;
    String url = "http://localhost:" + SillyHttpServer.SERVER_PORT;
    for (int i = 1; i < names.length; i+=2, count++)
    {
      names[i-1] = file1 + count + file2;
      names[i] = url;
    }
    return names;
  
public static synchronized voidincCount(int i)

parallelCount += i;
public static voiditerativeTest(java.lang.String[] files)

    long time = System.currentTimeMillis();
    for (int i = 0; i < files.length; i+=2)
      dowload(files[i], files[i+1]);
    System.out.println("iterativeTest: " + (System.currentTimeMillis()-time));
  
public static voidloadBalancedTest(java.lang.String[] files, int numThreads)

    long time = System.currentTimeMillis();
    ActiveRequestQueue server = new ActiveRequestQueue(numThreads);
    for (int i = 0; i < files.length; i+=2)
    {
      server.acceptRequest(new Request(files[i], files[i+1]));
    }

    while(RequestResult.resultCount() < files.length/2)
      try{Thread.sleep(2000);}catch(InterruptedException e){}
        System.out.println("loadBalancedTest: " + (RequestResult.lastResultTime()-time));

    server.niceShutdown();
  
public static voidmain(java.lang.String[] args)

    try
    {
      int roughlyParallelTestDelay = Integer.parseInt(args[0]);
      int loadBalancedTestNumThreads = Integer.parseInt(args[1]);
      boolean runAllTests = args.length > 2;
      String[] files = getFileNames(args[1]);
      SillyHttpServer.serve();
      loadBalancedTest(files, loadBalancedTestNumThreads);
      if (runAllTests)
        massivelyParallelTest(files);
      roughlyParallelTest(files, roughlyParallelTestDelay);
      if (runAllTests)
        iterativeTest(files);

      SillyHttpServer.stop();
      System.exit(0);
    }
    catch (Exception e) {e.printStackTrace();}
  
public static voidmassivelyParallelTest(java.lang.String[] files)

    parallelStartTime = System.currentTimeMillis();
    parallelThreadCount = files.length/2;
    parallelCount = 0;
    for (int i = 0; i < files.length; i+=2)
      (new Thread(new LoadBalancing(files[i], files[i+1]))).start();

    //Wait for the threads to finish, so the next test doesn't
    //run until this one is fully finished.
    while(parallelThreadCount > 0)
      try{Thread.sleep(2000);}catch(InterruptedException e){}
  
public static voidroughlyParallelTest(java.lang.String[] files, int delay)

    parallelStartTime = System.currentTimeMillis();
    parallelThreadCount = files.length/2;
    parallelCount = 0;
    for (int i = 0; i < files.length; i+=2)
    {
      (new Thread(new LoadBalancing(files[i], files[i+1]))).start();
      try{Thread.sleep(delay);}catch(InterruptedException e){}
    }

    //Wait for the threads to finish, so the next test doesn't
    //run until this one is fully finished.
    while(parallelThreadCount > 0)
      try{Thread.sleep(2000);}catch(InterruptedException e){}
  
public voidrun()

      try
      {
        dowload(localfilename, url);
        incCount(0);
      }
      catch(Exception e) {e.printStackTrace();}
      if (decThreadCount() == 0)
        System.out.println("parallelTest: " + 
          (System.currentTimeMillis()-parallelStartTime));