您当前的位置: 首页 >  Java
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

fisco bcos PerformanceDTCollector.java 源码解析阅读注释

软件工程小施同学 发布时间:2021-04-29 16:26:37 ,浏览量:0

package org.fisco.bcos.channel.test.parallel.parallelok;

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PerformanceDTCollector {

    // 日志
    private static Logger logger = LoggerFactory.getLogger(PerformanceDTCollector.class);
    // 错误信息
    private static HashMap errorInfos = new HashMap();

    // 交易总数
    private Integer total = 0;
    // 用户管理器
    private DagUserMgr dagUserMrg;
    // 性能测试
    private PerformanceDTTest PerformanceDTTest;

    public PerformanceDTCollector() {
        errorInfos.put("0x0", "None");
        errorInfos.put("0x1", "Unknown");
        errorInfos.put("0x2", "BadRLP");
        errorInfos.put("0x3", "InvalidFormat");
        errorInfos.put("0x4", "OutOfGasIntrinsic");
        errorInfos.put("0x5", "InvalidSignature");
        errorInfos.put("0x6", "InvalidNonce");
        errorInfos.put("0x7", "NotEnoughCash");
        errorInfos.put("0x8", "OutOfGasBase");
        errorInfos.put("0x9", "BlockGasLimitReached");
        errorInfos.put("0xa", "BadInstruction");
        errorInfos.put("0xb", "BadJumpDestination");
        errorInfos.put("0xc", "OutOfGas");
        errorInfos.put("0xd", "OutOfStack");
        errorInfos.put("0xe", "StackUnderflow");
        errorInfos.put("0xf", "NonceCheckFail");
        errorInfos.put("0x10", "BlockLimitCheckFail");
        errorInfos.put("0x11", "FilterCheckFail");
        errorInfos.put("0x12", "NoDeployPermission");
        errorInfos.put("0x13", "NoCallPermission");
        errorInfos.put("0x14", "NoTxPermission");
        errorInfos.put("0x15", "PrecompiledError");
        errorInfos.put("0x16", "RevertInstruction");
        errorInfos.put("0x17", "InvalidZeroSignatureFormat");
        errorInfos.put("0x18", "AddressAlreadyUsed");
        errorInfos.put("0x19", "PermissionDenied");
        errorInfos.put("0x1a", "CallAddressError");
        errorInfos.put("0x1b", "GasOverflow");
        errorInfos.put("0x1c", "TxPoolIsFull");
    }

    // 得到性能测试
    public PerformanceDTTest getPerformanceDTTest() {
        return PerformanceDTTest;
    }

    // 设置性能测试
    public void setPerformanceDTTest(PerformanceDTTest PerformanceDTTest) {
        this.PerformanceDTTest = PerformanceDTTest;
    }

    // 得到交易总数
    public Integer getTotal() {
        return total;
    }

    // 设置交易总数
    public void setTotal(Integer total) {
        this.total = total;
    }

    // 得到用户管理器
    public DagUserMgr getDagUserMrg() {
        return dagUserMrg;
    }

    // 设置用户管理器
    public void setDagUserMrg(DagUserMgr dagUserMrg) {
        this.dagUserMrg = dagUserMrg;
    }

    // 是否已经收到所有交易的返回
    public boolean isEnd() {
        return received.intValue() >= total;
    }

    // 每个交易的回执和时间花费
    public void onMessage(TransactionReceipt receipt, Long cost) {
        try {

            // 如果错误,则错误+1
            if (!receipt.isStatusOK()) {
                // System.out.println("receipt error! status: " + receipt.getStatus());
                error.addAndGet(1);
            }

            // |received: 10%
            // |received: 20%
            if ((received.get() + 1) % (total / 10) == 0) {
                System.out.println(
                        "                                                       |received:"
                                + String.valueOf((received.get() + 1) * 100 / total)
                                + "%");
            }

            // 将该交易进行归纳
            // 0    < time <  50ms   : 1873  : 1.873%
            // 50   < time <  100ms  : 431  : 0.43099999999999994%
            // 100  < time <  200ms  : 1011  : 1.011%
            // 200  < time <  400ms  : 1864  : 1.864%
            // 400  < time <  1000ms : 774  : 0.774%

            if (cost < 50) {
                less50.incrementAndGet();
            } else if (cost < 100) {
                less100.incrementAndGet();
            } else if (cost < 200) {
                less200.incrementAndGet();
            } else if (cost < 400) {
                less400.incrementAndGet();
            } else if (cost < 1000) {
                less1000.incrementAndGet();
            } else if (cost < 2000) {
                less2000.incrementAndGet();
            } else if (cost < 5000) {
                less5000.incrementAndGet();
            } else if (cost < 10000) {
                less10000.incrementAndGet();
            } else if (cost < 30000) {
                less30000.incrementAndGet();
            } else if (cost < 60000) {
                less60000.incrementAndGet();
            } else {
                timeout60000.incrementAndGet();
            }

            // 总耗时
            totalCost.addAndGet(cost);

            if (received.incrementAndGet() >= total) {
                System.out.println("total");

                //
                Long totalTime = System.currentTimeMillis() - startTimestamp;

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

                System.out.println("Total transactions:  " + String.valueOf(total));
                System.out.println("Total time: " + String.valueOf(totalTime) + "ms");
                System.out.println(
                        "TPS(include error requests): "
                                + String.valueOf(total / ((double) totalTime / 1000)));
                System.out.println(
                        "TPS(exclude error requests): "
                                + String.valueOf(
                                        (double) (total - error.get())
                                                / ((double) totalTime / 1000)));
                System.out.println(
                        "Avg time cost: " + String.valueOf(totalCost.get() / total) + "ms");
                System.out.println(
                        "Error rate: "
                                + String.valueOf(
                                        ((double) error.get() / (double) received.get()) * 100)
                                + "%");
                System.out.println(
                        "Return Error rate: "
                                + String.valueOf(
                                        ((double) ret_error.get() / (double) received.get()) * 100)
                                + "%");

                System.out.println("Time area:");
                System.out.println(
                        "0    < time <  50ms   : "
                                + String.valueOf(less50)
                                + "  : "
                                + String.valueOf((double) less50.get() / total * 100)
                                + "%");
                System.out.println(
                        "50   < time <  100ms  : "
                                + String.valueOf(less100)
                                + "  : "
                                + String.valueOf((double) less100.get() / total * 100)
                                + "%");
                System.out.println(
                        "100  < time <  200ms  : "
                                + String.valueOf(less200)
                                + "  : "
                                + String.valueOf((double) less200.get() / total * 100)
                                + "%");
                System.out.println(
                        "200  < time <  400ms  : "
                                + String.valueOf(less400)
                                + "  : "
                                + String.valueOf((double) less400.get() / total * 100)
                                + "%");
                System.out.println(
                        "400  < time <  1000ms : "
                                + String.valueOf(less1000)
                                + "  : "
                                + String.valueOf((double) less1000.get() / total * 100)
                                + "%");
                System.out.println(
                        "1 < time <  2s : "
                                + String.valueOf(less2000)
                                + "  : "
                                + String.valueOf((double) less2000.get() / total * 100)
                                + "%");
                System.out.println(
                        "2 < time <  5s : "
                                + String.valueOf(less5000)
                                + "  : "
                                + String.valueOf((double) less5000.get() / total * 100)
                                + "%");
                System.out.println(
                        "5 < time <  10s : "
                                + String.valueOf(less10000)
                                + "  : "
                                + String.valueOf((double) less10000.get() / total * 100)
                                + "%");
                System.out.println(
                        "10 < time <  30s : "
                                + String.valueOf(less30000)
                                + "  : "
                                + String.valueOf((double) less30000.get() / total * 100)
                                + "%");
                System.out.println(
                        "30 < time <  60s : "
                                + String.valueOf(less60000)
                                + "  : "
                                + String.valueOf((double) less60000.get() / total * 100)
                                + "%");
                System.out.println(
                        "time > 60s           : "
                                + String.valueOf(timeout60000)
                                + "  : "
                                + String.valueOf((double) timeout60000.get() / total * 100)
                                + "%");
            }

        } catch (Exception e) {
            logger.error("error:", e);
            System.exit(0);
        }
    }

    private AtomicLong less50 = new AtomicLong(0);
    private AtomicLong less100 = new AtomicLong(0);
    private AtomicLong less200 = new AtomicLong(0);
    private AtomicLong less400 = new AtomicLong(0);
    private AtomicLong less1000 = new AtomicLong(0);
    private AtomicLong less2000 = new AtomicLong(0);
    private AtomicLong less5000 = new AtomicLong(0);
    private AtomicLong less10000 = new AtomicLong(0);
    private AtomicLong less30000 = new AtomicLong(0);
    private AtomicLong less60000 = new AtomicLong(0);
    private AtomicLong timeout60000 = new AtomicLong(0);
    private AtomicLong totalCost = new AtomicLong(0);

    private AtomicInteger received = new AtomicInteger(0);

    // 收到的交易总数
    public AtomicInteger getReceived() {
        return received;
    }

    // 设置收到的交易总数
    public void setReceived(AtomicInteger received) {
        this.received = received;
    }

    private AtomicInteger error = new AtomicInteger(0);
    private AtomicInteger ret_error = new AtomicInteger(0);

    // 开始时间戳
    private Long startTimestamp = System.currentTimeMillis();

    // 得到开始时间戳
    public Long getStartTimestamp() {
        return startTimestamp;
    }

    // 设置开始时间戳
    public void setStartTimestamp(Long startTimestamp) {
        this.startTimestamp = startTimestamp;
    }
}

 

关注
打赏
1665320866
查看更多评论
立即登录/注册

微信扫码登录

0.0411s