Build Hyperledger caliper on Fabric for performance testing

Introduction to Fabric ( recommended articles )

Hyperledger (super ledger) is a project of the Linux Foundation. Fabric is the earliest and most widely used blockchain project in the Hyperledger project. It was originally developed by IBM and later donated to the foundation.

  • Is an open source enterprise-level permissioned distributed ledger technology platform
  • is a highly modular and configurable architecture (a,b,c)
  • Support for pluggable implementations of different components
  • Smart contracts support multiple languages: go, java, node.js, etc.

Introduction to Hyperledger Caliper ( official document )

Hyperledger Caliper is a general-purpose blockchain performance testing framework that allows users to test different blockchain solutions using custom use cases and obtain a set of performance test results.

Caliper currently supports the following blockchain platforms:

  • Hyperledger Besu
  • Hyperledger Burrow
  • Ethereum
  • Hyperledger Fabric
  • TAX BCOS
  • Hyperledger Iroha
  • Hyperledger Sawtooth

The performance indicators currently supported by Caliper include:

  • transaction/read throughput
  • Transaction/Read Latency: Min, Max, Average, Percentage
  • Resource consumption: CPU, memory, network IO...

install nodejs

This test requires the environment of docker, docker-compose, go, and nodejs. There is no installation introduction here, but a brief introduction to the installation of nodejs.

Here I have used the following versions of node, which one to use depends on the error message. But every time you install a new one, you need to rebuild and install the node-gyp build automation tool. Go to the bin directory of node to see if there is a node-gyp command.

insert image description here

wget https://nodejs.org/dist/v16.15.0/node-v16.15.0-linux-x64.tar.xz
tar -xvf node-v16.15.0-linux-x64.tar.xz 
sudo vim /etc/profile
export NODE_HOME=你自己的安装目录
export PATH=$NODE_HOME/bin:$PATH
source /etc/profile
node -v

#安装node-gyp构建自动化工具
sudo npm install -g node-gyp

Build Hyperledger caliper ( official website )

Before the test is to ensure that our blockchain project has been installed successfully, and the previous relevant steps have been completed.
You can also refer to the official website, which is less detailed.

Create and initialize the Fabric network

cd test-network
./network.sh up createChannel

Create a Caliper workspace

Create a caliper-workspace folder in the same directory as network.sh and create three subfolders

mkdir -p caliper-workspace/{
    
    networks,benchmarks,workload}

Initialize the workspace
Enter the caliper-workspace directory to execute the command

npm init -y

Install caliper-cli, which will download the dependencies used to the node_modules directory of the current workspace

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

Precautions:
For safety reasons in Linux, when the root user executes the npm command, it will be replaced by a nobody user, and this user basically has no permissions, so when executing this command, you can log in with other users yourself, and then Grant other users permission to operate our fabric workspace. ( reference article )

#下面这个命令使这个文件夹所有用户都可以操作
chmod -R 777 /home/workspace/

Binding terminal SDK

npx caliper bind --caliper-bind-sut fabric:2.1

build test file

Build the load module for the test job.
Create a createAsset.js file under the workload folder, create a test account command, randomly generate an account id and make an initialization call.

'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() {
    
    
            const randomId = Math.random();
            const assetID = `${
     
     this.roundArguments.prefix}_${
     
     randomId}`;
            console.log(`Creating asset ${
     
     assetID}`);
            const request = {
    
    
                contractId: this.roundArguments.contractId,
                contractFunction: 'CreateAsset',
                invokerIdentity: '[email protected]',
                contractArguments: [assetID,'blue','20','500','500'],
                readOnly: false
            };

            await this.sutAdapter.sendRequests(request);
    }
    
    async cleanupWorkloadModule() {
    
    
    }
}

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

module.exports.createWorkloadModule = createWorkloadModule;


Create a file named networkConfig.json under the networks folder.

Let me explain here that the path in the networkConfig.json file is the path corresponding to the files in your own project.
Other names correspond to the configuration in the host, and will not be modified if not modified.

