AwsHori.java Source code

Java tutorial

Introduction

Here is the source code for AwsHori.java

Source

/*
 * Copyright 2010-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.URL;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.model.Tag;

public class AwsHori {
    private static final long WAIT_CYCLE = 100 * 1000;
    private static final long SHORT_BREAK = 5 * 1000;

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

        // initialize/determine parameters
        String instanceType = "m3.medium";
        String lgAmi = "ami-8ac4e9e0";
        String dcAmi = "ami-349fbb5e";
        String bidPrice = "0.1";
        String securityGroup = "Project2";
        String lgDns = null; // load generator DNS
        String dcDns = null; // data center DNS
        String dashboardUrl = null; // the URL for dashboard
        String testId = null;
        int rps = 0; // total RPS

        // create a list of tags
        ArrayList<Tag> requestTags = new ArrayList<Tag>();
        requestTags.add(new Tag("Project", "2.1"));

        // create a security group
        SecurityGroup proj2 = new SecurityGroup(securityGroup);

        try {

            // create a load generator instance and return its DNS
            Requests requestsLg = new Requests(instanceType, lgAmi, bidPrice, securityGroup, requestTags);
            lgDns = requestsLg.submitRequests();

            // submit and start the test
            String submissionUrl = "http://" + lgDns
                    + "/password?passwd=0WSb4ufhYI7SkxfLWnnIWU0MC1NdcNKT&andrewId=jiabeip";
            sendGET(submissionUrl);

            // create the first data center instance and return its DNS
            Requests requestsDc1 = new Requests(instanceType, dcAmi, bidPrice, securityGroup, requestTags);
            dcDns = requestsDc1.submitRequests();

            // add the data center to the test and find our test ID
            String firstDataCenter = "http://" + lgDns + "/test/horizontal?dns=" + dcDns;
            testId = sendGET(firstDataCenter);
            System.out.println("Test ID is: " + testId);

            // get the dashboard URL
            dashboardUrl = "http://" + lgDns + "/log?name=test." + testId + ".log";

            // waiting period between instance creations
            Thread.sleep(WAIT_CYCLE);

            // create more data centers until RPS reaches 4000
            do {

                // create new instances 
                Requests requestsDcMore = new Requests(instanceType, dcAmi, bidPrice, securityGroup, requestTags);
                dcDns = requestsDcMore.submitRequests();

                // add new instance to the test
                String scaleOut = "http://" + lgDns + "/test/horizontal/add?dns=" + dcDns;
                sendGET(scaleOut);

                // get RPS from the dashboard
                rps = getRps(dashboardUrl);
                System.out.println("Current RPS: " + rps);

                Thread.sleep(WAIT_CYCLE);
            } while (rps < 4000);

            System.out.println("Test complete!");

        } catch (AmazonServiceException ase) {
            // Write out any exceptions that may have occurred.
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
    }

    /**
     * Send GET request
     * 
     * @param url
     *          the target url
     * @throws Exception
     */
    public static String sendGET(String url) throws Exception {

        int responseCode = 0;
        HttpURLConnection submitCon = null;
        do {

            try {

                // Send another request if the server returns error
                URL send = new URL(url);
                submitCon = (HttpURLConnection) send.openConnection();
                submitCon.setRequestMethod("GET");
                responseCode = submitCon.getResponseCode();
                System.out.println("Response: " + submitCon.getResponseMessage());
                Thread.sleep(SHORT_BREAK);

            } catch (ConnectException con) {
                con.printStackTrace();
                Thread.sleep(SHORT_BREAK);
                continue;
            } catch (Exception e) {
                e.printStackTrace();
                Thread.sleep(SHORT_BREAK);
                continue;
            }
        } while (responseCode != 200);
        System.out.println("Creation complete!");

        // Read and print log
        try (BufferedReader input = new BufferedReader(new InputStreamReader(submitCon.getInputStream()));) {
            String newLine;
            StringBuilder log = new StringBuilder();

            while ((newLine = input.readLine()) != null) {
                log.append(newLine);
            }

            // If the log contains test ID, get and return it
            if (log.indexOf("log?name=test") != -1) {
                Matcher testId = Pattern.compile("test\\.(\\d+)\\.log").matcher(log);
                if (testId.find())
                    return testId.group(1);
            }

            System.out.println("Log: " + log);
        } catch (IOException io) {
            io.printStackTrace();
        }

        return null;
    }

    /**
     * Calculate total RPS
     * 
     * @param url
     *          Target url
     * @return total RPS
     * @throws Exception
     */
    public static int getRps(String url) throws Exception {

        int rps = 0;

        // Set up connections
        URL send = new URL(url);
        HttpURLConnection submitCon = (HttpURLConnection) send.openConnection();
        submitCon.setRequestMethod("GET");
        System.out.println("Response: " + submitCon.getResponseMessage());

        // Find out current RPS and compare it with the requirement (4000)
        try (BufferedReader input = new BufferedReader(new InputStreamReader(submitCon.getInputStream()));) {

            String newLine;
            StringBuilder log = new StringBuilder();

            while ((newLine = input.readLine()) != null) {
                log.append(newLine);
            }

            // Remove the content before the last minute
            int delete = 0;
            Matcher afterLastMinute = Pattern.compile("\\[Minute \\d+\\]").matcher(log);
            while (afterLastMinute.find()) {
                delete = log.indexOf(afterLastMinute.group(0));
            }
            log = log.delete(0, delete);

            // If the test reaches the last minute, remove the content after the last
            // minute
            if (log.indexOf("[Load Generator]") != -1) {
                Matcher onlyLastMinute = Pattern.compile("\\[Load Generator\\]").matcher(log);
                if (onlyLastMinute.find()) {
                    delete = log.indexOf(onlyLastMinute.group(0));
                }
                log = log.delete(delete, log.length());
            }

            // Find all RPS within the last minute
            Matcher calculateRps = Pattern.compile("\\.amazonaws\\.com=(\\d+)").matcher(log);
            while (calculateRps.find()) {
                rps += Integer.parseInt(calculateRps.group(1));
            }

        } catch (IOException io) {
            io.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rps;
    }
}