根据测试需要,在configtx.yaml中设置合适的参数以及排序服务类型。
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 100
AbsoluteMaxBytes: 99 MB
PreferredMaxBytes: 512 KB
启动网络,以test-network为例
cd test-network
./network.sh up createChannel
./network.sh deployCC -ccn basic -ccl javascript
在fabric-samples同级目录创建几个目录,安装caliper
mkdir -p caliper-workspace/{networks,benchmarks,workload}
cd caliper-workspace
#版本要匹配,0.4对应fabric2.x,0.3对应fabric1.4
npm install --only=prod @hyperledger/caliper-cli@0.4.0 //报了很多错,但不影响!?
npx caliper bind --caliper-bind-sut fabric:2.1 //也报了很多错,也不影响!?
在networks文件夹中创建网络连接配置文件networkConfig.json,根据实际网络来配置,类似下面这样:
{
"caliper": {
"blockchain": "fabric"
},
"clients": {
"Admin@org1.example.com": {
"client": {
"credentialStore": {
"path": "tmp/hfc-kvs/org1",
"cryptoStore": {
"path": "tmp/hfc-kvs/org1"
}
},
"organization": "Org1",
"clientPrivateKey": {
"path": "../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
},
"clientSignedCert": {
"path": "../fabric-samples/first-network/crypto-config/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":"mycc",
"version":"1.0"
}
]
}
},
"name": "test-network-org1",
"organizations":{
"Org1": {
"mspid": "Org1MSP",
"peers": [
"peer0.org1.example.com"
],
"certificateAuthorities": [
"ca.org1.example.com"
],
"adminPrivateKey": {
"path": "../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
},
"signedCert": {
"path": "../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
}
}
},
"peers": {
"peer0.org1.example.com": {
"url": "grpcs://localhost:7051",
"tlsCACerts": {
"path": "../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem"
},
"grpcOptions": {
"ssl-target-name-override": "peer0.org1.example.com",
"hostnameOverride": "peer0.org1.example.com"
}
}
},
"certificateAuthorities": {
"ca.org1.example.com": {
"url": "https://localhost:7054",
"caName": "ca-org1",
"tlsCACerts": {
"path": "../fabric-samples/first-network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem"
},
"httpOptions": {
"verify": false
}
}
},
"version": "1.0"
}
在workload文件夹中创建writeAsset.js,用于测试写入交易的TPS,内容类似下面这样:
'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);
//TODO
}
//单次交易的内容,这个函数会被多次调用,key不能重复,否则会冲突,导致交易提交失败
async submitTransaction() {
const randomId = Math.floor(Math.random()*this.roundArguments.assets);
const request = {
contractId: this.roundArguments.contractId,
contractFunction: 'set', //智能合约中的函数
invokerIdentity: 'Admin@org1.example.com',
contractArguments: ['key_' + randomId,'value'], //参数
readOnly: false
};
await this.sutAdapter.sendRequests(request);
}
async cleanupWorkloadModule() {
//TODO
}
}
function createWorkloadModule() {
return new MyWorkload();
}
module.exports.createWorkloadModule = createWorkloadModule;
在benchmarks文件夹中创建myAssetBenchmark.yaml,内容如下:
#测试参数,根据测试需要进行调整
test:
name: basic-contract-benchmark
description: test benchmark
workers:
type: local
number: 8
rounds:
- label: writeAsset
description: Write asset benchmark
txDuration: 30
rateControl:
type: fixed-load
opts:
transactionLoad: 200
workload:
module: workload/writeAsset.js
arguments:
assets: 500
contractId: mycc
#监测docker容器的资源使用情况
monitors:
resource:
- module: docker
options:
interval: 5
containers:
- all
charting:
bar:
metrics: [Memory(avg), CPU%(avg)]
polar:
metrics: [all]
observer:
type: local
interval: 5
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
最后两个参数不开启的话,需要在网络连接配置文件networkConfig.json中指明orderer的参数。
容器资源使用情况
+-------------------------------------+-----------+-----------+------------------+------------------+-----------------+------------------+-----------------+---------------+
| Name | CPU%(max) | CPU%(avg) | Memory(max) [MB] | Memory(avg) [MB] | Traffic In [MB] | Traffic Out [MB] | Disc Write [MB] | Disc Read [B] |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| dev-peer1.org2.example.com-mycc-1.0 | 2.55 | 1.89 | 9.13 | 9.11 | 8.91 | 2.63 | 0.00 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| dev-peer0.org1.example.com-mycc-1.0 | 2.95 | 1.93 | 11.2 | 11.2 | 9.02 | 2.66 | 0.00 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| dev-peer0.org2.example.com-mycc-1.0 | 0.00 | 0.00 | 10.1 | 10.1 | 0.000479 | 0.000542 | 0.00 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| cli | 0.00 | 0.00 | 7.35 | 7.35 | 0.00 | 0.00 | 0.00 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer0.org2.example.com | 14.23 | 7.93 | 342 | 315 | 34.2 | 0.484 | 102 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| orderer.example.com | 10.19 | 7.44 | 120 | 104 | 36.6 | 69.9 | 70.2 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer0.org1.example.com | 21.10 | 16.10 | 332 | 312 | 45.1 | 20.2 | 102 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer1.org2.example.com | 20.30 | 15.29 | 321 | 295 | 44.8 | 50.3 | 102 | 0.00 |
|-------------------------------------|-----------|-----------|------------------|------------------|-----------------|------------------|-----------------|---------------|
| peer1.org1.example.com | 12.76 | 9.02 | 334 | 311 | 34.4 | 37.9 | 102 | 0.00 |
+-------------------------------------+-----------+-----------+------------------+------------------+-----------------+------------------+-----------------+---------------+
吞吐量情况
+------------+-------+------+-----------------+-----------------+-----------------+-----------------+------------------+
| Name | Succ | Fail | Send Rate (TPS) | Max Latency (s) | Min Latency (s) | Avg Latency (s) | Throughput (TPS) |
|------------|-------|------|-----------------|-----------------|-----------------|-----------------|------------------|
| writeAsset | 11719 | 0 | 205.2 | 2.11 | 0.06 | 0.48 | 198.1 |
+------------+-------+------+-----------------+-----------------+-----------------+-----------------+------------------+