前言:Hyperledger caliper 的本质是使用node对被测试网络进行压力测试,因此需要nodejs。本次使用 Hyperledger caliper 0.5 对 fabric 1.4.6进行压测
准备条件:nodejs 16 (略 linux下 解压+环境变量即可)
# 创建工作目录
mkdir /opt/fabric/caliper
# 进入工作目录
cd /opt/fabric/caliper
# 安装 caliper CLI, (如果慢 可以使用代理 sudo npm config set https-proxy http://172.16.10.34:7890)
npm install --only=prod @hyperledger/caliper-cli@0.5.0
配置文件(放在工作空间中:/root/fabric/caliper)
network-config.yaml (配置要测试的网络和组织信息)
name: Caliper Benchmarks
version: "2.0.0"caliper:blockchain: fabricchannels:# channelName of mychannel matches the name of the channel created by test network- channelName: channel1# the chaincodeIDs of all the fabric chaincodes in caliper-benchmarkscontracts:- id: ccmaporganizations:- mspid: Org1MSP# Identities come from cryptogen created material for test-networkidentities:certificates:- name: 'Admin'clientPrivateKey:path: '/opt/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/5ca88db427a2026c0c16fbba1bd17d9e596683f1fa7c8a9b412d88604b63e66d_sk'clientSignedCert:path: '/opt/fabric/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem'connectionProfile:path: 'connection-org1.yaml'discover: true
connection-org1.yaml (配置组织的连接信息)
---
name: first-network-org1
version: 1.0.0
client:organization: Org1connection:timeout:peer:endorser: '300'
organizations:Org1:mspid: Org1MSPpeers:- peer0.org1.example.com
peers:peer0.org1.example.com:url: grpcs://localhost:7051tlsCACerts:path: /opt/fabric/crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pemgrpcOptions:ssl-target-name-override: peer0.org1.example.com
benchmark-config.yaml (配置压力测试的参数)
test:workers:number: 5rounds:- label: Query map.txDuration: 30rateControl:type: fixed-loadopts:transactionLoad: 1000workload:module: workload/query.jsarguments:assets: 2
query.js (在子目录下,绝对路径为:/root/fabric/caliperworkload/query.js) 根据业务编写查询方法或插入方法
'use strict';const { WorkloadModuleBase } = require('@hyperledger/caliper-core');class QueryAssetWorkload extends WorkloadModuleBase {constructor() {super();}async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);}async submitTransaction() {await this.sutAdapter.sendRequests({contractId: 'ccmap', // 你的链码名称contractFunction: 'query', // 查询方法contractArguments: ["a"],readOnly: true});}
} module.exports.createWorkloadModule = () => new QueryAssetWorkload();
执行
npx caliper launch manager --caliper-workspace . --caliper-benchconfig /opt/fabric/caliper/benchmark-config.yaml --caliper-networkconfig /opt/fabric/caliper/network-config.yaml --caliper-flow-only-test --caliper-fabric-gateway-enabled
测试效果如下