Skip to content

Commit

Permalink
Stress Tests 3 - create long duration ws tests (#6588)
Browse files Browse the repository at this point in the history
* add script

* add eslint disable

* add details

* add browser test

* fix manual ws test cases

* add manual tests to stress folder

* update naming

* add test:manual script

* rename folder

* update script

* add html comment
  • Loading branch information
Alex authored Nov 20, 2023
1 parent af91519 commit 2785782
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ yarn add web3
| test | Uses `jest` to run unit tests in each package |
| test:integration | Uses `jest` to run tests under `/test/integration` in each package |
| test:unit | Uses `jest` to run tests under `/test/unit` in each package |
| test:manual | Runs manual tests under `test/manual` in the web3 package |

[npm-url]: https://npmjs.org/package/web3
[downloads-image]: https://img.shields.io/npm/dm/web3?label=npm%20downloads
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"test:manual:stress:data": "packages/web3/test/stress/start.sh",
"test:manual:stress:validation": "npx ts-node packages/web3/test/stress/validator.ts",
"test:manual:stress": "yarn test:manual:stress:data && yarn test:manual:stress:validation",
"test:manual:long-connection-ws":"node ./packages/web3/test/manual/long_ws_tests/nodejs_test/long_connection_ws.js",
"test:manual":"yarn test:manual:stress && yarn test:manual:long-connection-ws",
"husky:install": "husky install",
"husky:uninstall": "husky uninstall",
"postinstall": "yarn build",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Manual browser tests</title>
<!-- To Run -->
<script src="../../../../dist/web3.min.js">
</script>
</head>
<body>

<p> Manual browser test - for this test you will need to provide an ws infura endpoint in this html page. This will test the web3js ws provider by sending a request every 10 minutes for 10 hours </p>
<p> Have this html test open in a browser of your choice</p>
<p> Keep dev console open in case of any unintended errors occur </p>
<!-- Display start date -->
<p>Start time: <span id="startTime"></span></p>

<!-- Display end date -->
<p>End Time: <span id="endTime"></span></p>

<!-- Display attempts made -->
<p>Number of requests sent: <span id="attempts"></span></p>

<!-- How long it has ran for -->
<p>Has ran for <span id="minutes"></span> minutes</p>

<!-- Display block number -->
<p>Block number: <span id="call"></span></p>

<!-- Display result -->
<p>Result: <span id="result"></span></p>

<script>

let web3;
let attempt = 0;
let intervalId;
let start;
let end;
let result;
// You will need to set mainnet infura provider
web3 = new Web3("");

// constantly send requests through WS for 10 hours
const sendRequests = () => {
return new Promise((resolve, reject) => {
// send a request in intervals of 10 minutes
intervalId = setInterval( async() => {
try{
const block = await web3.eth.getBlock()
attempt++;
document.getElementById("attempts").innerText = attempt.toString();
document.getElementById("call").innerText = block.number;
document.getElementById("minutes").innerText = attempt*10;
if (attempt === 144) { // after 10 hours
clearInterval(intervalId);
resolve("");
document.getElementById("result").innerText = "success";
}
} catch (error) {
clearInterval(intervalId);
document.getElementById("result").innerText = "Error";
reject(error);
}
},60000) // every 10 minutes
})

}

const main = async () => {

try {
start = new Date();
document.getElementById("startTime").innerText = start.toTimeString();
document.getElementById("attempts").innerText = attempt.toString();
document.getElementById("endTime").innerText = "Currently running";
const promise = sendRequests();
await promise;
} catch (e) {
console.warn("error")
}
end = new Date();
document.getElementById("endDate").innerText = new Date().toTimeString();

}
main();
</script>

<!-- You can include additional scripts at the end of the body section if needed -->


</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-disable */
const { Web3 } = require('../../../../lib/commonjs');
const secrets = require('../../../../../../.secrets.json');

let web3;
let attempt = 0;
let intervalId;
let start;
let end;

// constantly send requests through WS for 10 hours
const sendRequests = () => {
start = new Date();
console.log("start:",start)
return new Promise((resolve, reject) => {
// send a request in intervals of 10 minutes
intervalId = setInterval( async() => {
try{
const block = await web3.eth.getBlock()
attempt++;
console.log(block)
console.log("successful calls:", attempt, "has ran for:", attempt*10, "minutes")
if (attempt === 144) { // after 10 hours
clearInterval(intervalId);
resolve("");
}
} catch (error) {
clearInterval(intervalId);
reject(error);
}
},600000) // every 10 minutes
})

}

const main = async () => {

try {
// You will need to set mainnet infura provider
const provider = secrets.MAINNET.WS;
web3 = new Web3(provider);
const promise = sendRequests();
await promise;
end = new Date();
console.log("websocket test successful")
} catch (e) {
console.warn("error occured during ws test, on attempt: ", attempt, "program ran for: ", attempt ,"minutes with error: ", e)
}
console.log("start", start)
console.log("end", end)
process.exit();
}

main();

0 comments on commit 2785782

Please sign in to comment.