`
lihy70
  • 浏览: 1075 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

test

阅读更多
/*
 * @(#)PerformanceTest .java        2008/01/16
 *
 * Copyright 2008 li haiyang. All rights reserved.
 */

import java.io.PrintWriter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.sql.Time;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 *
 * @author li haiyang
 *
 */
public abstract class PerformanceTest {
    /**
     *
     * @param methodName
     * @param args
     * @param threadNum
     * @param loops
     * @return
     * @see #run(String, List, int, int, int, boolean, boolean)
     */
    public LoopsStatistics run(String methodName, List args, final int threadNum, final int loops) {
        return run(methodName, args, threadNum, 0, loops, true, true);
    }

    /**
     *
     * @param methodNames
     * @param args
     * @param threadNum
     * @param loops
     * @return
     * @see #run(String, List, int, int, int, boolean, boolean)
     */
    public LoopsStatistics run(List methodNames, List args, final int threadNum, final int loops) {
        return run(methodNames, args, threadNum, 0, loops, true, true);
    }

    /**
     *
     * @param methodName
     * @param args
     * @param threadNum
     * @param threadDelay
     * @param loops
     * @param loopSetUpAndTearDown
     * @param callSetUpAndTearDown
     * @return
     *
     * @see #run(List, List, int, int, int, boolean, boolean)
     */
    public LoopsStatistics run(String methodName, List args, final int threadNum,
        final int threadDelay, final int loops, final boolean loopSetUpAndTearDown,
        final boolean callSetUpAndTearDown) {
        List methodNames = new ArrayList();
        methodNames.add(methodName);

        return run(methodNames, args, threadNum, threadDelay, loops, loopSetUpAndTearDown,
            callSetUpAndTearDown);
    }

    /**
     *
     * <br>The test method must have following format:
     * <br><code>Object testMethod(Object arg)</code>
     * <br>The input parameter <code>arg</code> is come from the previous method returned value,
     * <br> or from this loop argument if this test method is the first test method.
     * <br>The <code>returned value</code> is the initial method argument for next test method if this test method is not the last one.
     * <br>The last test method returned value is not signed as next loop initial argument.
     * <br>
     * <br>
     * <b>The argument flow:</b>
     * <br>(0) --> loop initial argument = thread initial argument
     * <br>(1) --> loop initial argument = loopSetup(loop initial argument) // if callLoopSetUpAndTearDown == true
     * <br>(2) --> method initial argument = loop initial argument
     * <br>(3) --> method initial argument = methodSetUp(method initial argument) // if callMethodSetUpAndTearDown == true.
     * <br>(4) --> method initial argument = testMethod(method initial argument) // if the test method doesn't throw exception.
     * <br>(5) --> method initial argument = methodTearDown(method initial argument) // if callMethodSetUpAndTearDown == true.
     * <br>(6) --> go to (3) for each method.
     * <br>(7) --> loop initial argument = loopTearDown(loop initial argument) // if callLoopSetUpAndTearDown == true
     * <br>(8) --> go to (1) for each loop.
     *
     * @param methodNames
     * @param args initial parameters for each test thread. Each thread has one parameter.
     *                           <br>So the <code>args</code>'s size must  be equals with <code>threadNum</code>.
     * @param threadNum
     * @param threadDelay waiting time to start next thread. Start next thread right now if <code>threadDelay==0</code>.
     * @param loops loop times for each thread.
     * @param callLoopSetUpAndTearDown run <code>loopSetUp</code> before each loop
     *                           <br> and run <code>loopTearDown</code> after loop if <code>true</code>
     * @param callMethodSetUpAndTearDown  run <code>methodSetUp</code> before each test method
     *                           <br> and run <code>methodTearDown</code> after test method if <code>true</code>
     * @return
     */
    public LoopsStatistics run(List methodNames, final List args, final int threadNum,
        final int threadDelay, final int loops, final boolean callLoopSetUpAndTearDown,
        final boolean callMethodSetUpAndTearDown) {
        final Method[] methods = getMethodByName(methodNames);
        final Counter threadCounter = new Counter();

        final List loopStatisticsList = Collections.synchronizedList(new ArrayList());
        long startTime = System.currentTimeMillis();

        if (threadNum == 0) {
        } else if (threadNum == 1) {
            runLoop(methods, args.get(0), loops, callLoopSetUpAndTearDown,
                callMethodSetUpAndTearDown, loopStatisticsList);
        } else {
            for (int threadIndex = 0; threadIndex < threadNum; threadIndex++) {
                final Object threadArg = args.get(threadIndex);
                Thread th = new Thread() {
                        public void run() {
                            try {
                                runLoop(methods, threadArg, loops, callLoopSetUpAndTearDown,
                                    callMethodSetUpAndTearDown, loopStatisticsList);
                            } finally {
                                threadCounter.decreaseAndGet();
                            }
                        }

                        public synchronized void start() {
                            threadCounter.increaseAndGet();
                            super.start();
                        }
                    };

                th.start();

                if (threadDelay > 0) {
                    sleep(threadDelay);
                }
            }

            while (threadCounter.getActiveThreadNum() > 0) {
                sleep(10);
            }
        }

        long endTime = System.currentTimeMillis();

        return new LoopsStatistics(startTime, endTime, threadNum, loopStatisticsList);
    }

    protected void runLoop(final Method[] methods, final Object threadArg, final int loops,
        final boolean callLoopSetUpAndTearDown, final boolean callMethodSetUpAndTearDown,
        final List loopStatisticsList) {
        Object loopArg = threadArg;

        for (int loopIndex = 0; loopIndex < loops; loopIndex++) {
            if (callLoopSetUpAndTearDown) {
                try {
                    loopArg = loopSetUp(threadArg);
                } catch (Exception e) {
                    e.printStackTrace();

                    // ignore;
                }
            }

            long loopStartTime = System.currentTimeMillis();

            List methodStatisticsList = new ArrayList();
            Object methodArg = loopArg;

            for (int methodIndex = 0; methodIndex < methods.length; methodIndex++) {
                if (callMethodSetUpAndTearDown) {
                    try {
                        methodArg = methodSetUp(methodArg);
                    } catch (Exception e) {
                        e.printStackTrace();

                        // ignore;
                    }
                }

                Object result = null;
                long methodStartTime = System.currentTimeMillis();

                try {
                    result = invokeMethod(methods[methodIndex], methodArg);
                    methodArg = result;
                } catch (Exception e) {
                    result = e;
                    e.printStackTrace();

                    // ignore;
                }

                long methodEndTime = System.currentTimeMillis();

                if (callMethodSetUpAndTearDown) {
                    try {
                        methodArg = methodTearDown(methodArg);
                    } catch (Exception e) {
                        e.printStackTrace();

                        // ignore;
                    }
                }

                MethodStatistics methodStatistics = new MethodStatistics(methods[methodIndex]
                        .getName(), methodStartTime, methodEndTime, result);
                methodStatisticsList.add(methodStatistics);
            }

            long loopEndTime = System.currentTimeMillis();

            if (callLoopSetUpAndTearDown) {
                try {
                    loopArg = loopTearDown(loopArg);
                } catch (Exception e) {
                    e.printStackTrace();

                    // ignore;
                }
            }

            Loop loopStatistics = new LoopStatistics(loopStartTime, loopEndTime,
                    methodStatisticsList);

            loopStatisticsList.add(loopStatistics);
        }
    }

    protected Object loopSetUp(Object arg) {
        return arg;
    }

    protected Object loopTearDown(Object arg) {
        return arg;
    }

    protected Object methodSetUp(Object arg) {
        return arg;
    }

    protected Object methodTearDown(Object arg) {
        return arg;
    }

    protected Method getMethodByName(String methodName) {
        try {
            return this.getClass().getMethod(methodName, new Class[0]);
        } catch (SecurityException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    protected Method[] getMethodByName(List methodNames) {
        try {
            Method[] result = new Method[methodNames.size()];

            for (int index = 0; index < methodNames.size(); index++) {
                result[index] = this.getClass()
                                    .getMethod((String) methodNames.get(index),
                        new Class[] { Object.class });
            }

            return result;
        } catch (SecurityException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    protected Object invokeMethod(Method method, Object arg) {
        try {
            return method.invoke(this, new Object[] { arg });
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    protected void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public interface Loop {
        List getMethodNames();

        MethodStatistics getSpendMinTimeMethod();

        MethodStatistics getSpendMaxTimeMethod();

        long getTotalMethodSpendedTime();

        List getFailedMethodList();

        public long getMethodSpendedTotalTime(String methodName);

        public long getMethodSpendedMaxTime(String methodName);

        public long getMethodSpendedMinTime(String methodName);

        public float getMethodSpendedAvgTime(String methodName);

        public List getMethodFailed(String methodName);

        public int getMethodSize(String methodName);
    }

    public static class Statistics {
        private Object result;
        private long startTime;
        private long endTime;

        public Statistics() {
            this.startTime = 0;
            this.endTime = 0;
        }

        public Statistics(long startTime, long endTime) {
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public Object getResult() {
            return result;
        }

        public void setResult(Object result) {
            this.result = result;
        }

        public long getSpendedTime() {
            return endTime - startTime;
        }

        public long getStartTime() {
            return startTime;
        }

        public void setStartTime(long startTime) {
            this.startTime = startTime;
        }

        public long getEndTime() {
            return endTime;
        }

        public void setEndTime(long endTime) {
            this.endTime = endTime;
        }
    }

    public static class LoopStatistics extends Statistics implements Loop {
        private List methodStatisticsList;

        public LoopStatistics() {
            super();
        }

        public LoopStatistics(long startTime, long endTime) {
            super(startTime, endTime);
        }

        public LoopStatistics(long startTime, long endTime, List methodStatisticsList) {
            super(startTime, endTime);
            this.methodStatisticsList = methodStatisticsList;
        }

        public List getMethodNames() {
            List result = new ArrayList();

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);
                    result.add(methodStatistics.getMethodName());
                }
            }

            return result;
        }

        public List getMethodStatisticsList() {
            if (methodStatisticsList == null) {
                methodStatisticsList = new ArrayList();
            }

            return methodStatisticsList;
        }

        public void setMethodStatisticsList(List methodStatisticsList) {
            this.methodStatisticsList = methodStatisticsList;
        }

        public void addMethodStatisticsList(MethodStatistics methodStatistics) {
            getMethodStatisticsList().add(methodStatistics);
        }

        public long getTotalMethodSpendedTime() {
            long result = 0;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);
                    result += methodStatistics.getSpendedTime();
                }
            }

            return result;
        }

        public MethodStatistics getSpendMaxTimeMethod() {
            MethodStatistics result = null;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if ((result == null)
                            || (methodStatistics.getSpendedTime() > result.getSpendedTime())) {
                        result = methodStatistics;
                    }
                }
            }

            return result;
        }

        public MethodStatistics getSpendMinTimeMethod() {
            MethodStatistics result = null;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if ((result == null)
                            || (methodStatistics.getSpendedTime() < result.getSpendedTime())) {
                        result = methodStatistics;
                    }
                }
            }

            return result;
        }

        public List getFailedMethodList() {
            List result = new ArrayList();

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.isFailed()) {
                        result.add(methodStatistics);
                    }
                }
            }

            return result;
        }

        public long getMethodSpendedTotalTime(String methodName) {
            long result = 0;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.getMethodName().equals(methodName)) {
                        result += methodStatistics.getSpendedTime();
                    }
                }
            }

            return result;
        }

        public long getMethodSpendedMaxTime(String methodName) {
            long result = 0;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.getMethodName().equals(methodName)) {
                        if (methodStatistics.getSpendedTime() > result) {
                            result = methodStatistics.getSpendedTime();
                        }
                    }
                }
            }

            return result;
        }

        public long getMethodSpendedMinTime(String methodName) {
            long result = Integer.MAX_VALUE;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.getMethodName().equals(methodName)) {
                        if (methodStatistics.getSpendedTime() < result) {
                            result = methodStatistics.getSpendedTime();
                        }
                    }
                }
            }

            return result;
        }

        public float getMethodSpendedAvgTime(String methodName) {
            float totalTime = 0;
            int methodNum = 0;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.getMethodName().equals(methodName)) {
                        totalTime += methodStatistics.getSpendedTime();
                        methodNum++;
                    }
                }
            }

            return (methodNum > 0) ? (totalTime / methodNum) : totalTime;
        }

        public List getMethodFailed(String methodName) {
            List result = new ArrayList();

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.isFailed() && methodStatistics.equals(methodName)) {
                        result.add(methodStatistics);
                    }
                }
            }

            return result;
        }

        public int getMethodSize(String methodName) {
            int methodSize = 0;

            if (methodStatisticsList != null) {
                for (int index = 0; index < methodStatisticsList.size(); index++) {
                    MethodStatistics methodStatistics = (MethodStatistics) methodStatisticsList.get(index);

                    if (methodStatistics.getMethodName().equals(methodName)) {
                        methodSize++;
                    }
                }
            }

            return methodSize;
        }
    }

    public static class LoopsStatistics extends Statistics implements Loop {
        private final int threadNum;
        private List loopStatisticsList;

        public LoopsStatistics(long startTime, long endTime, int threadNum) {
            super(startTime, endTime);
            this.threadNum = threadNum;
        }

        public LoopsStatistics(long startTime, long endTime, int threadNum, List loopStatisticsList) {
            this(startTime, endTime, threadNum);
            this.loopStatisticsList = loopStatisticsList;
        }

        public int getThreadNum() {
            return threadNum;
        }

        public List getMethodNames() {
            List result = null;

            if (loopStatisticsList == null) {
                result = new ArrayList();
            } else {
                result = ((Loop) loopStatisticsList.get(0)).getMethodNames();
            }

            return result;
        }

        public List getLoopStatisticsList() {
            if (loopStatisticsList == null) {
                loopStatisticsList = new ArrayList();
            }

            return loopStatisticsList;
        }

        public void setLoopStatisticsList(List loopStatisticsList) {
            this.loopStatisticsList = loopStatisticsList;
        }

        public void addMethodStatisticsList(Loop loopStatistics) {
            getLoopStatisticsList().add(loopStatistics);
        }

        public long getTotalMethodSpendedTime() {
            long result = 0;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    result += loopStatistics.getTotalMethodSpendedTime();
                }
            }

            return result;
        }

        public MethodStatistics getSpendMaxTimeMethod() {
            MethodStatistics result = null;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    MethodStatistics methodStatistics = loopStatistics.getSpendMaxTimeMethod();

                    if ((result == null)
                            || (methodStatistics.getSpendedTime() > result.getSpendedTime())) {
                        result = methodStatistics;
                    }
                }
            }

            return result;
        }

        public MethodStatistics getSpendMinTimeMethod() {
            MethodStatistics result = null;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    MethodStatistics methodStatistics = loopStatistics.getSpendMinTimeMethod();

                    if ((result == null)
                            || (methodStatistics.getSpendedTime() < result.getSpendedTime())) {
                        result = methodStatistics;
                    }
                }
            }

            return result;
        }

        public List getFailedMethodList() {
            List result = new ArrayList();

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    result.addAll(loopStatistics.getFailedMethodList());
                }
            }

            return result;
        }

        public long getMethodSpendedTotalTime(String methodName) {
            long result = 0;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    result += loopStatistics.getMethodSpendedTotalTime(methodName);
                }
            }

            return result;
        }

        public long getMethodSpendedMaxTime(String methodName) {
            long result = 0;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    long loopMethodMaxTime = loopStatistics.getMethodSpendedMaxTime(methodName);

                    if (loopMethodMaxTime > result) {
                        result = loopMethodMaxTime;
                    }
                }
            }

            return result;
        }

        public long getMethodSpendedMinTime(String methodName) {
            long result = Integer.MAX_VALUE;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    long loopMethodMinTime = loopStatistics.getMethodSpendedMinTime(methodName);

                    if (loopMethodMinTime < result) {
                        result = loopMethodMinTime;
                    }
                }
            }

            return result;
        }

        public float getMethodSpendedAvgTime(String methodName) {
            float totalTime = 0;
            int methodNum = 0;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    float loopMethodTotalTime = loopStatistics.getMethodSpendedTotalTime(methodName);
                    int loopMethodSize = loopStatistics.getMethodSize(methodName);
                    totalTime += loopMethodTotalTime;
                    methodNum += loopMethodSize;
                }
            }

            return (methodNum > 0) ? (totalTime / methodNum) : totalTime;
        }

        public List getMethodFailed(String methodName) {
            List result = new ArrayList();

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    result.addAll(loopStatistics.getMethodFailed(methodName));
                }
            }

            return result;
        }

        public int getMethodSize(String methodName) {
            int result = 0;

            if (loopStatisticsList != null) {
                for (int index = 0; index < loopStatisticsList.size(); index++) {
                    Loop loopStatistics = (Loop) loopStatisticsList.get(index);
                    result += loopStatistics.getMethodSize(methodName);
                }
            }

            return result;
        }

        public void printResult() {
            System.out.println(
                "================================================================================");

            writeResult(new PrintWriter(System.out));

            System.out.println(
                "================================================================================");
        }

        public void writeResult(PrintWriter writer) {
            writer.println();
            writer.println("threadNum=" + threadNum + "; totalLoops=" + loopStatisticsList.size()
                + ".");
            writer.println("<startTime>: " + getStartTime() + "(" + new Time(getStartTime())
                + ")");
            writer.println("<endTime>: " + getEndTime() + "(" + new Time(getEndTime()) + ")");
            writer.println("<totalSpendedTime>: " + getSpendedTime() + "ms");
            writer.write("<eachLoopSpendedTime>: "
                + (((float) getSpendedTime()) / loopStatisticsList.size()) + "ms");
            writer.println();

            MethodStatistics methodStatistics = getSpendMaxTimeMethod();

            if (methodStatistics != null) {
                writer.println("<singleMethodSpendedMaxTime>: method="
                    + methodStatistics.getMethodName() + ", spendedTime="
                    + methodStatistics.getSpendedTime() + "ms");
            }

            methodStatistics = getSpendMinTimeMethod();

            if (methodStatistics != null) {
                writer.println("<singleMethodSpendedMinTime>: method="
                    + methodStatistics.getMethodName() + ", spendedTime="
                    + methodStatistics.getSpendedTime() + "ms");
            }

            String methodNameTitil = "<method name>,  ";
            List methodNameList = getMethodNames();
            int maxMethodNameLength = methodNameTitil.length();

            if (methodNameList.size() > 0) {
                for (int index = 0; index < methodNameList.size(); index++) {
                    String methodName = (String) methodNameList.get(index);

                    if ((methodName.length() + 3) > maxMethodNameLength) {
                        maxMethodNameLength = methodName.length() + 3;
                    }
                }
            }

            writer.println();
            writer.println(fillSpace(methodNameTitil, maxMethodNameLength)
                + "<arg time>, <max time>, <min time>, <total time>");

            for (int index = 0; index < methodNameList.size(); index++) {
                String methodName = (String) methodNameList.get(index);
                float avgTime = getMethodSpendedAvgTime(methodName);

                String st = String.valueOf(avgTime);
                int periodIndex = st.indexOf('.');

                if ((periodIndex > 0) && ((st.length() - periodIndex) > 3)) {
                    st = st.substring(0, periodIndex + 3);
                    avgTime = Float.valueOf(st).floatValue();
                }

                long maxTime = getMethodSpendedMaxTime(methodName);
                long minTime = getMethodSpendedMinTime(methodName);
                long totalTime = getMethodSpendedTotalTime(methodName);

                writer.println(fillSpace(methodName + ",  ", maxMethodNameLength)
                    + fillSpace(" " + avgTime + ",  ", 12) + fillSpace(" " + maxTime + ",  ", 12)
                    + fillSpace(" " + minTime + ",  ", 12) + fillSpace(" " + totalTime + ",  ", 11));
            }

            writer.println("(unit: millisecond)");
            writer.println();

            writeError(writer);
            writer.println();

            writer.flush();
        }

        protected void writeError(PrintWriter writer) {
            MethodStatistics methodStatistics;
            List failedMethodList = getFailedMethodList();

            if (failedMethodList.size() > 0) {
                writer.println();
                writer.println("Errors: ");

                for (int index = 0; index < failedMethodList.size(); index++) {
                    writer.println("<<---------------------------------------------------------");
                    methodStatistics = (MethodStatistics) failedMethodList.get(index);
                    writer.println(methodStatistics.toString());

                    // Exception e = (Exception) methodStatistics.getResult();
                    // e.printStackTrace();
                    writer.println("--------------------------------------------------------->>");
                    writer.println();
                }
            }
        }

        private String fillSpace(String st, int length) {
            while (st.length() < length) {
                st += " ";
            }

            return st;
        }
    }

    public static class MethodStatistics extends Statistics {
        private String methodName;
        private Object result;

        public MethodStatistics(String methodName) {
            super();
            this.methodName = methodName;
        }

        public MethodStatistics(String methodName, long startTime, long endTime) {
            super(startTime, endTime);
            this.methodName = methodName;
        }

        public MethodStatistics(String methodName, long startTime, long endTime, Object result) {
            super(startTime, endTime);
            this.methodName = methodName;
            this.result = result;
        }

        public String getMethodName() {
            return methodName;
        }

        public Object getResult() {
            return result;
        }

        public void setResult(Object result) {
            this.result = result;
        }

        public boolean isFailed() {
            return (result != null) && result instanceof Exception;
        }

        public String toString() {
            return "method=" + methodName + ", startTime=" + getStartTime() + ", endTime="
            + getEndTime() + ", result=" + result + ". ";
        }
    }

    public static class Counter {
        private volatile int value = 0;

        public synchronized int getActiveThreadNum() {
            return value;
        }

        public synchronized int increaseAndGet() {
            return ++value;
        }

        public synchronized int getAndIncrease() {
            return value++;
        }

        public synchronized int decreaseAndGet() {
            return --value;
        }

        public synchronized int getAndDecrease() {
            return value--;
        }
    }
}
分享到:
评论

相关推荐

    BURNINTEST--硬件检测工具

    PassMark BurnInTest V5.3 Copyright (C) 1999-2008 PassMark Software All Rights Reserved http://www.passmark.com Overview ======== Passmark's BurnInTest is a software tool that allows all the major sub...

    eNetTest 网管内网单机测速工具

    eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速工具eNetTest 网管内网单机测速...

    parasoft c++Test9.0破解

    最好用的单元测试工具,除了这里你是找不到9.0版本的破解的。 ... 独立的版本破解: ... 把lic_client.jar复制到 ... c:\Program Files (x86)\Parasoft\Test\9.0\plugins\...这个是:plugins-c++Test For Visual Studio.7z

    Parasoft c++Test9.0破解文件

    c:\Program Files (x86)\Parasoft\C++test for Visual Studio\9.0\plugins\ 这个目录中 把plugins-Test for Virsual Studio.7z 中的文件覆盖到 c:\Program Files (x86)\Parasoft\Test for Visual Studio\9.0\...

    Modeltest 使用说明

    Modeltest 使用说明 Modeltest 是一个选择核苷酸替代模型的软件,通过和 PAUP 配合使用,可以选择出合适的 MODEL,并同时计算出相关参数。下面是 Modeltest 的使用说明和相关知识点: 一、Modeltest 概述 * Model...

    Parasoft C++Test 9.5

    Parasoft C++Test 9.5是一款由Parasoft公司开发的专业自动化白盒测试工具,专注于C++编程语言的测试。它集成了多种测试策略,包括静态代码分析、动态测试、单元测试、代码覆盖率分析以及缺陷预防等功能,旨在提高...

    speedtest服务器搭建教程

    (speedtest服务器搭建教程) 本篇教程旨在指导读者搭建speedtest服务器,通过安装PHPStudy、配置WNMP和Nginx、下载并配置speedtest测速平台,实现本地测速功能。 一、 PHPStudy 安装和配置 PHPStudy 是一个集成...

    ECU-Test高级教程

    ### ECU-Test高级教程知识点解析 #### 一、ECU-Test概述 **ECU-Test**是一款专为汽车电子控制单元(ECU)开发与验证而设计的强大工具。它支持自动化测试流程,并能有效管理和控制整个测试环境,极大地提高了ECU开发...

    google test框架使用中文文档

    Google Test是Google开发的一款强大的C++测试框架,它使得C++开发者能够编写单元测试和集成测试,以确保代码的质量和稳定性。本文档将详细介绍Google Test框架的使用方法,包括基本概念、断言、测试套件、测试用例、...

    Parasoftc++ Test 9.0破解文件

    最好用的单元测试工具,除了这里你是找不到9.0版本的破解的。 ... 独立的版本破解: ... 把lic_client.jar复制到 ... c:\Program Files (x86)\Parasoft\Test\9.0\plugins\...这个是:( plugins-Test for Virsual Studio.7z )

    Test Track Client 使用说明(新)

    Test Track Client 使用说明 Test Track 是一个功能强大且实用的BUG管理软件,能够帮助测试工程师、开发工程师、开发主管和项目管理人员等角色更好地管理和跟踪项目中的BUG。该软件具有强大的管理功能和灵活的配置...

    Test Bench 经典教程.rar

    Test Bench是电子设计自动化(EDA)领域中的一个重要概念,主要用于验证数字集成电路的设计。在硬件描述语言(HDL,如Verilog或VHDL)中,Test Bench是模拟真实硬件环境来测试设计功能的一个虚拟平台。它能帮助...

    CAN Test V2.53软件使用说明

    CAN Test V2.53 软件使用说明 CAN Test V2.53 软件是一款功能强大且易用的CAN总线测试工具,旨在帮助用户快速地测试和诊断CAN总线设备。以下是CAN Test V2.53 软件使用说明的详细知识点: 软件安装 CAN Test 软件...

    ECU-TEST基本教程

    ### ECU-TEST基本教程知识点概述 #### 一、ECU-TEST简介 ECU-TEST是一款由Vector公司开发的专业汽车电子控制单元(Electronic Control Unit, ECU)测试工具,它能够实现对ECU进行全面而深入的功能性测试,并且支持...

    Parasoft C++ test 9.2官方用户手册_eclipse_中文版

    《Parasoft C++test 9.2官方用户手册_eclipse_中文版》是一本详尽的指南,专为使用C++test工具的开发者提供在Eclipse集成开发环境中的使用方法。C++test是一款强大的静态代码分析和单元测试工具,旨在提高C++软件的...

    CANTest_Setup_V2.70.zip

    CANTest_Setup_V2.70.zip 是一个包含周立功CAN调试工具的软件安装包。这个工具主要用于汽车电子系统中的控制器局域网络(Controller Area Network, CAN)的测试和调试。CAN总线是一种广泛应用的多节点通信协议,尤其...

    C++test(VS2010插件版)简明操作手册-亲测可用.doc

    C++test简明操作手册 C++test是一款功能强大的测试工具,旨在帮助开发者编写高质量的代码。作为Parasoft公司的旗舰产品,C++test提供了全面的测试解决方案,涵盖了静态测试、动态测试、测试用例生成等多方面的测试...

    PortTest串口调试工具

    **串口调试工具——PortTest详解** 在计算机通信领域,串行端口(Serial Port)是一种常见的硬件接口,用于设备间的通信。PortTest是一款专为串口调试设计的实用工具,它可以帮助用户检测和测试串口通讯功能,确保...

Global site tag (gtag.js) - Google Analytics