博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

http loadgen performance test tools

Posted on 2012-06-27 18:08  钟悍  阅读(598)  评论(0编辑  收藏  举报


package com.test.karl.tools;

class Statistics implements Runnable {

    public void run() {
        while (true) {
            String message="start: " + Loadgen.numOfStart.get() + " fail: " + Loadgen.numOfFail.get()+" Finished: "
            + Loadgen.numOfFinished.get() +"  succeed requests:"+Loadgen.successedRequest.get()+"  failed requests:"+Loadgen.failedRequest.get();
            Loadgen.message(message, "info");
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                
            }
        }
    }
}

 

 

package com.test.karl.tools; 


class RRRequest {
    public String uri;
    public String body;

    public String toString() {
        return "uri:" + uri + ",body=" + body;
    }
}

 

package com.test.karl.tools;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

class Client implements Runnable {
    int timeout = 20000;
    int clientId = 0;
    public static final Integer MAX_IDLE_TIME_OUT = 60000;
    public static final Integer MAX_CONN = 100;

    public Client(int clientId_i, int time_out) {
        clientId = clientId_i;
        timeout = time_out;
    }

    public void run() {
        try {
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            connectionManager.closeIdleConnections(MAX_IDLE_TIME_OUT);
            connectionManager.getParams().setParameter(
                    "http.connection-manager.max-total", MAX_CONN);
            HttpClient httpClient = new HttpClient(connectionManager);
            httpClient.getParams().setParameter("http.socket.timeout", timeout);
            httpClient.getParams().setParameter("http.connection.timeout",
                    timeout);
            httpClient.getParams().setParameter(
                    "http.connection-manager.timeout", Long.valueOf(timeout));
            for (int i = 0; i < Loadgen.cmd.size(); i++) {
                RRRequest request = Loadgen.cmd.get(i);
                String url = request.uri;

                String body = request.body;
                byte[] bytes = body.getBytes();
                RequestEntity re = new ByteArrayRequestEntity(bytes,
                        "text/plain; charset=UTF-8");
                PostMethod method = new PostMethod(url);
                try {
                    method.setRequestEntity(re);
                    method.getParams().setParameter("http.socket.timeout",
                            timeout);
                    method.addRequestHeader(new Header("Connection", "close"));
                    int returncode = httpClient.executeMethod(method);
                    if (returncode == 200) {
                        Loadgen.successedRequest.incrementAndGet();
                    } else {
                        method.abort();
                        Loadgen.failedRequest.incrementAndGet();
                        Loadgen.message("url:" + url + " responseCode: "
                                + returncode, "error");
                    }
                } catch (Exception e) {
                    Loadgen.numOfFail.incrementAndGet();
                    Loadgen.message("url:" + url + " error: " + e.getMessage(),
                            "error");
                    break;
                } finally {
                    method.releaseConnection();
                }
            }
        } catch (Exception e) {
            Loadgen.numOfFail.incrementAndGet();
        }
        Loadgen.numOfFinished.incrementAndGet();
    }
}

 


package com.test.karl.tools;

import java.io.BufferedReader;

public class Loadgen {
    public static List<RRRequest> cmd = null;
    private static int num = 0;
    private static String cmdfilepath = null;
    private static long duration = 0; // unit : minute
    private static long interval = 0;
    private static int time_out = 10000;
    public static AtomicLong numOfStart = new AtomicLong(0);
    public static AtomicLong numOfFinished = new AtomicLong(0);
    public static AtomicLong numOfFail = new AtomicLong(0);
    public static AtomicLong successedRequest = new AtomicLong(0);
    public static AtomicLong failedRequest = new AtomicLong(0);
    private static boolean printLog = false;

    public Loadgen() {
        loadcmd();
        loadstatistics();
        checkoutDuration();
    }

    private void checkoutDuration() {
        Long starttime = System.currentTimeMillis();
        int j=0;
        while (true) {
            if (System.currentTimeMillis() - starttime > duration * 1000) {
                message("exiting... (wait 30 second for exiting)","info");
                try {
                    Thread.sleep(30000L);
                } catch (InterruptedException e) {
                }
                long tps=Loadgen.successedRequest.get()/duration;
                message("TPS:"+tps, "info");
                
                Long started=Loadgen.numOfStart.get();
                Long failed = Loadgen.numOfFail.get();
                float clientFailRate = Float.valueOf(failed)/Float.valueOf(started);
                message("Client Fail Rate "+failed+"/"+started+"="+clientFailRate,"info");
                
                Long successReq=Loadgen.successedRequest.get();
                Long failedReq = Loadgen.failedRequest.get();
                float requestFailRate = Float.valueOf(failedReq)/Float.valueOf(successReq+failedReq);
                message("Request Fail Rate "+failedReq+"/("+successReq+"+"+failedReq+")="+requestFailRate,"info");
                message("normal exit, total spend " + duration + " seconds", "info");
                System.exit(0);
            }
            for (int i=0; i < num; i++) {
                new Thread(new Client(j + 1, time_out)).start();
                j++;
                numOfStart.incrementAndGet();
            }
            try{
                Thread.sleep(interval);
            }catch(Exception e){
                
            }
        }
    }

    private void loadstatistics() {
        try {
            new Thread(new Statistics()).start();
        } catch (Exception e) {
            message(e.getMessage(), "error");
        }
    }

