Centos7 安装Caliper 测试 Fabric2.3

文章目录

目录

文章目录

前言

一、安装npm

二、创建Fabric测试网

三、Caliper创建工作区

四、构建网络配置文件

五、构建测试工作负载模块

六、构建基准测试配置文件

七、运行Caliper基准测试

总结

参考博客


前言

Caliper 是一个区块链性能基准框架,它允许用户使用自定义用例测试不同的区块链解决方案,并获得一组性能测试结果。

本人记录自己使用Caliper来测试Fabric2.3的性能。

本机环境:Centos7.9,Fabric2.3.3


一、安装npm

我用的是nvm来安装npm,因为nvm是node.js不同版本的管理器,允许通过命令来快速安装和使用不同版本的node。

1.查看git是否安装

# 查看git版本
git --version
# 如果没有安装git 执行 yum install git

2.安装Node.js版本管理工具nvm

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

或者

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

运行上述任何一个命令都会下载一个脚本并且运行这个脚本,这个脚本是将nvm存储库克隆到~/.nvm,并且会将以下代码段中的源代码添加到配置文件(~/.bashrc)

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

可以查看~/.bashrc中写了什么

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

3.设置nvm自动运行

echo "source ~/.nvm/nvm.sh" >> ~/.bashrc
# 更新配置
source ~/.bashrc

 4.检验是否安装

command -v nvm

5.使用nvm

# 查看node.js版本
nvm ls-remote
# 安装最新node.js版本
nvm install node
# 切换版本
nvm use x.x
# 查看更多指令
nvm --help

6.查看node版本

node -v
npm -v

二、创建Fabric测试网

# 进入自己的本地的test-network
cd /opt/fabric/fabric-samples/test-network
# 启动网络、创建通道、安装链码
./network.sh up createChannel
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

三、Caliper创建工作区

fabric-samples同级的目录下创建一个名为caliper-workspace的文件夹,然后在caliper-workspace文件夹内创建三个文件夹,分别命名为networksbenchmarksworkload

# 进入自己的fabric-samples同级目录下
cd /opt/fabric
# 创建工作区
mkdir caliper-workspace
cd caliper-workspace
mkdir networks && mkdir benchmarks && mkdir workload
# 或者这个指令 mkdir -p caliper-workspace/{networks,benchmarks,workload}

Caliper的安装和使用是基于本地npm来安装。在caliper-workspace目录中,使用以下终端命令初始化项目,可以发现文件夹内多了一个package.json。

npm init -y

使用以下终端命令安装Caliper CLI,执行完后,会将依赖下载到当前目录下的node_modules文件下。

# 版本要匹配,0.4对应fabric2.x,0.3对应fabric1.4
npm install --only=prod @hyperledger/[email protected]

使用以下终端命令绑定 SDK:

npx caliper bind --caliper-bind-sut fabric:2.2

四、构建网络配置文件

网络配置文件是 Caliper 使用者可以在 Hyperledger Fabric 网络上提交和评估交易所需的文件。该文件可以是 YAML 或 JSON 格式,这里显示的是 YAML 格式。

networks文件夹下创建一个名为networkConfig.yaml的模板文件

cd networks
touch networkConfig.yaml

其模板内容如下:

name: Caliper test
version: "2.0.0"

caliper:
  blockchain: fabric

channels:

organizations:

name:配置的名称,本文中为“Caliper test”

version:正在使用的配置文件的版本,“2.0.0”确保使用新的结构连接器

caliper:向 Caliper 指示目标 SUT,以fabric网络为目标。

channels:描述 Hyperledger Fabric 通道和部署在这些通道上的智能合约以进行基准测试。需要提供通道名称和智能合约名称,因为fabric可以存在多个通道。

organizations:Hyperledger Fabric 组织的列表,其中包含与每个组织关联的身份和连接配置文件。在这里,我们需要添加mspid的信息如Org1MSP,我们还需要提供一个名称、关联的连接配置文件和至少1个身份,并且仔细查看并确保证书、私钥和连接配置文件路径是否正确。

填充模板,完整内容如下:

name: Calier test
version: "2.0.0"

caliper:
  blockchain: fabric

channels:
  - channelName: mychannel
    contracts:
    - id: basic

organizations:
  - mspid: Org1MSP
    identities:
      certificates:
      - name: '[email protected]'
        clientPrivateKey:
          path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk'
        clientSignedCert:
          path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]'
    connectionProfile:
      path: '/opt/fabric/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml'
      discover: true

五、构建测试工作负载模块

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);
    }
    
    async submitTransaction() {
        // NOOP
    }
    
    async cleanupWorkloadModule() {
        // NOOP
    }
}

function createWorkloadModule() {
    return new MyWorkload();
}

module.exports.createWorkloadModule = createWorkloadModule;

工作负载模块在基准测试期间与部署的智能合约进行交互。主要有三个内容:

  ① initializeWorkloadModule - 用于初始化基本测试所需要的任何项目

  ② submitTransaction - 用于在基准的监控阶段与智能合约进行交互

  ③ createWorkloadModule - 用于在基准测试完成后进行清理

完整内容如下:

'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: '[email protected]',
                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: '[email protected]',
            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: '[email protected]',
                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: 60
        rateControl: 
          type: fixed-load
          opts:
            transactionLoad: 50
            startingTps: 100
        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.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test --caliper-fabric-gateway-enabled

总结

Caliper还有很多详细内容我没有叙述,可以查看官方文档。

 官方配置详解:https://hyperledger.github.io/caliper/vNext/fabric-tutorial/tutorials-fabric-existing/


参考博客

CentOS7下安装nvm_李胜步的博客-CSDN博客_centos7 安装nvm

Fabric 2.2.0上搭建Hyperledger caliper进行性能测试_Famidlistimo的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/humingwei11/article/details/122554948
今日推荐