[jboss-cvs] JBossAS SVN: r68855 - projects/cluster/varia/sessionstress.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Jan 10 18:46:03 EST 2008


Author: bela at jboss.com
Date: 2008-01-10 18:46:03 -0500 (Thu, 10 Jan 2008)
New Revision: 68855

Added:
   projects/cluster/varia/sessionstress/PerfTest2.java
Log:
experimental 2nd version, with MultiThreaded connection pool

Added: projects/cluster/varia/sessionstress/PerfTest2.java
===================================================================
--- projects/cluster/varia/sessionstress/PerfTest2.java	                        (rev 0)
+++ projects/cluster/varia/sessionstress/PerfTest2.java	2008-01-10 23:46:03 UTC (rev 68855)
@@ -0,0 +1,472 @@
+package multicast;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpStatus;
+import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
+import org.apache.commons.httpclient.HttpState;
+import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
+import org.apache.commons.httpclient.methods.GetMethod;
+
+import javax.naming.NamingException;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.text.NumberFormat;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CyclicBarrier;
+
+/**
+ * @author Bela Ban
+ * @version $Id: PerfTest.java 68847 2008-01-10 21:56:26Z bela at jboss.com $
+ */
+public class PerfTest2 {
+    private Client[] clients;
+    private CyclicBarrier barrier;
+    private MultiThreadedHttpConnectionManager pool=new MultiThreadedHttpConnectionManager();
+
+    static NumberFormat f;
+
+    static {
+        f=NumberFormat.getNumberInstance();
+        f.setGroupingUsed(false);
+        f.setMaximumFractionDigits(2);
+    }
+
+
+    
+    private void start(String host, String setup_url, String read_url, String write_url, String destroy_url,
+                       int num_threads, int num_requests, int num_attrs, int size, int write_percentage)
+            throws NamingException, BrokenBarrierException, InterruptedException {
+        HttpConnectionManagerParams params=new HttpConnectionManagerParams();
+        params.setDefaultMaxConnectionsPerHost(200);
+        params.setMaxTotalConnections(500);
+        pool.setParams(params);
+        this.clients=new Client[num_threads];
+        this.barrier=new CyclicBarrier(num_threads + 1);
+        System.out.println("Starting " + num_threads + " clients");
+        for(int i=0; i < clients.length; i++) {
+            Client client=new Client(barrier, host, setup_url, read_url, write_url, destroy_url, write_percentage,
+                                     num_requests, num_attrs, size);
+            clients[i]=client;
+            client.start();
+        }
+
+        System.out.println("Waiting for clients to complete");
+        barrier.await();
+
+        pool.shutdown();
+
+        long total_time=0, total_bytes_read=0, total_bytes_written=0;
+        int total_successful_reads=0, total_successful_writes=0, total_failed_reads=0, total_failed_writes=0;
+
+        int num_clients=0;
+        for(Client client: clients) {
+            if(!client.isSuccessful()) {
+                continue;
+            }
+            num_clients++;
+            total_time+=client.getTime();
+            total_bytes_read+=client.getBytesRead();
+            total_bytes_written+=client.getBytesWritten();
+            total_successful_reads+=client.getSuccessfulReads();
+            total_successful_writes+=client.getSuccessfulWrites();
+            total_failed_reads+=client.getFailedReads();
+            total_failed_writes+=client.getFailedWrites();
+        }
+
+        if(num_clients < 1) {
+            System.err.println("No client completed successfully, will not compute results");
+            return;
+        }
+
+        int failed_clients=num_threads - num_clients;
+        int total_requests=total_successful_reads + total_successful_writes;
+        double avg_time=total_time / num_clients;
+        double reqs_sec=total_requests / (avg_time / 1000.0);
+
+        System.out.println("\nTotal requests: " + total_requests + " in (avg) " + (avg_time / 1000.0) + " secs");
+        System.out.println("\n*** " + f.format(reqs_sec) + " requests/sec, requests/sec/client: " +
+                f.format((total_requests / num_clients) / (avg_time / 1000.0)) + " ***\n");
+       
+        System.out.println("Successful reads: " + total_successful_reads + ", successful writes: " + total_successful_writes);
+        System.out.println("Failed reads: " + total_failed_reads + ", failed writes: " + total_failed_writes);
+        System.out.println("Bytes read: " + printBytes(total_bytes_read) + ", bytes written: " + printBytes(total_bytes_written));
+        System.out.println("Bytes read/sec: " + printBytes(total_bytes_read / (avg_time / 1000.0)) + ", bytes written/sec: " +
+                printBytes(total_bytes_written / (avg_time / 1000.0)));
+        System.out.println("Total client: " + num_clients + ", failed clients: " + failed_clients);
+    }
+
+    private static String printBytes(long bytes) {
+        double tmp;
+
+        if(bytes < 1000)
+            return bytes + "b";
+        if(bytes < 1000000) {
+            tmp=bytes / 1000.0;
+            return f.format(tmp) + "KB";
+        }
+        if(bytes < 1000000000) {
+            tmp=bytes / 1000000.0;
+            return f.format(tmp) + "MB";
+        }
+        else {
+            tmp=bytes / 1000000000.0;
+            return f.format(tmp) + "GB";
+        }
+    }
+
+    public static String printBytes(double bytes) {
+        double tmp;
+
+        if(bytes < 1000)
+            return bytes + "b";
+        if(bytes < 1000000) {
+            tmp=bytes / 1000.0;
+            return f.format(tmp) + "KB";
+        }
+        if(bytes < 1000000000) {
+            tmp=bytes / 1000000.0;
+            return f.format(tmp) + "MB";
+        }
+        else {
+            tmp=bytes / 1000000000.0;
+            return f.format(tmp) + "GB";
+        }
+    }
+
+   
+
+    public static void main(String[] args) throws Exception {
+        int num_threads=1;
+        int num_requests=1000;
+        int num_attrs=40;
+        int size=1000;
+        int write_percentage=10; // percent
+        String host="localhost";
+        String setup_url="/web/setup.jsp";
+        String read_url="/web/read.jsp";
+        String write_url="/web/write.jsp";
+        String destroy_url="/web/destroy.jsp";
+
+        for(int i=0; i < args.length; i++) {
+            if(args[i].equals("-host")) {
+                host=args[++i];
+                continue;
+            }
+            if(args[i].equals("-setup_url")) {
+                setup_url=args[++i];
+                continue;
+            }
+            if(args[i].equals("-read_url")) {
+                read_url=args[++i];
+                continue;
+            }
+            if(args[i].equals("-write_url")) {
+                write_url=args[++i];
+                continue;
+            }
+            if(args[i].equals("-destroy_url")) {
+                destroy_url=args[++i];
+                continue;
+            }
+            if(args[i].equals("-num_threads")) {
+                num_threads=Integer.parseInt(args[++i]);
+                continue;
+            }
+            if(args[i].equals("-num_requests")) {
+                num_requests=Integer.parseInt(args[++i]);
+                continue;
+            }
+            if(args[i].equals("-num_attrs")) {
+                num_attrs=Integer.parseInt(args[++i]);
+                continue;
+            }
+            if(args[i].equals("-size")) {
+                size=Integer.parseInt(args[++i]);
+                continue;
+            }
+            if(args[i].equals("-write_percentage")) {
+                write_percentage=Integer.parseInt(args[++i]);
+                if(write_percentage < 0 || write_percentage > 100) {
+                    System.err.println("write_percentage (" + write_percentage + ") has to be >= 0 && <= 100");
+                    return;
+                }
+                continue;
+            }
+            help();
+            return;
+        }
+
+
+        /*MultiThreadedHttpConnectionManager pool=new MultiThreadedHttpConnectionManager();
+
+        GetMethod method=new GetMethod("http://localhost/web/setup.jsp?num_attrs=40&size=1000");
+        System.out.println("path is " + method.getPath());
+        HttpState state=new HttpState();
+
+        HttpConnection conn;
+        HostConfiguration config=new HostConfiguration();
+        config.setHost("localhost", 80);
+        conn=pool.getConnection(config);
+        conn.open();
+        method.execute(state, conn);
+        byte[] body=method.getResponseBody();
+        System.out.println("resp: " + body.length + " bytes");
+
+        method.execute(state, conn);
+        method.getResponseBody();
+        body=method.getResponseBody();
+        System.out.println("resp: " + body.length + " bytes");
+
+        Cookie[] cookies=state.getCookies();
+        for(int i=0; i < cookies.length; i++) {
+            Cookie tmp=cookies[i];
+            System.out.println("cookie = " + tmp);
+        }
+
+
+        method.setPath("/web/write.jsp?id=5&size=10000");
+        method.setQueryString((String)null);
+        method.execute(state, conn);
+        method.getResponseBody();
+
+        cookies=state.getCookies();
+        for(int i=0; i < cookies.length; i++) {
+            Cookie cookie=cookies[i];
+            System.out.println("cookie = " + cookie);
+        }
+
+        method.releaseConnection();
+*/
+
+        /* HttpClient session=new HttpClient(new MultiThreadedHttpConnectionManager());
+        GetMethod method=new GetMethod("http://localhost/web/setup.jsp?num_attrs=40&size=1000");
+        System.out.println("path is " + method.getPath());
+        session.executeMethod(method);
+        method.getResponseBody();
+        System.out.println("query string is " + method.getQueryString());
+
+        HttpState state=session.getState();
+        Cookie[] cookies=state.getCookies();
+        for(int i=0; i < cookies.length; i++) {
+            Cookie cookie=cookies[i];
+            System.out.println("cookie = " + cookie);
+        }
+
+
+        method.setPath("/web/write.jsp?id=5&size=10000");
+        method.setQueryString((String)null);
+        session.executeMethod(method);
+        method.getResponseBody();
+
+        state=session.getState();
+        cookies=state.getCookies();
+        for(int i=0; i < cookies.length; i++) {
+            Cookie cookie=cookies[i];
+            System.out.println("cookie = " + cookie);
+
+
+        method.releaseConnection();*/
+
+
+
+        new PerfTest2().start(host, setup_url, read_url, write_url, destroy_url, num_threads, num_requests, num_attrs, size, write_percentage);
+    }
+
+    private static void help() {
+        System.out.println("PerfTest2 [-host <host[:port] of apache>] [-read_url <URL>] " +
+                "[-num_threads <number of client sessions>] " +
+                "[-write_url <URL>] [-setup_url <URL>] [-destroy_url <URL>] [-num_requests <requests>] " +
+                "[-num_attrs <attrs>] [-size <bytes>] [-write_percentage <percentage, 0-100>]");
+    }
+
+
+    private class Client extends Thread {
+        private final int           read_percentage;
+        private final int           num_requests, num_attrs, size;
+        private final HttpClient    session=new HttpClient(pool);
+        private final GetMethod     method;
+        private final HttpState     state=new HttpState();
+        private final String        setup_url, read_url, write_url, destroy_url;
+        private final CyclicBarrier my_barrier;
+        private int                 successful_reads=0, failed_reads=0, successful_writes=0, failed_writes=0;
+        private long                bytes_read=0, bytes_written=0;
+        private long                start=0, stop=0;
+        private boolean             successful=true;
+
+
+        
+        private Client(CyclicBarrier barrier, String host,
+                       String setup_url, String read_url, String write_url, String destroy_url,
+                       int write_percentage, int num_requests, int num_attrs, int size) {
+            this.my_barrier=barrier;
+            this.read_percentage=100 - write_percentage;
+            this.num_requests=num_requests;
+            this.num_attrs=num_attrs;
+            this.size=size;
+            String tmp="http://" + host + "/";
+            this.setup_url=setup_url;
+            this.read_url=read_url;
+            this.write_url=write_url;
+            this.destroy_url=destroy_url;
+            method=new GetMethod(tmp);
+        }
+
+        public void run() {
+            try {
+                init(num_attrs, size);
+                start=System.currentTimeMillis();
+                loop(num_requests);
+            }
+            catch(Exception e) {
+                error("failure", e);
+                successful=false;
+            }
+            finally {
+                stop=System.currentTimeMillis();
+                try {terminate();} catch(IOException e) {}
+                log("barrier.await()");
+                try {my_barrier.await();} catch(Exception e) {}
+                // method.releaseConnection();
+            }
+        }
+
+
+
+        public long getBytesRead() {
+            return bytes_read;
+        }
+
+        public long getBytesWritten() {
+            return bytes_written;
+        }
+
+        public int getFailedReads() {
+            return failed_reads;
+        }
+
+        public int getFailedWrites() {
+            return failed_writes;
+        }
+
+        public int getSuccessfulReads() {
+            return successful_reads;
+        }
+
+        public int getSuccessfulWrites() {
+            return successful_writes;
+        }
+
+        public long getTime() {
+            return stop - start;
+        }
+
+        public boolean isSuccessful() {
+            return successful;
+        }
+
+        /** Create NUM_SESSIONS sessions with NUM_ATTRS attributes of SIZE size. Total size is multiplication of the 3 */
+        private void init(int num_attrs, int size) throws IOException {
+            method.setPath(setup_url);
+            method.setQueryString((String)null);
+            int rc=executeRequest(method);
+            if(rc != HttpStatus.SC_OK) {
+                throw new RuntimeException("failed initializing session", null);
+            }
+            else {
+                log("successfully initialized session with " + num_attrs + " attrs of " + size + " bytes");
+            }
+        }
+
+        private void terminate() throws IOException {
+            method.setPath(destroy_url);
+            method.setQueryString((String)null);
+            executeRequest(method);
+            log("destroyed session");
+        }
+
+        private void loop(int num_requests) throws IOException {
+            int random, id;
+            int print=num_requests / 10;
+
+            int rc, total=0;
+
+            for(int i=0; i < num_requests; i++) {
+                random=(int)random(100);
+                id=(int)random(num_attrs -1);
+                if(random <= read_percentage) { // read
+                    method.setPath(read_url + "?id=" + id + "&size=" + size);
+                    method.setQueryString((String)null);
+                    rc=executeRequest(method);
+                    if(rc == HttpStatus.SC_OK) {
+                        successful_reads++;
+                        bytes_read+=size; // bytes read from the session, not by the HttpClient !
+                    }
+                    else {
+                        failed_reads++;
+                    }
+                }
+                else {             // write
+                    method.setPath(write_url + "?id=" + id + "&size=" + size);
+                    method.setQueryString((String)null);
+                    rc=executeRequest(method);
+                    if(rc == HttpStatus.SC_OK) {
+                        successful_writes++;
+                        bytes_written+=size; // bytes read from the session, not by the HttpClient !
+                    }
+                    else {
+                        failed_writes++;
+                    }
+                }
+                total++;
+                if(print > 0 && total % print == 0)
+                    log(total + " / " + num_requests);
+            }
+        }
+        
+//        private int executeRequest(GetMethod method) throws IOException {
+//            try {
+//                int rc = session.executeMethod(method);
+//               method.getResponseBody();
+//               return rc;
+//           }
+//           finally {
+//               method.releaseConnection();
+//           }
+//        }
+
+
+        private int executeRequest(GetMethod method) throws IOException {
+            return session.executeMethod(method);
+        }
+
+        private void log(String msg) {
+            System.out.println("[thread-" + Thread.currentThread().getId() + "]: " + msg);
+        }
+
+        private void error(String msg, Throwable th) {
+            String tmp="[thread-" + Thread.currentThread().getId() + "]: " + msg;
+            if(th != null) {
+                tmp+=", ex: " + th + "\n";
+                StringWriter writer = new StringWriter();
+                PrintWriter pw = new PrintWriter(writer);
+                th.printStackTrace(pw);
+                pw.flush();
+                tmp+= writer.toString();
+            }
+            System.err.println(tmp);
+//            log(tmp);
+        }
+
+        private long random(long range) {
+            return (long)((Math.random() * 100000) % range) + 1;
+        }
+
+
+
+
+
+    }
+
+
+}


Property changes on: projects/cluster/varia/sessionstress/PerfTest2.java
___________________________________________________________________
Name: svn:executable
   + *




More information about the jboss-cvs-commits mailing list