Java tutorial
/* Copyright 1995-2015 Esri Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License 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. For additional information, contact: Environmental Systems Research Institute, Inc. Attn: Contracts Dept 380 New York Street Redlands, California, USA 92373 email: contracts@esri.com */ package com.esri.geoevent.test.tools; import java.io.BufferedReader; import java.io.FileReader; import java.io.OutputStream; import java.net.Socket; import java.text.DecimalFormat; import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; import org.apache.commons.cli.BasicParser; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; /** * * @author davi5017 */ public class RunTcpInBdsOutTest { private Double send_rate; private Double rcv_rate; public void send(String server, Integer port, Long numEvents, Integer rate, String data_file) { BufferedReader br = null; ArrayList<String> lines = new ArrayList<>(); LocalDateTime st = null; try { // Read the file into String array br = new BufferedReader(new FileReader(data_file)); String line = null; while ((line = br.readLine()) != null) { lines.add(line); } Socket sckt = new Socket(server, port); OutputStream os = sckt.getOutputStream(); Integer cnt = 0; st = LocalDateTime.now(); Double ns_delay = 1000000000.0 / (double) rate; long ns = ns_delay.longValue(); if (ns < 0) { ns = 0; } int i = 0; int j = 0; while (i < numEvents) { i++; j++; if (j >= lines.size()) { j = 0; } line = lines.get(j) + "\n"; final long stime = System.nanoTime(); long etime = 0; do { etime = System.nanoTime(); } while (stime + ns >= etime); os.write(line.getBytes()); os.flush(); } LocalDateTime et = LocalDateTime.now(); if (st != null) { et = LocalDateTime.now(); Duration delta = Duration.between(st, et); Double elapsed_seconds = (double) delta.getSeconds() + delta.getNano() / 1000000000.0; send_rate = (double) numEvents / elapsed_seconds; } sckt.close(); os = null; } catch (Exception e) { System.err.println(e.getMessage()); send_rate = -1.0; } finally { try { br.close(); } catch (Exception e) { // } this.send_rate = send_rate; } } private void runTest(long numberEvents, int rate, String server, int in_port, String data_file, String msLayerUrl, Boolean isSingle) { this.send_rate = -1.0; this.rcv_rate = -1.0; try { GetMapServiceCountRunnable reader = new GetMapServiceCountRunnable(numberEvents, msLayerUrl, isSingle); Thread readerThread = new Thread(reader); readerThread.start(); // Give the consumer a couple of seconds to start Thread.sleep(2000); this.send(server, in_port, numberEvents, rate, data_file); readerThread.join(); rcv_rate = reader.getAverage_read_per_second(); if (reader.getNum_events_read() != numberEvents) { // Number of events read differs from send throw new Exception("Number of Events received (" + Long.toString(reader.getNum_events_read()) + ") did not match what was sent (" + Long.toString(numberEvents) + ") !"); } } catch (Exception e) { System.out.println(e.getMessage()); rcv_rate = -1.0; } finally { this.rcv_rate = rcv_rate; } } public static void main(String args[]) { // Example Command line args //-n 10000 -g w12ags104a.jennings.home -i 5565 -m https://w12ags104a.jennings.home/arcgis/rest/services/Hosted/FAA-Stream/MapServer/0 -f D:\github\performance-test-harness-for-geoevent\app\simulations\faa-stream.csv -r 1000,3000,1000 int numberEvents = 10000; // Number of Events String gisServer = "w12ags104a.jennings.home"; // GIS Server int inputTcpPort = 5565; // TCP Input Port String msLayerUrl = "http://w12ags104a.jennings.home/arcgis/rest/services/Hosted/FAA-Stream/MapServer/0"; String EventsInputFile = "D:\\github\\performance-test-harness-for-geoevent\\app\\simulations\\faa-stream.csv"; // Events input File int start_rate = 5000; int end_rate = 5005; int rate_step = 1; Options opts = new Options(); opts.addOption("n", true, "Number of Events"); opts.addOption("g", true, "GIS Server"); opts.addOption("i", true, "Input TCP Port"); opts.addOption("m", true, "Map Service Layer URL"); opts.addOption("f", true, "File with GeoEvents to Send"); opts.addOption("r", true, "Rates to test Start,End,Step"); opts.addOption("h", false, "Help"); try { CommandLineParser parser = new BasicParser(); CommandLine cmd = null; try { cmd = parser.parse(opts, args, false); } catch (org.apache.commons.cli.ParseException ignore) { System.err.println(ignore.getMessage()); } String cmdInputErrorMsg = ""; if (cmd.getOptions().length == 0 || cmd.hasOption("h")) { throw new org.apache.commons.cli.ParseException("Show Help"); } if (cmd.hasOption("n")) { String val = cmd.getOptionValue("n"); try { numberEvents = Integer.valueOf(val); } catch (NumberFormatException e) { cmdInputErrorMsg += "Invalid value for n. Must be integer.\n"; } } if (cmd.hasOption("g")) { gisServer = cmd.getOptionValue("g"); } if (cmd.hasOption("i")) { String val = cmd.getOptionValue("i"); try { inputTcpPort = Integer.valueOf(val); } catch (NumberFormatException e) { cmdInputErrorMsg += "Invalid value for i. Must be integer.\n"; } } if (cmd.hasOption("m")) { msLayerUrl = cmd.getOptionValue("m"); } if (cmd.hasOption("f")) { EventsInputFile = cmd.getOptionValue("f"); } if (cmd.hasOption("r")) { String val = cmd.getOptionValue("r"); try { String parts[] = val.split(","); if (parts.length == 3) { start_rate = Integer.parseInt(parts[0]); end_rate = Integer.parseInt(parts[1]); rate_step = Integer.parseInt(parts[2]); } else if (parts.length == 1) { // Run single rate start_rate = Integer.parseInt(parts[0]); end_rate = start_rate; rate_step = start_rate; } else { throw new org.apache.commons.cli.ParseException( "Rate must be three comma seperated values or a single value"); } } catch (org.apache.commons.cli.ParseException e) { cmdInputErrorMsg += e.getMessage(); } catch (NumberFormatException e) { cmdInputErrorMsg += "Invalid value for r. Must be integers.\n"; } } if (!cmdInputErrorMsg.equalsIgnoreCase("")) { throw new org.apache.commons.cli.ParseException(cmdInputErrorMsg); } // Assuming the ES port is 9220 RunTcpInBdsOutTest t = new RunTcpInBdsOutTest(); DecimalFormat df = new DecimalFormat("##0"); if (start_rate == end_rate) { // Single Rate Test System.out.println("*********************************"); System.out.println("Incremental testing at requested rate: " + start_rate); System.out.println("Count,Incremental Rate"); t.runTest(numberEvents, start_rate, gisServer, inputTcpPort, EventsInputFile, msLayerUrl, true); if (t.send_rate < 0 || t.rcv_rate < 0) { throw new Exception("Test Run Failed!"); } System.out.println("Overall Average Send Rate, Received Rate"); System.out.println(df.format(t.send_rate) + "," + df.format(t.rcv_rate)); } else { System.out.println("*********************************"); System.out.println("rateRqstd,avgSendRate,avgRcvdRate"); for (int rate = start_rate; rate <= end_rate; rate += rate_step) { t.runTest(numberEvents, rate, gisServer, inputTcpPort, EventsInputFile, msLayerUrl, false); if (t.send_rate < 0 || t.rcv_rate < 0) { throw new Exception("Test Run Failed!"); } System.out.println( Integer.toString(rate) + "," + df.format(t.send_rate) + "," + df.format(t.rcv_rate)); Thread.sleep(3 * 1000); } } } catch (ParseException e) { System.out.println("Invalid Command Options: "); System.out.println(e.getMessage()); System.out.println( "Command line options: -n NumberOfEvents -g GISServer -i InputTCPPort -m MapServerLayerURL -f FileWithEvents -r StartRate,EndRate,Step"); } catch (Exception e) { System.out.println(e.getMessage()); } } }