Skip to content

K6 test improvement #11505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion tools/k6/src/rest/libex/parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,55 @@
}
);

const computeAccountWithCryptoAllowanceParameters = wrapComputeParametersFunc(
['DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE', 'DEFAULT_SPENDER_ID_CRYPTO_ALLOWANCE'],
() => {

Check notice on line 127 in tools/k6/src/rest/libex/parameters.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/k6/src/rest/libex/parameters.js#L127

Method (anonymous) has 38 lines of code (limit is 30)
let accountsPath = `${baseUrlPrefix}/accounts?account.id=gt:${__ENV.DEFAULT_START_ACCOUNT}&balance=false&order=asc&limit=100`;
const candidate = {cryptoAllowances: []};
let totalAccounts = 0;

while (accountsPath && totalAccounts < 100000) {
const {
accounts,
links: {next},
} = getValidResponse(accountsPath, null, http.get);
accountsPath = restUrlFromNext(next);

for (const {account} of accounts) {
const cryptoAllowancesPath = `${baseUrlPrefix}/accounts/${account}/allowances/crypto?limit=25`;
const cryptoAllowances = getEntities(cryptoAllowancesPath, allowanceListName);

if (cryptoAllowances.length > candidate.cryptoAllowances.length) {
candidate.account = account;
candidate.cryptoAllowances = cryptoAllowances;
}

if (cryptoAllowances.length >= 25) {
console.info(`Found account ${account} with ${cryptoAllowances.length} crypto allowances`);
return {
DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE: account,
DEFAULT_SPENDER_ID_CRYPTO_ALLOWANCE: cryptoAllowances[0].spender,
};
}
}

totalAccounts += accounts.length;
}

if (candidate.length > 0) {
console.warn(
`Fallback to account ${candidate.account} with ${candidate.cryptoAllowances.length} crypto allowances`
);
return {

Check notice on line 164 in tools/k6/src/rest/libex/parameters.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/k6/src/rest/libex/parameters.js#L164

Unnecessary block.
DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE: candidate.account,
DEFAULT_SPENDER_ID_CRYPTO_ALLOWANCE: candidate.cryptoAllowances[0].spender,
};
}

throw new Error('It was not possible to find an account with with significant number of crypto allowances.');

Check notice on line 170 in tools/k6/src/rest/libex/parameters.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/k6/src/rest/libex/parameters.js#L170

A 'return', 'break', 'continue', or 'throw' statement should be the last in a block.
}
);

const computeAccountWithNftsParameters = wrapComputeParametersFunc(['DEFAULT_ACCOUNT_ID_NFTS'], () => {
const candidate = {balance: 0};
let tokensPath = `${baseUrlPrefix}/tokens?type=NON_FUNGIBLE_UNIQUE&limit=100&order=asc`;
Expand Down Expand Up @@ -197,7 +246,7 @@
return {DEFAULT_ACCOUNT_ID_TOKEN_ALLOWANCE: candidate.account};
}

throw new Error('It was not possible to find an account with with significant number of allowance tokens.');
throw new Error('It was not possible to find an account with with significant number of token allowances.');
}
);

Expand Down Expand Up @@ -345,6 +394,7 @@

const allHandlers = [
computeAccountParameters,
computeAccountWithCryptoAllowanceParameters,
computeAccountWithNftsParameters,
computeAccountWithTokenAllowanceParameters,
computeAccountWithTokenParameters,
Expand Down
11 changes: 6 additions & 5 deletions tools/k6/src/rest/test/accountsCryptoAllowance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import http from 'k6/http';

import {isSuccess, RestTestScenarioBuilder} from '../libex/common.js';
import {resultListName} from '../libex/constants.js';
import {isValidListResponse, RestTestScenarioBuilder} from '../libex/common.js';
import {allowanceListName} from '../libex/constants.js';

const urlTag = '/accounts/{id}/allowances/crypto';

const getUrl = (testParameters) => `/accounts/${testParameters['DEFAULT_ACCOUNT_ID']}/allowances/crypto`;
const getUrl = (testParameters) =>
`/accounts/${testParameters['DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE']}/allowances/crypto`;

const {options, run, setup} = new RestTestScenarioBuilder()
.name('accountCryptoAllowancesResults') // use unique scenario name among all tests
Expand All @@ -16,8 +17,8 @@ const {options, run, setup} = new RestTestScenarioBuilder()
const url = `${testParameters['BASE_URL_PREFIX']}${getUrl(testParameters)}`;
return http.get(url);
})
.requiredParameters('DEFAULT_ACCOUNT_ID')
.check('Account crypto allowances results OK', (r) => isSuccess(r, resultListName))
.requiredParameters('DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE')
.check('Account crypto allowances results OK', (r) => isValidListResponse(r, allowanceListName))
.build();

