Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // client thread
- public class SHTTPClientThread extends Thread {
- AtomicBoolean stop;
- int index;
- ClientStats cs;
- Socket s;
- String files[];
- int filesRead, bytesRead;
- long avgWaitTime;
- String server;
- int port;
- public SHTTPClientThread(String server, int port, String files[],
- int index, ClientStats cs) throws UnknownHostException, IOException {
- stop = new AtomicBoolean();
- stop.set(false);
- this.index = index;
- this.cs = cs;
- this.files = files;
- filesRead = 0; bytesRead = 0; avgWaitTime = 0;
- this.server = server;
- this.port = port;
- }
- public void run() {
- int i = 0;
- try {
- while (!stop.get()) {
- s = new Socket(server, port);
- String file = files[i % files.length];
- sendRequest(file);
- readResponse();
- i++;
- s.close();
- }
- cs.dataRateThroughPut[index] = bytesRead;
- cs.waitTimes[index] = avgWaitTime/filesRead;
- cs.totalThroughPut[index] = filesRead;
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- private void readResponse() throws IOException {
- long stime = System.currentTimeMillis();
- try {
- HTTPResponse r = new HTTPResponse(s);
- bytesRead += r.bodySize;
- filesRead += 1;
- avgWaitTime += r.time - stime;
- } catch (InvalidResponseException e) {
- System.err.println("Invalid Response.");
- e.printStackTrace();
- }
- }
- private void sendRequest(String file) throws IOException {
- s.getOutputStream().write(("GET /" + file + " HTTP/1.0\r\n\r\n").getBytes("US-ASCII"));
- }
- public void done() {
- stop.set(true);
- }
- }
- // client
- public class SHTTPTestClient {
- public static void main(String args[]) throws InterruptedException {
- int port, threads, time;
- String server, filename;
- try {
- Map<String, String[]> argmap = ServerUtil.readVars(args, "-");
- port = Integer.parseInt(argmap.get("port")[0]);
- threads = Integer.parseInt(argmap.get("parallel")[0]);
- time = Integer.parseInt(argmap.get("T")[0]);
- server = argmap.get("server")[0];
- filename = argmap.get("files")[0];
- } catch (NullPointerException e) {
- System.err
- .println("Usage: java SHTTPTestClient -server <server> -port <server port> -parallel <# of threads> -files <file name> -T <time of test in seconds>");
- return;
- }
- ArrayList<String> arrFiles = new ArrayList<String>();
- Scanner scan;
- try {
- scan = new Scanner(new File(filename));
- } catch (FileNotFoundException e1) {
- System.err.println("File does not exist.");
- return;
- }
- while(scan.hasNextLine())
- arrFiles.add(scan.nextLine());
- String files[] = null;
- if(arrFiles.size() == 0) {
- System.err.println("Empty File.");
- return;
- } else {
- files = arrFiles.toArray(new String[0]);
- }
- ClientStats cs = new ClientStats(threads, time);
- SHTTPClientThread threadSet[] = new SHTTPClientThread[threads];
- try {
- for (int i = 0; i < threads; i++) {
- threadSet[i] = new SHTTPClientThread(server, port, files, i, cs);
- threadSet[i].start();
- }
- } catch (Exception e) {
- System.err.println("Unknown host or IO error.");
- return;
- }
- Thread.sleep(time * 1000);
- for (int i = 0; i < threads; i++)
- threadSet[i].done();
- for (int i = 0; i < threads; i++)
- threadSet[i].join();
- cs.printStats();
- }
Advertisement
Add Comment
Please, Sign In to add comment