Java program – Load balancing simulation

by Nideesh C on April 29, 2011 · 0 comments

in Java Programming




public class LoadBalance {
    public static void main(String[] args) {

        int M = Integer.parseInt(args[0]);
        int N = Integer.parseInt(args[1]);
        int S = Integer.parseInt(args[2]);

        // Create server queues.
        RandomQueue<Queue<Integer>> servers = new RandomQueue<Queue<Integer>>();

        for (int i = 0; i < M; i++) {
            servers.enqueue(new Queue<Integer>());
        }

        // Assign an item to a server
        for (int j = 0; j < N; j++) {

            // Pick a random server, update if new min.
            Queue<Integer> min = servers.sample();
            for (int k = 1; k < S; k++) {
                Queue<Integer> q = servers.sample();
                if (q.length() < min.length()) min = q;
            }

            // min is the shortest server queue
            min.enqueue(j);
        }

        int i = 0;
        double[] lengths = new double[M];
        for (Queue<Integer> q: servers) {
            lengths[i++] = q.length();
            StdDraw.setYscale(0, 2.0*N/M);
            StdStats.plotBars(lengths);
        }
    }
}


/*************************************************************************
 *  Execution:    java LoadBalance M N S
 *  Dependencies: Queue.java RandomQueue.java StdDraw.java StdStats.java
 *
 *  Simulate the process of assignment N items to a set of M servers.
 *  Requests are put on the shortest of a sample of S queues chosen
 *  at random.
 *
 *************************************************************************/

 

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Java program – Sampling without replacement
  2. Java program – Gambler’s ruin simulation
  3. Java program – Albers squares
  4. Java program – Coupon collector simulation
  5. Java program – Data analysis library

Leave a Comment

Previous post:

Next post: