使用Caliper对Fabric地basic链码进行性能测试

如果你需要对fabric网络中地合约进行吞吐量、延迟等性能进行评估,可以使用Caliper来实现,会返回给你一份网页版的直观测试报告。下面是对test-network网络地basic链码地测试过程。

目录

    • [1. 建立caliper-workspace文件夹](#1. 建立caliper-workspace文件夹)
    • [2. 安装npm等](#2. 安装npm等)
    • [3. calipe安装](#3. calipe安装)
    • [4. 创建networks目录并编辑yaml文件](#4. 创建networks目录并编辑yaml文件)
    • [5. 创建workload目录编写js文件](#5. 创建workload目录编写js文件)
    • [6. 创建benchmarks目录并编写yaml文件](#6. 创建benchmarks目录并编写yaml文件)
    • [7. 启动测试](#7. 启动测试)
    • [8. 查看结果](#8. 查看结果)

1. 建立caliper-workspace文件夹

建立caliper-workspace文件夹,文件夹的建立路径是相对地,我这里是在fabric-samples的同级目录下创建的,创建的路径不同后面的配置文件中关于私钥证书等地路径也是不同的。

2. 安装npm等

  • 进入caliper文件夹 ,安装npm
bash 复制代码
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  • 安装Node.js和npm
bash 复制代码
sudo apt-get install -y nodejs
  • 验证是否安装成功

3. calipe安装

  • 使用npm安装特定版本的@hyperledger/caliper包
bash 复制代码
npm install --only=prod @hyperledger/caliper@0.5.0
  • 使用 Caliper 工具绑定到 Hyperledger Fabric 网络
bash 复制代码
npx caliper bind --caliper-bind-sut fabric:2.2


注意后面所创建的目录结构如下:

4. 创建networks目录并编辑yaml文件

bash 复制代码
mkdir networks
cd networks/
vim networkConfig.yaml

写入:

bash 复制代码
name: Caliper test
version: "2.0.0"

caliper:
  blockchain: fabric

channels:
  # channelName of mychannel matches the name of the channel created by test network
  - channelName: mychannel
    # the chaincodeIDs of all the fabric chaincodes in caliper-benchmarks
    contracts:
    - id: basic

organizations:
  - mspid: Org1MSP
    # Identities come from cryptogen created material for test-network
    identities:
      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文件

bash 复制代码
cd ..
mkdir workload
cd workload
vim readAsset.js

写入:

bash 复制代码
'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文件

bash 复制代码
cd ..
mkdir benchmarks
cd benchmarks/
vim myAssetBenchmark.yaml

写入:

bash 复制代码
test:
    name: basic-contract-benchmark
    description: test benchmark
    workers:
      number: 2
    rounds:
      - label: readAsset
        description: Read asset benchmark
        txDuration: 30
        rateControl:
          type: fixed-load
          opts:
            transactionLoad: 2
        workload:
          module: workload/readAsset.js
          arguments:
            assets: 10
            contractId: basic

7. 启动测试

cd ...

bash 复制代码
npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.yaml --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test

8. 查看结果

然后会在workspace目录下产生一个报告:

打开后就是测试的tps、时延等信息:

相关推荐
乌托邦的逃亡者1 小时前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ldj20201 小时前
Jenkins 流水线配置
运维·jenkins
古希腊数通小白(ip在学)4 小时前
stp拓扑变化分类
运维·服务器·网络·智能路由器
12点一刻6 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务6 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
o不ok!7 小时前
Linux面试问题-软件测试
linux·运维·服务器
宇钶宇夕10 小时前
SIMATIC S7-1200的以太网通信能力:协议与资源详细解析
运维·服务器·数据库·程序人生·自动化
杰夫贾维斯10 小时前
CentOS Linux 8 的系统部署 Qwen2.5-7B -Instruct-AWQ
linux·运维·人工智能·机器学习·centos
CodeWithMe11 小时前
【Note】Linux Kernel 实时技术深入:详解 PREEMPT_RT 与 Xenomai
linux·运维·服务器