caliper测试性能(小例子)

这个是我之前一周做的一个实验,多谢余府同学的大力帮助~
我们接下来要在一个存在的区块链网络建立并且运行性能测试

概况
用caliper测试,要安装nodejs。本文先用官方文档里面的。这个网络包含两个组织一个soloorderer,运用了js的asset-transfer-basic智能合约。

下载镜像二进制我默认已经下载了包括配置环境变量

cd go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
//以默认方式建立通道开启网络
./network.sh up createChannel
//部署链码 名字叫basic 语言是js
./network.sh deployCC -ccn basic -ccl javascript

如果有大佬知道为什么我这边用javascript格式特别特别慢,就卡着,或者说超时,不吝赐教
在这里插入图片描述
在这里插入图片描述
用go就一点点事情都没有
在这里插入图片描述
在script文件夹下面建立caliper-workspace,并建立networks,benchmarks,workload文件夹
安装calipercli

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

绑定sdk

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

之前已经在caliper-worksapce做过这步的不用再做了
把测试文件填充一下
在案例中,用org来连接一下。
我用fabric-samples -> test-network -> organizations -> peerOrganizations -> org1.example.com中间的connection-org1.json ccp(common connection profile)

组织和节点:
1.把organizations和peers的内容复制进入caliper网络配置文件
2.把certificateAuthorities 列给删掉,因为我们不用ca来登记注册,还有一点是和caliper有冲突(我也不知道他说的冲突指什么)
接下新建一个networkConfig.json文件到networks
复制一下网络配置文件:

{
    "version" : "1.0",
    "name": "Caliper test",
    "caliper" : {
        "blockchain": "fabric"
    },
    "clients": {
        "Admin@org1.example.com": {
            "client": {
                "credentialStore": {
                    "path": "/tmp/org1",
                    "cryptoStore": {
                        "path": "/tmp/org1"
                    }
                },
                "organization": "Org1",
                "clientPrivateKey": {
                    "path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
                },
                "clientSignedCert": {
                    "path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
                },
                "connection": {
                    "timeout": {
                        "peer": {
                            "endorser": "300"
                        }
                    }
                }

            }
        }
    },
    "channels": {
        "mychannel": {
            "created" : true,
            "contracts": [
                {
                    "id":"basic",
                    "version":"1.0.0"
                }
            ]
        }
    },
    "organizations":{
        "Org1": {
            "mspid": "Org1MSP",
            "peers": [
                "peer0.org1.example.com"
            ]
        }
    },
    "peers": {
        "peer0.org1.example.com": {
            "url": "grpcs://localhost:7051",
            "tlsCACerts": {
                "pem": "-----BEGIN CERTIFICATE-----\n<UNIQUE CONTENT>\n-----END CERTIFICATE-----\n"
            },
            "grpcOptions": {
                "ssl-target-name-override": "peer0.org1.example.com",
                "hostnameOverride": "peer0.org1.example.com"
            }
        }
    }
}

把这段替换"pem": "-----BEGIN CERTIFICATE-----\n<UNIQUE CONTENT>\n-----END CERTIFICATE-----\n"
在这里插入图片描述
文件路径就是testnetwork下面organization。。。。
**注意,每次开启关闭网络都要把这段重新复制

分解一下代码:
client:在运行benchmark的时候表述caliper用到的身份。身份必须被fabric网络知道并且有着对应的密码学材料。这边我们是用单个身份Admin@org1.example.com,这里我们从ccp嵌套个client来表示身份代表的组织,提供基础连接超时信息
在client下面增加个credentialStore,再在下面增加一个path属性,该属性具有一个字符串变量,该字符串变量指向工作空间中的temp文件,其名称为/tmp/org1。另外,在credentialStore属性下添加一个名为cryptoStore的属性,并在该属性下添加另一个路径属性,该属性指向上面相同的temp文件/tmp/org1

在client对象下添加一个名为clientPrivateKey的属性。在此属性下添加一个名为path的属性,该属性具有一个字符串变量,可以指向标识的私钥。请注意,所提供的路径是相对于工作区的。此示范中私钥在 fabric-samples -> test-network -> organizations -> peerOrganizations -> org1.example.com -> users -> Admin@org1.example.com -> msp -> keystore -> priv_sk .
然后再client下面再搞一个clientSignedCert属性,再在下面添加一个名为path的属性。该属性具有一个字符串变量,指向身份的签名证书。
clients整体:

"clients": {
        "Admin@org1.example.com": {
            "client": {
                "organization": "Org1",
                "connection": {
                    "timeout": {
                        "peer": {
                            "endorser": "300"
                        }
                    }
                },
                "credentialStore": {
                    "path": "tmp/hfc-kvs/org1",
                    "cryptoStore": {
                        "path": "tmp/hfc-kvs/org1"
                    }
                },
                "clientPrivateKey": {
                    "path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/keystore/priv_sk"
                },
                "clientSignedCert": {
                    "path": "../fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/signcerts/Admin@org1.example.com-cert.pem"
                }
            }
        }
    }

channel object
将由test-network创建的已知通道mychannel添加到Caliper网络配置文件的通道部分中,并将创建的布尔值为true的属性赋给它。我们必须将此通道上可用的智能契约作为一个名为“契约”的数组中的对象列出。每个智能契约对象都有两个属性:id和version。id指定合同id;在这个例子,是basic。版本为具体合约版本;在本例中是1.0.0。

第三步,构建一个测试工作负载的模块
这个模块与已经部署的只能合约交互(在测试轮)。负载模块从caliper-core扩展了WorkloadModuleBase
workload文件夹新建一个readAsset.js文件,这个模块提供三种覆盖:
initializeWorkloadModule:用来为测试初始化任何所需的项目(具体:在submitTransaction阶段创造可能被查询的资产)
submitTransaction:与合约交互(查询在initializeWorkloadModule步骤创建的资产)
cleanupWorkloadModule:在完成测试后清除

填充文件
1.initializeWorkloadModule
懒得打中文了
The number of assets to be created will be given as roundArguments.assets. We create assets using the smart contract by populating an arguments object, which defines the transaction body, and using the Caliper API sendRequests, which requires knowledge of:

contractId, the name of smart contract that is to be used and is present within the Caliper network configuration file
contractFunction, the specific function within the smart contract to invoke
contractArguments, the arguments to pass to the smart contract function
invokerIdentity, the identity to use that is present within the Caliper network configuration file
readOnly, if performing a query operation or not
2.Populate submitTransaction
此模式在测试阶段不断被运行,我们会评估readasset智能合约模式(通过查询在上一步产生的资产)
3.Populate cleanupWorkloadModule
这个函数用于在测试后进行清理,因为它通过使用智能合约函数DeleteAsset删除initializeWorkloadModule函数中创建的资产。。注意,可以重构initializeWorkloadModule和cleanupWorkloadModule,以利用一个通用方法来执行创建/删除操作

'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: 'Admin@org1.example.com',
                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: 'Admin@org1.example.com',
            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: 'Admin@org1.example.com',
                contractArguments: [assetID],
                readOnly: false
            };

            await this.sutAdapter.sendRequests(request);
        }
    }
}

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

module.exports.createWorkloadModule = createWorkloadModule;

第四步:建立一个测试配置文件
先贴代码:

test:
    name: basic-contract-benchmark
    description: test benchmark
    workers:
      type: local
      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

很简单不多说,这个表述了一轮测试,被labeled为readAsset,跑30s等等

第五步:跑程序
在caliper-benchmarks目录下

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

运行结束
显示
在这里插入图片描述
本篇结束

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值