Hyperledger Caliper是一个通用的区块链性能测试框架,它允许用户使用自定义的用例测试不同的区块链解决方案,并得到一组
性能测试结果。
Hyperledger Besu
Hyperledger Burrow
Ethereum
Hyperledger Fabric
FISCO BCOS
Hyperledger Iroha
Hyperledger Sawtooth
交易/读吞吐量
交易/读延迟:最小、最大、平均、百分比
资源消耗:CPU、内存、网络IO…
Hyperledger Caliper系统架构
Caliper是一个可以对不同区块链平台进行基准测试的通用框架
Caliper 设计时考虑了伸缩性和可扩展性,因此很容易和主流的运维监控系统集成。
Caliper的多区块链平台支持能力
Caliper的主进程与工作进程
Caliper的分布式处理能力
安装Hyperledger Caliper
你也可以直接克隆官方代码然后从源码进行安装:
用NPM安装Caliper
用Docker安装Caliper
从源代码安装Caliper
使用Caliper命令行工具
Caliper基准测试配置
配置Caliper观察者
配置Caliper监视指标
Caliper基准测试配置示例
编写Caliper工作负载模块
Caliper 官方仓库地址
Caliper GitHub下载比较慢 可以使用我的Gitee仓库地址下载
git clone https://gitee.com/successhp/caliper-benchmarks
npm node版本一定要对应统一好 不然非常麻烦,依赖下载不下来!!!
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - && sudo apt-get install -y nodejs
npm install [email protected] -g
sudo npm install -g [email protected]
cd ../fabric-samples/test-network
./network.sh up createChannel -ca -s couchdb
不知道如何部署链码,可以参考学习我这篇文章
Fabric 超级账本学习【2】Fabric2.4网络环境下部署自己编写的go语言链码并实例化测试(手把手教学,步骤超详细)
先部署我在 Fabric 超级账本学习【2】 中编写的链码,使用资产链码进行测试
./network.sh deployCC -ccn asseth -ccp ./mychaincode/asset -ccl go
来到fabric-sample 同级目录中,新建如下文件和文件夹
mkdir caliper-workspace
cd caliper-workspace
mkdir networks benchmarks workload
cd networks
touch networkConfig.yaml
cd workload
touch readAsset.js
证书文件路径一定要修改成自己的!!!!不然后面启动会报错
# 该文件本质上是一个网络连接配置,SDK使用该文件连接到Fabric网络
name: Calier test
version: "2.0.0"
caliper:
blockchain: fabric
channels:
- channelName: mychannel
contracts:
- id: asseth # 链码名
organizations:
- mspid: Org1MSP
identities:
certificates:
- name: 'User1'
clientPrivateKey:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk'
clientSignedCert:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]'
connectionProfile:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'
discover: true
'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{
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Creating asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'CreateAsset',
invokerIdentity: 'User1',
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: 'User1',
contractArguments: [`${this.workerIndex}_${randomId}`],
readOnly: true
};
await this.sutAdapter.sendRequests(myArgs);
}
async cleanupWorkloadModule() {
for (let i=0; i{
const assetID = `${this.workerIndex}_${i}`;
console.log(`Worker ${this.workerIndex}: Deleting asset ${assetID}`);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'DeleteAsset',
invokerIdentity: 'User1',
contractArguments: [assetID],
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
}
}
function createWorkloadModule() {
return new MyWorkload();
}
module.exports.createWorkloadModule = createWorkloadModule;
test:
# 基准测试的名称
name: basic-contract-benchmark
# 基准测试详细描述
description: test benchmark
workers:
# 指定用于执行工作负载的进程数
number: 2
# 描述每一个测试回合的设置
rounds:
# 测试标签,一般为测试链码的名称
- label: readAsset
description: Read asset benchmark
# caliper提交交易的时间
txDuration: 30
# 提交交易的速度控制
rateControl:
# 速度控制类型,有fixed-rate、fixed-load等,解释可参见https://hyperledger.github.io/caliper/v0.5.0/rate-controllers/
type: fixed-load
opts:
transactionLoad: 2
workload:
# 描述需要测试的工作负载文件路径
module: workload/readAsset.js
# 需要传递给工作负责文件的参数
arguments:
assets: 10
contractId: asseth # 链码名
然后进入 caliper-benchmarks文件夹
cd caliper-benchmarks
执行命令,安装最新版的hyperledger caliper
npm install --only=prod @hyperledger/caliper-cli
绑定我们的hyperledger fabric2.4.4注意要对应自己的fabric版本
npx caliper bind --caliper-bind-sut fabric:2.4
查看版本
npx caliper --version
先到caliper-benchmarks目录下
cd ../../caliper-benchmarks/
开始测试:
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test
works/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test
2023.03.29-14:09:25.216 info [caliper] [cli-launch-manager] Set workspace path: /opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace
2023.03.29-14:09:25.217 info [caliper] [cli-launch-manager] Set benchmark configuration path: /opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/benchmarks/myAssetBenchmark.yaml
2023.03.29-14:09:25.217 info [caliper] [cli-launch-manager] Set network configuration path: /opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/networks/networkConfig.yaml
2023.03.29-14:09:25.217 info [caliper] [cli-launch-manager] Set SUT type: fabric
2023.03.29-14:09:25.221 info [caliper] [benchmark-validator] No observer specified, will default to `none`
2023.03.29-14:09:25.221 info [caliper] [caliper-engine] Starting benchmark flow
2023.03.29-14:09:25.221 info [caliper] [caliper-engine] Skipping start commands due to benchmark flow conditioning
2023.03.29-14:09:25.222 info [caliper] [caliper-engine] Skipping initialization phase due to benchmark flow conditioning
2023.03.29-14:09:25.222 info [caliper] [caliper-engine] Skipping install smart contract phase due to benchmark flow conditioning
2023.03.29-14:09:25.642 info [caliper] [FabricConnectorFactory] Initializing peer gateway connector compatible with installed fabric-gateway SDK: 1.0.1
2023.03.29-14:09:25.660 error [caliper] [caliper-engine] Error while performing "test" step: Error: path property /opt/gopath/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected] does not point to a file that exists for clientSignedCert for name User1 in organization Org1MSP
at IdentityManager._extractPEMFromPath (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManager.js:308:23)
at async IdentityManager._extractPEMFromPathOrPEMDefinition (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManager.js:280:19)
at async IdentityManager._extractIdentitiesFromCertificateAndPrivateKeyArray (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManager.js:258:33)
at async IdentityManager._parseOrganizations (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManager.js:187:25)
at async IdentityManager.initialize (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManager.js:44:9)
at async IdentityManagerFactory.create (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/identity-management/IdentityManagerFactory.js:33:9)
at async ConnectorConfiguration.parseConfiguration (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/connector-configuration/ConnectorConfiguration.js:53:32)
at async ConnectorConfigurationFactory.create (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/connector-configuration/ConnectorConfigurationFactory.js:34:9)
at async CaliperEngine.connectorFactory [as adapterFactory] (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-fabric/lib/FabricConnectorFactory.js:135:36)
at async CaliperEngine.run (/opt/gopath/src/github.com/hyperledger/fabric/scripts/caliper-workspace/node_modules/@hyperledger/caliper-core/lib/manager/caliper-engine.js:149:53)
2023.03.29-14:09:25.660 info [caliper] [caliper-engine] Skipping end command due to benchmark flow conditioning
2023.03.29-14:09:25.660 error [caliper] [cli-launch-manager] Benchmark failed with error code 6
Usage:
caliper launch manager --caliper-bind-sut fabric:2.2 [other options]
Options:
--help, -h Show usage information [布尔]
--version Show version information [布尔]
--caliper-bind-sut The name and version of the platform to bind to [字符串]
--caliper-bind-cwd The working directory for performing the SDK install [字符串]
--caliper-bind-args Additional arguments to pass to "npm install". Use the "=" notation when setting this parameter [字符串]
--caliper-bind-file Yaml file to override default (supported) package versions when binding an SDK [字符串]
报错原因证书路径错误
正确的 networkConfig.yaml 配置文件
name: Calier test
version: "2.0.0"
caliper:
blockchain: fabric
channels:
- channelName: mychannel
contracts:
- id: asseth # 链码名
organizations:
- mspid: Org1MSP
identities:
certificates:
- name: 'User1'
clientPrivateKey:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/1354d4259c6f6502d0a3794fde2a3c137f5e6f365a090b83a66ed3eb50ebec55_sk'
clientSignedCert:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/cert.pem'
connectionProfile:
path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'
discover: true
再次启动,启动成功!!!
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test