{
    
    
    "version" : "1.0",
    "name": "Caliper test",
    "caliper" : {
    
    
        "blockchain": "fabric"
    },
    "clients": {
    
    
        "[email protected]": {
    
    
            "client": {
    
    
                "credentialStore": {
    
    
                    "path": "/tmp/org1",
                    "cryptoStore": {
    
    
                        "path": "/tmp/org1"
                    }
                },
                "organization": "Org1",
                "clientPrivateKey": {
    
    
                    "path": "/root/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/keystore/priv_sk"
                },
                "clientSignedCert": {
    
    
                    "path": "/root/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp/signcerts/[email protected]"
                },
                "connection": {
    
    
                    "timeout": {
    
    
                        "peer": {
    
    
                            "endorser": "3000"
                        }
                    }
                }

            }
        }
    },
    "channels": {
    
    
        "mychannel": {
    
    
            "created" : true,
	    "orderers":[
	       "orderer.example.com"
	    ],
	    "peers":{
    
    
                "peer0.org1.example.com": {
    
    }
	    },
            "contracts": [
                {
    
    
                    "id":"basic",
                    "version":"1.0"
                }
            ]
        }
    },
    "organizations":{
    
    
        "Org1": {
    
    
            "mspid": "Org1MSP",
            "peers": [
                "peer0.org1.example.com"
            ]
        }
    },
    "orderers":{
    
    
        "orderer.example.com": {
    
    
	   "url": "grpcs://orderer.example.com:7050",
	   "tlsCACerts": {
    
    
	       "path": "/root/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"

	   },
            "grpcOptions": {
    
    
                "ssl-target-name-override": "orderer.example.com"
            }
	}
    },

    "peers": {
    
    
        "peer0.org1.example.com": {
    
    
            "url": "grpcs://peer0.org1.example.com:7051",
            "tlsCACerts": {
    
    
		"path": "/root/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt"    
            },
            "grpcOptions": {
    
    
                "ssl-target-name-override": "peer0.org1.example.com",
                "hostnameOverride": "peer0.org1.example.com"
            }
        }
    }
}


Configuration files for benchmarked tests

Create a file called myAssetBenchmark.yaml under the benchmarks folder

The benchmark configuration file defines the benchmark round and references the defined workload modules. It will specify the number of test workers to use when generating the load, the number of test rounds, the duration of each round, the rate control applied to the transactional load during each round, and options related to the monitor.
In this file, we can also configure the total amount of sent transactions for testing, the tps sent and other information.

  • tps: transaction sending rate
  • txNumber: the total amount of transactions sent

insert image description here

test:
    #测试的名称
    name: basic-contract-benchmark
    #基本的描述信息
    description: test benchmark
    workers:
      type: local
      number: 5
    rounds:
      - label: createAsset-total20
        description: create asset benchmark
        #交易的发送总量
        txNumber: 20
        rateControl: 
          type: fixed-rate
          opts:
          	#交易发送的速率
            tps: 2
        workload:
        #这个配置我们刚才创建的js文件的路径
          module: workload/createAsset.js
          arguments:
            prefix: assetv1
            assets: 5
            contractId: basic
monitors:
  resource:
  - module: docker
    options:
      interval: 5 
      containers:
      - all


Configure the host file

vim /etc/hosts

#加入以下域名解析
127.0.0.1 peer0.org1.example.com
127.0.0.1 peer0.org2.example.com
127.0.0.1 peer1.org1.example.com
127.0.0.1 peer1.org2.example.com
127.0.0.1 orderer.example.com


Run caplier for performance testing

npx caliper launch manager --caliper-workspace ./ --caliper-networkconfig networks/networkConfig.json --caliper-benchconfig benchmarks/myAssetBenchmark.yaml --caliper-flow-only-test --caliper-fabric-gateway-enabled

After the operation is completed, a report.html file will be generated in the directory,
insert image description here
just click on the report file.

Guess you like

Origin blog.csdn.net/qq_45401910/article/details/126008153