Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试

文章目录

  • 什么是Hyperledger Caliper
    • Caliper目前支持以下区块链平台
    • Caliper目前支持的性能指标包括
    • Hyperledger Caliper系统架构总览
    • Caliper目前提供NPM和DOcker两种官方发布途径
    • 基准测试配置文件时运行一个Caliper基准测试必须的配置文件
  • 首先安转 Node 和 Npm
  • 部署 fabric 网络
    • 对fabric2.4.4进行测试
    • 开启网络创建通道并启动couchdb
    • 部署链码
  • 安装Hyperledger Caliper 准备工作
  • 修改如下三个配置文件
    • networkConfig.yaml
    • readAsset.js
    • myAssetBenchmark.yaml
  • 正式安装Hyperledger Caliper
    • 开启测试
    • 启动测试报错
    • 解决报错

什么是Hyperledger Caliper

Hyperledger Caliper是一个通用的区块链性能测试框架,它允许用户使用自定义的用例测试不同的区块链解决方案,并得到一组

性能测试结果。

Caliper目前支持以下区块链平台

Hyperledger Besu
Hyperledger Burrow
Ethereum
Hyperledger Fabric
FISCO BCOS
Hyperledger Iroha
Hyperledger Sawtooth

Caliper目前支持的性能指标包括

交易/读吞吐量
交易/读延迟:最小、最大、平均、百分比
资源消耗:CPU、内存、网络IO…
Hyperledger Caliper系统架构

Caliper是一个可以对不同区块链平台进行基准测试的通用框架
Caliper 设计时考虑了伸缩性和可扩展性,因此很容易和主流的运维监控系统集成。

Hyperledger Caliper系统架构总览

Caliper的多区块链平台支持能力
Caliper的主进程与工作进程
Caliper的分布式处理能力
安装Hyperledger Caliper

Caliper目前提供NPM和DOcker两种官方发布途径

你也可以直接克隆官方代码然后从源码进行安装:

用NPM安装Caliper
用Docker安装Caliper
从源代码安装Caliper
使用Caliper命令行工具

基准测试配置文件时运行一个Caliper基准测试必须的配置文件

Caliper基准测试配置
配置Caliper观察者
配置Caliper监视指标
Caliper基准测试配置示例
编写Caliper工作负载模块

Caliper 官方仓库地址

Caliper GitHub下载比较慢 可以使用我的Gitee仓库地址下载

git clone https://gitee.com/successhp/caliper-benchmarks

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第1张图片

首先安转 Node 和 Npm

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]

在这里插入图片描述

部署 fabric 网络

对fabric2.4.4进行测试

cd ../fabric-samples/test-network

开启网络创建通道并启动couchdb

./network.sh up createChannel -ca -s couchdb

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第2张图片

部署链码

不知道如何部署链码,可以参考学习我这篇文章

Fabric 超级账本学习【2】Fabric2.4网络环境下部署自己编写的go语言链码并实例化测试(手把手教学,步骤超详细)

先部署我在 Fabric 超级账本学习【2】 中编写的链码,使用资产链码进行测试

./network.sh deployCC -ccn asseth -ccp ./mychaincode/asset -ccl go

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第3张图片

安装Hyperledger Caliper 准备工作

来到fabric-sample 同级目录中,新建如下文件和文件夹

mkdir caliper-workspace
cd caliper-workspace
mkdir networks benchmarks workload
cd networks
touch networkConfig.yaml
cd workload
touch readAsset.js

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第4张图片

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第5张图片

修改如下三个配置文件

networkConfig.yaml

证书文件路径一定要修改成自己的!!!!不然后面启动会报错

# 该文件本质上是一个网络连接配置,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

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{
            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;

myAssetBenchmark.yaml

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  # 链码名

正式安装Hyperledger Caliper

然后进入 caliper-benchmarks文件夹

cd caliper-benchmarks

执行命令,安装最新版的hyperledger caliper

npm install --only=prod @hyperledger/caliper-cli

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第6张图片
绑定我们的hyperledger fabric2.4.4注意要对应自己的fabric版本

npx caliper bind --caliper-bind-sut fabric:2.4

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第7张图片

查看版本

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  [字符串]

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第8张图片

解决报错

报错原因证书路径错误

正确的 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

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第9张图片
Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第10张图片
完成后会在caliper-benchmarks文件夹中生成一个 report.html 文件
Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第11张图片
Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第12张图片

Fabric 超级账本学习【7】安装 Hyperledger Caliper0.5.0 对 fabric2.4.4 网络进行压力测试_第13张图片

你可能感兴趣的:(超级账本,区块链,fabric,fabric,学习,网络,区块链,压力测试)