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

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

fisco bcos java-sdk-demo PerformanceOk.java 源码解析阅读注释

软件工程小施同学 发布时间:2021-04-30 11:29:13 ,浏览量:0

 

/**
 * Copyright 2014-2020 [fisco-dev]
 *
 * 

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of the License at * *

http://www.apache.org/licenses/LICENSE-2.0 * *

Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package org.fisco.bcos.sdk.demo.perf; import com.google.common.util.concurrent.RateLimiter; import java.math.BigInteger; import java.net.URL; import java.util.concurrent.atomic.AtomicInteger; import org.fisco.bcos.sdk.BcosSDK; import org.fisco.bcos.sdk.BcosSDKException; import org.fisco.bcos.sdk.client.Client; import org.fisco.bcos.sdk.demo.contract.Ok; import org.fisco.bcos.sdk.demo.perf.callback.PerformanceCallback; import org.fisco.bcos.sdk.demo.perf.collector.PerformanceCollector; import org.fisco.bcos.sdk.model.ConstantConfig; import org.fisco.bcos.sdk.model.TransactionReceipt; import org.fisco.bcos.sdk.transaction.model.exception.ContractException; import org.fisco.bcos.sdk.utils.ThreadPoolService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 压测串行转账合约 */ public class PerformanceOk { // 日志 private static Logger logger = LoggerFactory.getLogger(PerformanceOk.class); // 已经发送的交易数 private static AtomicInteger sendedTransactions = new AtomicInteger(0); private static void Usage() { // # count: 压测的交易总量 // # tps: 压测QPS // # groupId: 压测的群组ID System.out.println(" Usage:"); System.out.println( " \t java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.demo.perf.PerformanceOk [count] [tps] [groupId]."); } public static void main(String[] args) { try { // 配置文件 String configFileName = ConstantConfig.CONFIG_FILE_NAME; URL configUrl = PerformanceOk.class.getClassLoader().getResource(configFileName); if (configUrl == null) { System.out.println("The configFile " + configFileName + " doesn't exist!"); return; } if (args.length < 3) { Usage(); return; } // # count: 压测的交易总量 // # tps: 压测QPS // # groupId: 压测的群组ID Integer count = Integer.valueOf(args[0]); Integer qps = Integer.valueOf(args[1]); Integer groupId = Integer.valueOf(args[2]); System.out.println( "====== PerformanceOk trans, count: " + count + ", qps:" + qps + ", groupId: " + groupId); String configFile = configUrl.getPath(); BcosSDK sdk = BcosSDK.build(configFile); // build the client // 构建客户端 Client client = sdk.getClient(groupId); // deploy the HelloWorld // 部署智能合约 System.out.println("====== Deploy Ok ====== "); // 获取加密套件,获取加密密钥对 Ok ok = Ok.deploy(client, client.getCryptoSuite().getCryptoKeyPair()); System.out.println( "====== Deploy Ok succ, address: " + ok.getContractAddress() + " ====== "); // 性能收集器 PerformanceCollector collector = new PerformanceCollector(); // 需要收集的个数 collector.setTotal(count); // 设置发送的速率 RateLimiter limiter = RateLimiter.create(qps); // 分布门槛 Integer area = count / 10; // 交易总数 final Integer total = count; System.out.println("====== PerformanceOk trans start ======"); ThreadPoolService threadPoolService = new ThreadPoolService( "PerformanceOk", sdk.getConfig().getThreadPoolConfig().getMaxBlockingQueueSize()); for (Integer i = 0; i < count; ++i) { // 是否能够发送 limiter.acquire(); threadPoolService .getThreadPool() .execute( new Runnable() { @Override public void run() { // 回调 PerformanceCallback callback = new PerformanceCallback(); // 如果这个时间设为 0,就代表立即插入队列,但并不是立即执行,仍然要等待前面代码执行完毕 callback.setTimeout(0); // 设置性能收集器 callback.setCollector(collector); try { // 发送交易 ok.trans(new BigInteger("4"), callback); } catch (Exception e) { // 交易回执 TransactionReceipt receipt = new TransactionReceipt(); receipt.setStatus("-1"); callback.onResponse(receipt); logger.info(e.getMessage()); } // 当前已经发送的交易数 int current = sendedTransactions.incrementAndGet(); if (current >= area && ((current % area) == 0)) { System.out.println( "Already sended: " + current + "/" + total + " transactions"); } } }); } // wait to collect all the receipts // 是否已经收齐所有交易回执 while (!collector.getReceived().equals(count)) { Thread.sleep(1000); } threadPoolService.stop(); System.exit(0); } catch (BcosSDKException | ContractException | InterruptedException e) { System.out.println( "====== PerformanceOk test failed, error message: " + e.getMessage()); System.exit(0); } } }

 

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

微信扫码登录

0.0424s