difftreelog
fix remove unused fn in perf test
in: master
1 file changed
tests/src/performance.seq.test.tsdiffbeforeafterboth1// Copyright 2019-2023 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itSub, usingPlaygrounds} from './util';20import {ICrossAccountId, IProperty} from './util/playgrounds/types';21import {UniqueHelper} from './util/playgrounds/unique';2223describe('Performace tests', () => {24 let alice: IKeyringPair;25 const MAX_TOKENS_TO_MINT = 120;2627 before(async () => {28 await usingPlaygrounds(async (helper, privateKey) => {29 const donor = await privateKey({url: import.meta.url});30 [alice] = await helper.arrange.createAccounts([100_000n], donor);31 });32 });3334 itSub('NFT tokens minting', async ({helper}) => {35 const propertyKey = 'prop-a';36 const collection = await helper.nft.mintCollection(alice, {37 name: 'test properties',38 description: 'test properties collection',39 tokenPrefix: 'TPC',40 tokenPropertyPermissions: [41 {key: propertyKey, permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},42 ],43 });4445 const step = 1_000;46 const sizeOfKey = sizeOfEncodedStr(propertyKey);47 let currentSize = step;48 let startCount = 0;49 let minterFunc = tryMintUnsafeRPC;50 try {51 startCount = await tryMintUnsafeRPC(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address});52 }53 catch (e) {54 startCount = await tryMintExplicit(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address});55 minterFunc = tryMintExplicit;56 }5758 expect(startCount).to.be.equal(MAX_TOKENS_TO_MINT);5960 while(currentSize <= 32_000) {61 const property = {key: propertyKey, value: 'A'.repeat(currentSize - sizeOfKey - sizeOfInt(currentSize))};62 const tokens = await minterFunc(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address}, property);63 expect(tokens).to.be.equal(MAX_TOKENS_TO_MINT);6465 currentSize += step;66 await helper.wait.newBlocks(2);67 }68 });69});707172const dryRun = async (api: ApiPromise, signer: IKeyringPair, tx: any) => {73 const signed = await tx.signAsync(signer);74 const dryRun = await api.rpc.system.dryRun(signed.toHex());75 return dryRun.isOk && dryRun.asOk.isOk;76};7778const getTokens = (tokensCount: number, owner: ICrossAccountId, property?: IProperty) => (new Array(tokensCount)).fill(0).map(() => {79 const token = {owner} as {owner: ICrossAccountId, properties?: IProperty[]};80 if(property) token.properties = [property];81 return token;82});8384const tryMintUnsafeRPC = async (helper: UniqueHelper, signer: IKeyringPair, tokensCount: number, collectionId: number, owner: ICrossAccountId, property?: IProperty): Promise<number> => {85 if(tokensCount < 10) console.log('try mint', tokensCount, 'tokens');86 const tokens = getTokens(tokensCount, owner, property);87 const tx = helper.constructApiCall('api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);88 if(!(await dryRun(helper.getApi(), signer, tx))) {89 if(tokensCount < 2) return 0;90 return await tryMintUnsafeRPC(helper, signer, tokensCount - 1, collectionId, owner, property);91 }92 await helper.executeExtrinsic(signer, 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);93 return tokensCount;94};9596const tryMintExplicit = async (helper: UniqueHelper, signer: IKeyringPair, tokensCount: number, collectionId: number, owner: ICrossAccountId, property?: IProperty): Promise<number> => {97 const tokens = getTokens(tokensCount, owner, property);98 try {99 await helper.executeExtrinsic(signer, 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);100 }101 catch (e) {102 if(tokensCount < 2) return 0;103 return await tryMintExplicit(helper, signer, tokensCount - 1, collectionId, owner, property);104 }105 return tokensCount;106};107108function sizeOfProperty(prop: IProperty) {109 return sizeOfEncodedStr(prop.key) + sizeOfEncodedStr(prop.value!);110}111112function sizeOfInt(i: number) {113 if(i < 0 || i > 0xffffffff) throw new Error('out of range');114 if(i < 0b11_1111) {115 return 1;116 } else if(i < 0b11_1111_1111_1111) {117 return 2;118 } else if(i < 0b11_1111_1111_1111_1111_1111_1111_1111) {119 return 4;120 } else {121 return 5;122 }123}124125const UTF8_ENCODER = new TextEncoder();126function sizeOfEncodedStr(v: string) {127 const encoded = UTF8_ENCODER.encode(v);128 return sizeOfInt(encoded.length) + encoded.length;129}1// Copyright 2019-2023 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {ApiPromise} from '@polkadot/api';18import {IKeyringPair} from '@polkadot/types/types';19import {expect, itSub, usingPlaygrounds} from './util';20import {ICrossAccountId, IProperty} from './util/playgrounds/types';21import {UniqueHelper} from './util/playgrounds/unique';2223describe('Performace tests', () => {24 let alice: IKeyringPair;25 const MAX_TOKENS_TO_MINT = 120;2627 before(async () => {28 await usingPlaygrounds(async (helper, privateKey) => {29 const donor = await privateKey({url: import.meta.url});30 [alice] = await helper.arrange.createAccounts([100_000n], donor);31 });32 });3334 itSub('NFT tokens minting', async ({helper}) => {35 const propertyKey = 'prop-a';36 const collection = await helper.nft.mintCollection(alice, {37 name: 'test properties',38 description: 'test properties collection',39 tokenPrefix: 'TPC',40 tokenPropertyPermissions: [41 {key: propertyKey, permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},42 ],43 });4445 const step = 1_000;46 const sizeOfKey = sizeOfEncodedStr(propertyKey);47 let currentSize = step;48 let startCount = 0;49 let minterFunc = tryMintUnsafeRPC;50 try {51 startCount = await tryMintUnsafeRPC(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address});52 }53 catch (e) {54 startCount = await tryMintExplicit(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address});55 minterFunc = tryMintExplicit;56 }5758 expect(startCount).to.be.equal(MAX_TOKENS_TO_MINT);5960 while(currentSize <= 32_000) {61 const property = {key: propertyKey, value: 'A'.repeat(currentSize - sizeOfKey - sizeOfInt(currentSize))};62 const tokens = await minterFunc(helper, alice, MAX_TOKENS_TO_MINT, collection.collectionId, {Substrate: alice.address}, property);63 expect(tokens).to.be.equal(MAX_TOKENS_TO_MINT);6465 currentSize += step;66 await helper.wait.newBlocks(2);67 }68 });69});707172const dryRun = async (api: ApiPromise, signer: IKeyringPair, tx: any) => {73 const signed = await tx.signAsync(signer);74 const dryRun = await api.rpc.system.dryRun(signed.toHex());75 return dryRun.isOk && dryRun.asOk.isOk;76};7778const getTokens = (tokensCount: number, owner: ICrossAccountId, property?: IProperty) => (new Array(tokensCount)).fill(0).map(() => {79 const token = {owner} as {owner: ICrossAccountId, properties?: IProperty[]};80 if(property) token.properties = [property];81 return token;82});8384const tryMintUnsafeRPC = async (helper: UniqueHelper, signer: IKeyringPair, tokensCount: number, collectionId: number, owner: ICrossAccountId, property?: IProperty): Promise<number> => {85 if(tokensCount < 10) console.log('try mint', tokensCount, 'tokens');86 const tokens = getTokens(tokensCount, owner, property);87 const tx = helper.constructApiCall('api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);88 if(!(await dryRun(helper.getApi(), signer, tx))) {89 if(tokensCount < 2) return 0;90 return await tryMintUnsafeRPC(helper, signer, tokensCount - 1, collectionId, owner, property);91 }92 await helper.executeExtrinsic(signer, 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);93 return tokensCount;94};9596const tryMintExplicit = async (helper: UniqueHelper, signer: IKeyringPair, tokensCount: number, collectionId: number, owner: ICrossAccountId, property?: IProperty): Promise<number> => {97 const tokens = getTokens(tokensCount, owner, property);98 try {99 await helper.executeExtrinsic(signer, 'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}]);100 }101 catch (e) {102 if(tokensCount < 2) return 0;103 return await tryMintExplicit(helper, signer, tokensCount - 1, collectionId, owner, property);104 }105 return tokensCount;106};107108function sizeOfInt(i: number) {109 if(i < 0 || i > 0xffffffff) throw new Error('out of range');110 if(i < 0b11_1111) {111 return 1;112 } else if(i < 0b11_1111_1111_1111) {113 return 2;114 } else if(i < 0b11_1111_1111_1111_1111_1111_1111_1111) {115 return 4;116 } else {117 return 5;118 }119}120121const UTF8_ENCODER = new TextEncoder();122function sizeOfEncodedStr(v: string) {123 const encoded = UTF8_ENCODER.encode(v);124 return sizeOfInt(encoded.length) + encoded.length;125}