    public static void message(String msg, String level) {
        if (printLog && level.equals("error")) {
            try {
                File file = new File("error.txt");
                boolean flag = true;
                if (!file.exists()) {
                    flag = file.createNewFile();
                }
                if (!flag) {
                    System.out.println("Create log file Error.");
                }
                FileWriter saveFile = new FileWriter(file, true);

                PrintWriter saveF = new PrintWriter(saveFile);
                saveF.println(msg);
                saveF.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if(level.equals("info")){
            try {
                File file = new File("info.txt");
                boolean flag = true;
                if (!file.exists()) {
                    flag = file.createNewFile();
                }
                if (!flag) {
                    System.out.println("Create log file Error.");
                }
                FileWriter saveFile = new FileWriter(file, true);

                PrintWriter saveF = new PrintWriter(saveFile);
                saveF.println(msg);
                saveF.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void loadcmd() {
        File file = new File(cmdfilepath);
        FileInputStream fis = null;
        InputStreamReader bis = null;
        BufferedReader dis = null;
        cmd = new ArrayList<RRRequest>();
        String temp = null;
        try {
            fis = new FileInputStream(file);
            bis = new InputStreamReader(fis);
            dis = new BufferedReader(bis);

            while ((temp = dis.readLine()) != null) {
                RRRequest request = new RRRequest();
                if (temp.length() != 0) {
                    request.uri = temp.substring(4);
                    temp = dis.readLine();
                    if (temp.length() != 0) {
                        request.body = temp.substring(5);
                    } else {
                        message(cmdfilepath + "error.","error");
                    }
                }
                cmd.add(request);
            }

            fis.close();
            bis.close();
            dis.close();
        } catch (FileNotFoundException e) {
            message(e.getMessage(),"error");
        } catch (IOException e) {
            message(e.getMessage(),"error");
        }
    }

    public static void main(String args[]) {
        System.out.println("Start.........");
        message("started at " + new Date().toString(),"info");
        for (int argsPos = 0; argsPos < args.length; argsPos++) {
            if (args[argsPos].equals("-n")) {
                argsPos++;
                num = Integer.valueOf(args[argsPos]).intValue();
            } else if (args[argsPos].equals("-cmdf")) {
                argsPos++;
                cmdfilepath = args[argsPos];
            } else if (args[argsPos].equals("-duration")) {
                argsPos++;
                duration = Long.valueOf(args[argsPos]);
            } else if (args[argsPos].equals("-t")) {
                argsPos++;
                interval = Long.valueOf(args[argsPos]).longValue() * 1000;
            } else if (args[argsPos].equals("-to")) {
                argsPos++;
                time_out = Integer.valueOf(args[argsPos]).intValue() * 1000;
            } else if (args[argsPos].equals("log")) {
                printLog = true;
            }
        }
        new Loadgen();
    }
}



readme.txt


package loadgen.jar

useage: 
java -jar loadgen.jar  -n 100  -t 1 -duration 10 -to 20 -cmdf request.txt log

every '-t' second create '-n' users to send requests(in -cmdf).

-n 100     : simulate 100 clients send request.
-t 1    : interval times(unit : second) of start 5(5 use -b to config) threads  ;
-to 20  : http connection timeout(unit : second) .
-cmdf      : indicated request file.
-duration  : run test test time, (unit : second), if the duration is timeout, then the test tool will exit.
log:   if the parameter exist, then the tools will write log, else will not write log.

in request file
three line combind one group, 
in one group:
the first line is the request uri.
the second line is the request body.
for example.

uri=http://127.0.0.1:8080/reportingService/karln_bmsc/1203_1000/
body=<?xml version="1.0" encoding="UTF-8"?><receptionReport xmlns="urn:3gpp:metadata:2008:MBMS:receptionreport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><receptionAcknowledgement><fileURI>http://192.168.0.100/ssss/iceage.mpd</fileURI><fileURI>http://192.168.0.100/ssss/iceage1.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage2.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage3.m4s</fileURI></receptionAcknowledgement></receptionReport>

 

request.txt

uri=http://127.0.0.1:9900/reportingService/karl_bmsc/1203_1000/
body=<?xml version="1.0" encoding="UTF-8"?><receptionReport xmlns="urn:3gpp:metadata:2008:MBMS:receptionreport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><receptionAcknowledgement><fileURI>http://192.168.0.100/ssss/iceage.mpd</fileURI><fileURI>http://192.168.0.100/ssss/iceage1.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage2.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage3.m4s</fileURI></receptionAcknowledgement></receptionReport>
uri=http://127.0.0.1:9900/reportingService/
karl_bmsc/1203_1000/
body=<?xml version="1.0" encoding="UTF-8"?><receptionReport xmlns="urn:3gpp:metadata:2008:MBMS:receptionreport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><receptionAcknowledgement><fileURI>http://192.168.0.100/ssss/iceage.mpd</fileURI><fileURI>http://192.168.0.100/ssss/iceage1.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage2.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage3.m4s</fileURI></receptionAcknowledgement></receptionReport>
uri=http://127.0.0.1:9900/reportingService/karl_bmsc/1203_1000/
body=<?xml version="1.0" encoding="UTF-8"?><receptionReport xmlns="urn:3gpp:metadata:2008:MBMS:receptionreport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><receptionAcknowledgement><fileURI>http://192.168.0.100/ssss/iceage.mpd</fileURI><fileURI>http://192.168.0.100/ssss/iceage1.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage2.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage3.m4s</fileURI></receptionAcknowledgement></receptionReport>
uri=http://127.0.0.1:9900/reportingService/karl_bmsc/1203_1000/
body=<?xml version="1.0" encoding="UTF-8"?><receptionReport xmlns="urn:3gpp:metadata:2008:MBMS:receptionreport" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><receptionAcknowledgement><fileURI>http://192.168.0.100/ssss/iceage.mpd</fileURI><fileURI>http://192.168.0.100/ssss/iceage1.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage2.m4s</fileURI><fileURI>http://192.168.0.100/ssss/iceage3.m4s</fileURI></receptionAcknowledgement></receptionReport>