export {getUrl, options, run, setup};
10 changes: 6 additions & 4 deletions tools/k6/src/rest/test/accountsCryptoAllowanceSpender.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import http from 'k6/http';

import {isSuccess, RestTestScenarioBuilder} from '../libex/common.js';
import {isValidListResponse, RestTestScenarioBuilder} from '../libex/common.js';
import {allowanceListName} from '../libex/constants.js';

const urlTag = '/accounts/{id}/allowances/crypto';

const getUrl = (testParameters) => `/accounts/${testParameters['DEFAULT_ACCOUNT_ID']}/allowances/crypto?spender.id=98`;
const getUrl = (testParameters) =>
`/accounts/${testParameters['DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE']}/allowances/crypto?spender.id=${testParameters['DEFAULT_SPENDER_ID_CRYPTO_ALLOWANCE']}`;

const {options, run, setup} = new RestTestScenarioBuilder()
.name('accountCryptoAllowancesResultsSpender') // use unique scenario name among all tests
Expand All @@ -15,8 +17,8 @@ const {options, run, setup} = new RestTestScenarioBuilder()
const url = `${testParameters['BASE_URL_PREFIX']}${getUrl(testParameters)}`;
return http.get(url);
})
.requiredParameters('DEFAULT_ACCOUNT_ID')
.check('Account crypto allowances for spender results OK', (r) => isSuccess(r))
.requiredParameters('DEFAULT_ACCOUNT_ID_CRYPTO_ALLOWANCE', 'DEFAULT_SPENDER_ID_CRYPTO_ALLOWANCE')
.check('Account crypto allowances for spender results OK', (r) => isValidListResponse(r, allowanceListName))
.build();

export {getUrl, options, run, setup};
21 changes: 19 additions & 2 deletions tools/k6/src/web3/test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
import {check, sleep} from 'k6';
import {vu} from 'k6/execution';
import http from 'k6/http';
import {SharedArray} from 'k6/data';

import * as utils from '../../lib/common.js';

const defaultVuData = {

Check warning on line 10 in tools/k6/src/web3/test/common.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/k6/src/web3/test/common.js#L10

Avoid trailing commas in object or array literals
block: '',
data: '',
to: '',
gas: 0,
from: '',
value: 0,
sleep: 0,
};
const resultField = 'result';

function isNonErrorResponse(response) {
Expand All @@ -29,6 +40,12 @@
},
});

const loadVuDataOrDefault = (filepath, key) =>
new SharedArray(key, () => {
const data = JSON.parse(open(filepath));
return key in data ? data[key] : [];
});

function ContractCallTestScenarioBuilder() {
this._args = null;
this._name = null;
Expand Down Expand Up @@ -66,7 +83,7 @@
} else {
const {_vuData: vuData} = that;
const data = vuData
? Object.assign({}, vuData[vu.idInTest % vuData.length])
? Object.assign({}, defaultVuData, vuData[vu.idInTest % vuData.length])
: {
block: that._block,
data: that._data,
Expand Down Expand Up @@ -173,4 +190,4 @@
return this;
}

export {isNonErrorResponse, jsonPost, ContractCallTestScenarioBuilder};
export {isNonErrorResponse, jsonPost, loadVuDataOrDefault, ContractCallTestScenarioBuilder};
15 changes: 6 additions & 9 deletions tools/k6/src/web3/test/commonContractCallEstimateTemplate.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// SPDX-License-Identifier: Apache-2.0

import {SharedArray} from 'k6/data';
import {ContractCallTestScenarioBuilder} from './common.js';
import {loadVuDataOrDefault, ContractCallTestScenarioBuilder} from './common.js';

function ContractCallEstimateTestTemplate(key) {
const data = new SharedArray(key, () => {
return JSON.parse(open('./resources/estimate.json'))[key];
});

const {options, run} = new ContractCallTestScenarioBuilder().name(key).estimate(true).vuData(data).build();

return {options, run};
return new ContractCallTestScenarioBuilder()
.name(key)
.estimate(true)
.vuData(loadVuDataOrDefault('./resources/estimate.json', key))
.build();
}

export {ContractCallEstimateTestTemplate};
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
// SPDX-License-Identifier: Apache-2.0

import {SharedArray} from 'k6/data';
import {ContractCallTestScenarioBuilder} from './common.js';
import {loadVuDataOrDefault, ContractCallTestScenarioBuilder} from './common.js';

function PrecompileModificationTestTemplate(key, shouldRevert) {
const data = new SharedArray(key, () => {
return JSON.parse(open('../resources/modificationFunctions.json'))[key];
});

const {options, run} = new ContractCallTestScenarioBuilder()
return new ContractCallTestScenarioBuilder()
.name(key)
.vuData(data)
.vuData(loadVuDataOrDefault('../resources/modificationFunctions.json', key))
.shouldRevert(shouldRevert)
.build();

return {options, run};
}

export {PrecompileModificationTestTemplate};
Loading
Loading