mirror of
https://github.com/logos-storage/codex-factory.git
synced 2026-05-28 19:59:27 +00:00
feat: traffic gen (#21)
* feat: generate random bytes and push to the binded nodes * fix: npm reinstall * fix: version dumps in package.json * ci: bump version number of node * feat: payment-tolerance * refactor: gen 400 chunks with the trafficgen command * feat: parametric traffic generation * docs: amend beeShPorts description * docs: readme * fix: payment tolerance default value in bee.sh * refactor: only beeApiUrl argument on script call * docs: traffic gen readme adjustment * refactor: instead of payment tolerance use payment threshold * refactor: sleep ms raise and take out unnecessary axios call * chore: await for upload, the script no longer overloads bee nodes with data (#27) Co-authored-by: Vojtech Simetka <vojtech@simetka.cz>
This commit is contained in:
parent
05dc7b511b
commit
e61f2cb8a1
2
.github/workflows/publish.yaml
vendored
2
.github/workflows/publish.yaml
vendored
@ -29,7 +29,7 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v1
|
||||||
with:
|
with:
|
||||||
node-version: 12
|
node-version: 15
|
||||||
registry-url: 'https://registry.npmjs.org'
|
registry-url: 'https://registry.npmjs.org'
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
|||||||
26
README.md
26
README.md
@ -78,3 +78,29 @@ start the Bee cluster
|
|||||||
```sh
|
```sh
|
||||||
./scripts/environment.sh start
|
./scripts/environment.sh start
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Utilities
|
||||||
|
|
||||||
|
It is possible to generate random traffic in your cluster:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ npm run gen:traffic
|
||||||
|
```
|
||||||
|
|
||||||
|
The script is in an infinite loop, so if you want to stop the generation you have to terminate it manually in your terminal by pressing `Ctrl^C`.
|
||||||
|
|
||||||
|
If you don't specify any parameters it will produce 400 chunks/0.5 sec that the script tries to upload on the `http://localhost:1633` - that is the binded port of the queen node if you orchestrated the environment with the `envrionment.sh`.
|
||||||
|
|
||||||
|
The following way you can pass parameter
|
||||||
|
|
||||||
|
1. BEE_API_URL - Host that has reachable port to the Bee API. [Array<string>,Default:['http://localhost:1633']]
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ npm run gen:traffic -- <BEE_API_URL> <BEE_API_URL> <BEE_API_URL> (...)
|
||||||
|
```
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ npm run gen:traffic -- http://localhost:1633 http://localhost:11633
|
||||||
|
```
|
||||||
|
|||||||
23570
package-lock.json
generated
23570
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -8,11 +8,14 @@
|
|||||||
"supply": "truffle exec src/supply.js",
|
"supply": "truffle exec src/supply.js",
|
||||||
"build:env": "./scripts/build-environment.sh",
|
"build:env": "./scripts/build-environment.sh",
|
||||||
"publish:env": "./scripts/publish-environment.sh",
|
"publish:env": "./scripts/publish-environment.sh",
|
||||||
"run:env": "./scripts/environment.sh"
|
"run:env": "./scripts/environment.sh",
|
||||||
|
"gen:traffic": "node ./scripts/gen-traffic.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@ethersphere/bee-js": "^0.8.1",
|
||||||
"@openzeppelin/contracts": "^3.1.0",
|
"@openzeppelin/contracts": "^3.1.0",
|
||||||
"truffle": "^5.3.1"
|
"axios": "^0.20.0",
|
||||||
|
"truffle": "^5.3.5"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"swarm",
|
"swarm",
|
||||||
|
|||||||
@ -21,6 +21,7 @@ PARAMETERS:
|
|||||||
--own-image If passed, the used Docker image names will be identical as the name of the workers.
|
--own-image If passed, the used Docker image names will be identical as the name of the workers.
|
||||||
--version=x.y.z used version of Bee client.
|
--version=x.y.z used version of Bee client.
|
||||||
--detach It will not log the output of Queen node at the end of the process.
|
--detach It will not log the output of Queen node at the end of the process.
|
||||||
|
--payment-threshold The threshold in BZZ where you expect to get paid from your peers. (without decimals, default: 10000000000000)
|
||||||
USAGE
|
USAGE
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
@ -106,6 +107,7 @@ PORT_MAPS=2
|
|||||||
SWAP=true
|
SWAP=true
|
||||||
SWAP_FACTORY_ADDRESS="0x5b1869D9A4C187F2EAa108f3062412ecf0526b24"
|
SWAP_FACTORY_ADDRESS="0x5b1869D9A4C187F2EAa108f3062412ecf0526b24"
|
||||||
INIT_ROOT_DATA_DIR="$MY_PATH/bee-data-dirs"
|
INIT_ROOT_DATA_DIR="$MY_PATH/bee-data-dirs"
|
||||||
|
PAYMENT_THRESHOLD="10000000000000"
|
||||||
|
|
||||||
# Decide script action
|
# Decide script action
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@ -145,6 +147,10 @@ do
|
|||||||
PORT_MAPS="${1#*=}"
|
PORT_MAPS="${1#*=}"
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
|
--payment-treshold=*)
|
||||||
|
PAYMENT_THRESHOLD="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
--own-image)
|
--own-image)
|
||||||
OWN_IMAGE=true
|
OWN_IMAGE=true
|
||||||
shift 1
|
shift 1
|
||||||
@ -197,6 +203,7 @@ if [ -z "$QUEEN_CONTAINER_IN_DOCKER" ] || $EPHEMERAL ; then
|
|||||||
--swap-enable=$SWAP \
|
--swap-enable=$SWAP \
|
||||||
--swap-endpoint="http://$SWARM_BLOCKCHAIN_NAME:9545" \
|
--swap-endpoint="http://$SWARM_BLOCKCHAIN_NAME:9545" \
|
||||||
--swap-factory-address=$SWAP_FACTORY_ADDRESS \
|
--swap-factory-address=$SWAP_FACTORY_ADDRESS \
|
||||||
|
--payment-threshold="$PAYMENT_THRESHOLD" \
|
||||||
--welcome-message="You have found the queen of the beehive..." \
|
--welcome-message="You have found the queen of the beehive..." \
|
||||||
--cors-allowed-origins="*"
|
--cors-allowed-origins="*"
|
||||||
else
|
else
|
||||||
@ -241,6 +248,7 @@ for i in $(seq 1 1 "$WORKERS"); do
|
|||||||
--swap-enable=$SWAP \
|
--swap-enable=$SWAP \
|
||||||
--swap-endpoint="http://$SWARM_BLOCKCHAIN_NAME:9545" \
|
--swap-endpoint="http://$SWARM_BLOCKCHAIN_NAME:9545" \
|
||||||
--swap-factory-address=$SWAP_FACTORY_ADDRESS \
|
--swap-factory-address=$SWAP_FACTORY_ADDRESS \
|
||||||
|
--payment-threshold="$PAYMENT_THRESHOLD" \
|
||||||
--welcome-message="I'm just Bee worker ${i} in the beehive." \
|
--welcome-message="I'm just Bee worker ${i} in the beehive." \
|
||||||
--cors-allowed-origins="*"
|
--cors-allowed-origins="*"
|
||||||
else
|
else
|
||||||
|
|||||||
@ -10,6 +10,7 @@ PARAMETERS:
|
|||||||
--ephemeral create ephemeral container for bee-client. Data won't be persisted.
|
--ephemeral create ephemeral container for bee-client. Data won't be persisted.
|
||||||
--workers=number all Bee nodes in the test environment. Default is 4.
|
--workers=number all Bee nodes in the test environment. Default is 4.
|
||||||
--detach It will not log the output of Queen node at the end of the process.
|
--detach It will not log the output of Queen node at the end of the process.
|
||||||
|
--payment-treshold excess debt above payment threshold in BZZ where you disconnect from your peer (without decimals, default: 2000000000)
|
||||||
USAGE
|
USAGE
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
@ -38,6 +39,7 @@ WORKERS=4
|
|||||||
LOG=true
|
LOG=true
|
||||||
SWARM_BLOCKCHAIN_NAME="$BEE_ENV_PREFIX-blockchain"
|
SWARM_BLOCKCHAIN_NAME="$BEE_ENV_PREFIX-blockchain"
|
||||||
SWARM_NETWORK="$BEE_ENV_PREFIX-network"
|
SWARM_NETWORK="$BEE_ENV_PREFIX-network"
|
||||||
|
PAYMENT_THRESHOLD="2000000000"
|
||||||
|
|
||||||
# Decide script action
|
# Decide script action
|
||||||
case "$1" in
|
case "$1" in
|
||||||
@ -66,6 +68,10 @@ do
|
|||||||
WORKERS=${1#*=}
|
WORKERS=${1#*=}
|
||||||
shift 1
|
shift 1
|
||||||
;;
|
;;
|
||||||
|
--payment-treshold=*)
|
||||||
|
PAYMENT_THRESHOLD=${1#*=}
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
--detach)
|
--detach)
|
||||||
LOG=false
|
LOG=false
|
||||||
shift 1
|
shift 1
|
||||||
@ -100,7 +106,7 @@ fi
|
|||||||
sleep 5
|
sleep 5
|
||||||
|
|
||||||
# Build up bee.sh parameters
|
# Build up bee.sh parameters
|
||||||
BEE_SH_ARGUMENTS="--workers=$WORKERS --own-image"
|
BEE_SH_ARGUMENTS="--workers=$WORKERS --own-image --payment-treshold=$PAYMENT_THRESHOLD"
|
||||||
if $EPHEMERAL ; then
|
if $EPHEMERAL ; then
|
||||||
BEE_SH_ARGUMENTS="$BEE_SH_ARGUMENTS --ephemeral"
|
BEE_SH_ARGUMENTS="$BEE_SH_ARGUMENTS --ephemeral"
|
||||||
fi
|
fi
|
||||||
|
|||||||
69
scripts/gen-traffic.js
Normal file
69
scripts/gen-traffic.js
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
const axios = require('axios').default;
|
||||||
|
const Bee = require('@ethersphere/bee-js').Bee;
|
||||||
|
|
||||||
|
const SLEEP_BETWEEN_UPLOADS_MS = 1000
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lehmer random number generator with seed (minstd_rand in C++11)
|
||||||
|
* !!! Very fast but not well distributed pseudo-random function !!!
|
||||||
|
*
|
||||||
|
* @param seed Seed for the pseudo-random generator
|
||||||
|
*/
|
||||||
|
function lrng(seed) {
|
||||||
|
return () => ((2 ** 31 - 1) & (seed = Math.imul(48271, seed))) / 2 ** 31
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility function for generating random Buffer
|
||||||
|
* !!! IT IS NOT CRYPTO SAFE !!!
|
||||||
|
* For that use `crypto.randomBytes()`
|
||||||
|
*
|
||||||
|
* @param length Number of bytes to generate
|
||||||
|
* @param seed Seed for the pseudo-random generator
|
||||||
|
*/
|
||||||
|
function randomByteArray(length, seed = 500) {
|
||||||
|
const rand = lrng(seed)
|
||||||
|
const buf = new Uint8Array(length)
|
||||||
|
|
||||||
|
for (let i = 0; i < length; ++i) {
|
||||||
|
buf[i] = (rand() * 0xff) << 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
async function trafficGen(host = 'http://localhost:1633', seed = 500, bytes = 1024 * 4 * 400) {
|
||||||
|
const randomBytes = randomByteArray(bytes, seed)
|
||||||
|
const bee = new Bee(host)
|
||||||
|
const ref = await bee.uploadData(randomBytes)
|
||||||
|
console.log(`Generated ${bytes} bytes traffic, the random data's root reference: ${ref}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate traffic on Bee node(s)
|
||||||
|
*
|
||||||
|
* @param beeApiUrls Bee API URLs where the random generated data will be sent to.
|
||||||
|
*/
|
||||||
|
async function genTrafficOnOpenPorts(beeApiUrls) {
|
||||||
|
const promises = beeApiUrls.map((url) => {
|
||||||
|
console.log(`Generate Swarm Chunk traffic on ${url}...`)
|
||||||
|
return trafficGen(url, new Date().getTime())
|
||||||
|
})
|
||||||
|
await Promise.all(promises)
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function genTrafficLoop(hosts) {
|
||||||
|
while(true) {
|
||||||
|
await genTrafficOnOpenPorts(hosts)
|
||||||
|
|
||||||
|
await sleep(SLEEP_BETWEEN_UPLOADS_MS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const inputArray = process.argv.slice(2)
|
||||||
|
const hosts = inputArray.length > 0 ? inputArray : [ 'http://localhost:1633' ]
|
||||||
|
genTrafficLoop(hosts)
|
||||||
Loading…
x
Reference in New Issue
Block a user