ie.aib.nbp.zosresttest.RunTest.java Source code

Java tutorial

Introduction

Here is the source code for ie.aib.nbp.zosresttest.RunTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.aib.nbp.zosresttest;

import ie.aib.nbp.zosconnect.ZosRestServicePerformanceRunner;
import ie.aib.nbp.zosconnect.ZosRestServiceDiscoverer;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author 58128
 */
public class RunTest {

    private static final String CONFIG_FILE_PATH = "config.properties";
    private static final String RUN_PARAM_FILE_PATH = "run.properties";
    private static Properties config;

    private static void readConfig(String paramFile) {
        Configurator configurator = new Configurator(paramFile);
        config = configurator.getConfig();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PrintStream printer = null;

        if (args.length < 2) {
            System.out.println("***************************************************************************");
            System.out.println("*                   Z/OS REST SERVICES PERFORMANCE TESTER                 *");
            System.out.println("*                                                                         *");
            System.out.println("* At least 2 run parameters are required:                                 *");
            System.out.println("*  1 username: your mainframe username                                    *");
            System.out.println("*  2 password: your mainframe passeord                                    *");
            System.out.println("*  3 runtime behaviour switch: 1 - active, everything else - inactive     *");
            System.out.println("*    this parameter is optional and causes the app to run the test or not *");
            System.out.println("*    lack of it assings default value: 0                                  *");
            System.out.println("*    which triggers the service discovery feature                         *");
            System.out.println("*    and displays all currently available services                        *");
            System.out.println("*  4 output type:                                                         *");
            System.out.println("*     F - output to file (then fifth parameter hold the file location)    *");
            System.out.println("*     Everything else (including no parameter at all) outputs to console  *");
            System.out.println("***************************************************************************");
            return;
        }

        readConfig(CONFIG_FILE_PATH);
        String username = args[0];
        String password = args[1];

        String runPerformanceTest;
        try {
            runPerformanceTest = args[2];
        } catch (ArrayIndexOutOfBoundsException ex) {
            runPerformanceTest = "0";
        }

        String outputType;
        String outputFileLocation;
        try {
            outputType = args[3]; // F saves to file, everything else output to console            
        } catch (ArrayIndexOutOfBoundsException ex) {
            outputType = "C";
        }

        if (outputType.equalsIgnoreCase("F")) { // write to file
            String outputFile;
            try {
                outputFile = args[4]; // if prev param is F then this one may contain the file name (default name instead)
            } catch (ArrayIndexOutOfBoundsException ex) {
                SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy-HHmmssZ");
                outputFile = "ZosPerformanceTest".concat("-".concat(sdf.format(new Date()))).concat(".txt");
            }
            try {
                String filePath = config.getProperty("OUT_FILE_PATH").concat(outputFile);
                printer = new PrintStream(new FileOutputStream(filePath, true));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(RunTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else { // othewise write to java console
            printer = new PrintStream(System.out);
        }

        if (runPerformanceTest.equals("1"))
            runPerformanceTest(username, password, printer, true, false);
        else
            runServiceDiscovery(username, password, printer);
    }

    // display all available services 
    private static void runServiceDiscovery(String username, String password, PrintStream out) {
        ZosRestServiceDiscoverer discoverer = new ZosRestServiceDiscoverer(config.getProperty("HOST"),
                Integer.parseInt(config.getProperty("PORT")), out);
        out.println("Discovering services ...");
        out.println("");
        JSONObject response = discoverer.getZosServices(config.getProperty("ZOSSERVICELIST"), username, password);
        if (response == null) {
            out.println("execution failed or hit the timeout !!!");
        } else {
            JSONArray services = response.getJSONArray("zosConnectServices");
            int i = 0;
            for (Object obj : services) {
                out.print(++i + ": ");
                JSONObject entry = (JSONObject) obj;
                out.println(entry);
            }
        }
    }

    // read test runtime properties and run the test
    private static void runPerformanceTest(String username, String password, PrintStream out,
            boolean attachResponse, boolean formatResponse) {
        ZosRestServicePerformanceRunner runner = new ZosRestServicePerformanceRunner(config.getProperty("HOST"),
                Integer.parseInt(config.getProperty("PORT")), out);
        try {
            readConfig(RUN_PARAM_FILE_PATH);
            String url = config.getProperty("URL");
            String payload = config.getProperty("PAYLOAD");
            int loops = Integer.parseInt(config.getProperty("LOOPS"));

            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
            out.println("z/OS performance test started at: " + sdf.format(new Date()));
            out.println(" ");
            List<Long> elapsedTimes = new ArrayList<>();
            for (int i = 0; i < loops; i++) {
                long elapsedTime = runner.executeServices(url, username, password, payload, out, attachResponse,
                        formatResponse);
                elapsedTimes.add(elapsedTime);
            }
            // calculating avg. time
            Long totalTime = 0L;
            for (Long time : elapsedTimes)
                totalTime = totalTime + time;
            Double avgTime = totalTime.doubleValue() / elapsedTimes.size();
            out.println("Average run time: " + avgTime + " miliseconds");
            out.println("");
            out.println("z/OS performance test ended at: " + sdf.format(new Date()));

        } catch (NumberFormatException ex) {
            out.println("Unable to run the test, elther the number of parameters or their values are incorrect.");
        }

    }
}