目录
Hyperledger Caliper是区块链基准测试工具,它允许用户用一组预定义的用例来衡量区块链实现的性能。
目前支持的区块链解决方案:
- Hyperledger Besu, 利用以太坊适配器。
- Hyperledger Fabric v1.X, v2.X
- Ethereum
- FISCO BCOS
支持的性能指标:
- Transaction/read throughput
- Transaction/read latency (minimum, maximum, average, percentile)
- Resource consumption (CPU, Memory, Network IO, …)
本文环境:
- Ubuntu 18.04
- Fabric 2.2.0
- docker,docker-compose
- npm
一、创建并初始化Fabric网络
cd test-network
./network.sh up createChannel
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
二、创建Caliper工作区
当前在test-network目录下,回到fabric-samples的同一级目录,在fabric-samples目录的同一级别创建一个名为caliper-workspace的文件夹,然后在caliper-workspace文件夹中,分别创建三个名为networks、benchmarks和workload的文件夹。
mkdir -p caliper-workspace/{networks,benchmarks,workload}
1.初始化项目
在caliper-workspace目录中
npm init -y
执行完后,在当前目录生成了package.json文件。
2.安装caliper-cli
在caliper-workspace目录中,使用以下终端命令安装caliper CLI:
#版本要匹配,0.4对应fabric2.x,0.3对应fabric1.4
npm install --only=prod @hyperledger/caliper-cli@0.4.0
执行完后,会将依赖下载到当前目录下的node_modules文件下。
3.查看帮助
$ npx caliper --help
caliper <command>
Commands:
caliper bind [options] Bind Caliper to a specific SUT and its SDK version
caliper launch <subcommand> Launch a Caliper process either in a manager or worker role.
caliper unbind [options] Unbind Caliper from a previously bound SUT and its SDK version
caliper completion generate completion script
Options:
--help, -h Show usage information [boolean]
--version Show version information [boolean]
Examples:
caliper bind
caliper unbind
caliper launch manager
caliper launch worker
For more information on Hyperledger Caliper: https://hyperledger.github.io/caliper/
4.使用以下终端命令绑定SDK
npx caliper bind --caliper-bind-sut fabric:2.1
三、构建网络配置文件
在networks文件夹下创建一个名为networkConfig.json的文件
cd networks
touch networkConfig.json
修改networkConfig.json文件中内容如下:
{
"version" : "1.0",
"name": "Caliper test",
"caliper" : {
"blockchain": "fabric"
},
"clients": {
"Admin@org1.example.com": {
"client": {
"credentialStore": {
"path": "/tmp/org1",
"cryptoStore": {
"path": "/tmp/org1"
}
},
"organization": "Org1",
"clientPrivateKey": {
"path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
},
"clientSignedCert": {
"path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
},
"connection": {
"timeout": {
"peer": {
"endorser": "300"
}
}
}
}
}
},
"channels": {
"mychannel": {
"created" : true,
"contracts": [
{
"id":"basic",
"version":"1.0.0"
}
]
}
},
"organizations":{
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
]
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpcs://localhost:7051",
"tlsCACerts": {
"pem": "-----BEGIN CERTIFICATE-----\n<UNIQUE CONTENT>\n-----END CERTIFICATE-----\n"
},
"grpcOptions": {
"ssl-target-name-override": "peer0.org1.example.com",
"hostnameOverride": "peer0.org1.example.com"
}
}
}
}
其中"pem": "-----BEGIN CERTIFICATE-----\n<UNIQUE CONTENT>\n-----END CERTIFICATE-----\n"
中的内容需要根据自己网络中生成的证书内容来更改。更改的内容为fabric-samples/test-network/organizations/peerOrganizations/org1.example.com
里面connection-org1.json中的以下:
四、构建测试工作负载模块
在workload文件夹中,创建一个名为readAsset.js的文件
cd workload
touch readAsset.js
修改内容为:
'use strict';
const { WorkloadModuleBase } = require('@hyperledger/caliper-core');
class MyWorkload extends WorkloadModuleBase {
constructor() {
super();
}
async initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext) {
await super.initializeWorkloadModule(workerIndex, totalWorkers, roundIndex, roundArguments, sutAdapter, sutContext);
for (let i=0; i<this.roundArguments.assets; i++) {
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Creating asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'CreateAsset',
invokerIdentity: 'Admin@org1.example.com',
contractArguments: [assetID,'blue','20','penguin','500'],
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
}
async submitTransaction() {
const randomId = Math.floor(Math.random()*this.roundArguments.assets);
const myArgs = {
contractId: this.roundArguments.contractId,
contractFunction: 'ReadAsset',
invokerIdentity: 'Admin@org1.example.com',
contractArguments: [`${this.workerIndex}_${randomId}`],
readOnly: true
};
await this.sutAdapter.sendRequests(myArgs);
}
async cleanupWorkloadModule() {
for (let i=0; i<this.roundArguments.assets; i++) {
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Deleting asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'DeleteAsset',
invokerIdentity: 'Admin@org1.example.com',
contractArguments: [assetID],
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
}
}
function createWorkloadModule() {
return new MyWorkload();
}
module.exports.createWorkloadModule = createWorkloadModule;
五、构建基准测试配置文件
在benchmarks文件夹下创建一个名为myAssetBenchmark.yaml
cd benchmarks
touch myAssetBenchmark.yaml
修改内容为:
test:
name: basic-contract-benchmark
description: test benchmark
workers:
type: local
number: 2
rounds:
- label: readAsset
description: Read asset benchmark
txDuration: 30
rateControl:
type: fixed-load
opts:
transactionLoad: 2
workload:
module: workload/readAsset.js
arguments:
assets: 10
contractId: basic
monitors:
resource:
- module: docker
options:
interval: 5
containers:
- all
六、运行Caliper基准测试
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.json --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test --caliper-fabric-gateway-enabled --caliper-fabric-gateway-discovery
2021.04.14-20:17:04.617 info [caliper] [cli-launch-manager] Set workspace path: /home/yulin/go/src/github.com/hyperledger/fabric2.2.0/caliper-workspace
2021.04.14-20:17:04.618 info [caliper] [cli-launch-manager] Set benchmark configuration path: /home/yulin/go/src/github.com/hyperledger/fabric2.2.0/caliper-workspace/benchmarks/myAssetBenchmark.yaml
2021.04.14-20:17:04.618 info [caliper] [cli-launch-manager] Set network configuration path: /home/yulin/go/src/github.com/hyperledger/fabric2.2.0/caliper-workspace/networks/networkConfig.json
2021.04.14-20:17:04.619 info [caliper] [cli-launch-manager] Set SUT type: fabric
2021.04.14-20:17:04.700 info [caliper] [benchmark-validator] No observer specified, will default to `none`
2021.04.14-20:17:04.702 info [caliper] [caliper-engine] Starting benchmark flow
2021.04.14-20:17:05.305 info [caliper] [fabric-connector] Initializing gateway connector compatible with installed SDK: 2.1.0
2021.04.14-20:17:05.628 info [caliper] [caliper-engine] Skipping start commands due to benchmark flow conditioning
2021.04.14-20:17:05.628 info [caliper] [caliper-engine] Skipping initialization phase due to benchmark flow conditioning
2021.04.14-20:17:05.628 info [caliper] [caliper-engine] Skipping install smart contract phase due to benchmark flow conditioning
2021.04.14-20:17:05.632 info [caliper] [monitor.js] Attempting to create resource monitor of type docker
2021.04.14-20:17:05.634 info [caliper] [default-observer] Observer interval set to 5000 seconds
2021.04.14-20:17:05.636 info [caliper] [round-orchestrator] Preparing worker connections
2021.04.14-20:17:05.637 info [caliper] [worker-orchestrator] Launching worker 1 of 2
2021.04.14-20:17:05.647 info [caliper] [worker-orchestrator] Launching worker 2 of 2
2021.04.14-20:17:05.656 info [caliper] [worker-orchestrator] Messenger not configured, entering configure phase...
2021.04.14-20:17:05.658 info [caliper] [worker-orchestrator] No existing workers detected, entering worker launch phase...
2021.04.14-20:17:05.659 info [caliper] [worker-orchestrator] Waiting for 2 workers to be connected...
2021.04.14-20:17:06.184 info [caliper] [cli-launch-worker] Set workspace path: /home/yulin/go/src/github.com/hyperledger/fabric2.2.0/caliper-workspace
.........
2021.04.14-20:18:22.711 info [caliper] [connectors/v2/fabric-gateway] disconnecting gateway for user Admin@org1.example.com
2021.04.14-20:18:22.712 info [caliper] [connectors/v2/fabric-gateway] disconnecting gateway for user Admin@org1.example.com
2021.04.14-20:18:22.717 info [caliper] [worker-message-handler] Worker#0 finished Round#0
2021.04.14-20:18:22.719 info [caliper] [worker-message-handler] Worker#1 finished Round#0
2021.04.14-20:18:27.723 info [caliper] [default-observer] [readAsset Round 0 Transaction Info] - Submitted: 0 Succ: 0 Fail:0 Unfinished:0
2021.04.14-20:18:27.725 info [caliper] [report-builder] ### Test result ###
2021.04.14-20:18:27.775 info [caliper] [report-builder]
+-----------+------+------+-----------------+-----------------+-----------------+-----------------+------------------+
| Name | Succ | Fail | Send Rate (TPS) | Max Latency (s) | Min Latency (s) | Avg Latency (s) | Throughput (TPS) |
|-----------|------|------|-----------------|-----------------|-----------------|-----------------|------------------|
| readAsset | 4951 | 0 | 167.0 | 0.07 | 0.01 | 0.01 | 167.0 |
+-----------+------+------+-----------------+-----------------+-----------------+-----------------+------------------+
2021.04.14-20:18:27.777 info [caliper] [report-builder] ### docker resource stats ###'
2021.04.14-20:18:27.783 info [caliper] [report-builder]
+-------------------------------------------------------------------------------------------------------+-----------+-----------+------------------+------------------+-----------------+------------------+-----------------+---------------+
| Name | CPU%(max) | CPU%(avg) | Memory(max) [MB] | Memory(avg) [MB] | Traffic In [MB] | Traffic Out [MB] | Disc Write [KB] | Disc Read [B] |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| dev-peer0.org1.example.com-basic_1.0-4ec191e793b27e953ff2ede5a8bcc63152cecb1e4c3f301a26e22692c61967ad | 50.55 | 22.31 | 11.7 | 11.1 | 9.09 | 3.84 | 0.00 | 0.00 |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| dev-peer0.org2.example.com-basic_1.0-4ec191e793b27e953ff2ede5a8bcc63152cecb1e4c3f301a26e22692c61967ad | 4.60 | 0.51 | 9.01 | 8.86 | 0.0435 | 0.0169 | 0.00 | 0.00 |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| cli | 0.00 | 0.00 | 20.7 | 20.7 | 0.0000668 | 0.00 | 0.00 | 0.00 |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer0.org1.example.com | 82.30 | 39.20 | 120 | 119 | 10.8 | 16.4 | 260 | 0.00 |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| orderer.example.com | 1.43 | 0.57 | 17.9 | 17.6 | 0.0995 | 0.176 | 248 | 0.00 |
|-------------------------------------------------------------------------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer0.org2.example.com | 7.13 | 4.66 | 105 | 105 | 0.184 | 0.134 | 260 | 0.00 |
+-------------------------------------------------------------------------------------------------------+-----------+-----------+------------------+------------------+-----------------+------------------+-----------------+---------------+
2021.04.14-20:18:27.785 info [caliper] [round-orchestrator] Finished round 1 (readAsset) in 30.174 seconds
2021.04.14-20:18:27.785 info [caliper] [monitor.js] Stopping all monitors
2021.04.14-20:18:27.888 info [caliper] [report-builder] ### All test results ###
2021.04.14-20:18:27.892 info [caliper] [report-builder]
+-----------+------+------+-----------------+-----------------+-----------------+-----------------+------------------+
| Name | Succ | Fail | Send Rate (TPS) | Max Latency (s) | Min Latency (s) | Avg Latency (s) | Throughput (TPS) |
|-----------|------|------|-----------------|-----------------|-----------------|-----------------|------------------|
| readAsset | 4951 | 0 | 167.0 | 0.07 | 0.01 | 0.01 | 167.0 |
+-----------+------+------+-----------------+-----------------+-----------------+-----------------+------------------+
2021.04.14-20:18:27.959 info [caliper] [report-builder] Generated report with path /home/yulin/go/src/github.com/hyperledger/fabric2.2.0/caliper-workspace/report.html
2021.04.14-20:18:27.960 info [caliper] [monitor.js] Stopping all monitors
2021.04.14-20:18:27.961 info [caliper] [worker-orchestrator] Sending exit message to connected workers
2021.04.14-20:18:27.962 info [caliper] [round-orchestrator] Benchmark finished in 80.67 seconds. Total rounds: 1. Successful rounds: 1. Failed rounds: 0.
2021.04.14-20:18:27.962 info [caliper] [caliper-engine] Skipping end command due to benchmark flow conditioning
2021.04.14-20:18:27.962 info [caliper] [worker-message-handler] Worker#1 is exiting
2021.04.14-20:18:27.962 info [caliper] [cli-launch-manager] Benchmark successfully finished
2021.04.14-20:18:27.962 info [caliper] [worker-message-handler] Worker#0 is exiting
在caliper-workspace目录下生成report.html的性能测试报告。
参考:
https://blog.csdn.net/bean_business/article/details/108937601
https://blog.csdn.net/kk3909/article/details/105490642