如果你需要對fabric網絡中地合約進行吞吐量、延遲等性能進行評估
,可以使用Caliper來實現,會返回給你一份網頁版的直觀測試報告。下面是對test-network網絡地basic鏈碼地測試過程。
目錄
- 1. 建立caliper-workspace文件夾
- 2. 安裝npm等
- 3. calipe安裝
- 4. 創建networks目錄并編輯yaml文件
- 5. 創建workload目錄編寫js文件
- 6. 創建benchmarks目錄并編寫yaml文件
- 7. 啟動測試
- 8. 查看結果
1. 建立caliper-workspace文件夾
建立caliper-workspace文件夾,文件夾的建立路徑是相對地,我這里是在fabric-samples的同級目錄下創建的,創建的路徑不同后面的配置文件中關于私鑰證書等地路徑也是不同的。
2. 安裝npm等
- 進入caliper文件夾 ,安裝npm
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
- 安裝Node.js和npm
sudo apt-get install -y nodejs
- 驗證是否安裝成功
3. calipe安裝
- 使用npm安裝特定版本的@hyperledger/caliper包
npm install --only=prod @hyperledger/caliper@0.5.0
- 使用 Caliper 工具綁定到 Hyperledger Fabric 網絡
npx caliper bind --caliper-bind-sut fabric:2.2
注意后面所創建的目錄結構如下:
4. 創建networks目錄并編輯yaml文件
mkdir networks
cd networks/
vim networkConfig.yaml
寫入:
name: Caliper test
version: "2.0.0"caliper:blockchain: fabricchannels:# channelName of mychannel matches the name of the channel created by test network- channelName: mychannel# the chaincodeIDs of all the fabric chaincodes in caliper-benchmarkscontracts:- id: basicorganizations:- mspid: Org1MSP# Identities come from cryptogen created material for test-networkidentities:certificates:- name: 'User1'clientPrivateKey:path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk'clientSignedCert:path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem'connectionProfile:path: '../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'discover: true
5. 創建workload目錄編寫js文件
cd ..
mkdir workload
cd workload
vim 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: '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<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: 'User1',contractArguments: [assetID],readOnly: false};await this.sutAdapter.sendRequests(request);}}
}function createWorkloadModule() {return new MyWorkload();
}module.exports.createWorkloadModule = createWorkloadModule;
6. 創建benchmarks目錄并編寫yaml文件
cd ..
mkdir benchmarks
cd benchmarks/
vim myAssetBenchmark.yaml
寫入:
test:name: basic-contract-benchmarkdescription: test benchmarkworkers:number: 2rounds:- label: readAssetdescription: Read asset benchmarktxDuration: 30rateControl:type: fixed-loadopts:transactionLoad: 2workload:module: workload/readAsset.jsarguments:assets: 10contractId: basic
7. 啟動測試
cd …
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test
8. 查看結果
然后會在workspace目錄下產生一個報告:
打開后就是測試的tps、時延等信息: