Web-worker workload levelling

Size
2,797 Kb
Views
8,096

How do I make an web-worker workload levelling?

What is a web-worker workload levelling? How do you make a web-worker workload levelling? This script and codes were developed by Sarah Cartwright on 05 December 2022, Monday.

Web-worker workload levelling Previews

Web-worker workload levelling - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Web-worker workload levelling</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> This code splits a group of objects running on Web Workers equally amongst them.<br />
<br />
It returns an array of objects that describe the start, end index and count of objects that each Web-Worker needs to work on.<br />
The indexes are INCLUDING the values specified.<br />
<br />
For example:
<pre>
var indexes = webWorkerBalance({objects: 10, webWorkers: 3});
indexes = [ { startIndex: 0, endIndex: 3, objects: 4 } { startIndex: 4, endIndex: 6, objects: 3 } { startIndex: 7, endIndex: 9, objects: 3 }
]
</pre>
<br />
<br /> <script src="js/index.js"></script>
</body>
</html>

Web-worker workload levelling - Script Codes CSS Codes

body{ padding: 20px; margin: 0px; background: #2e2e2e; color: #909090; font: 20px arial, sans-serif;
}
canvas{ border: 1px solid black;
}

Web-worker workload levelling - Script Codes JS Codes

/*jshint esversion: 6 */
//Demo to show how we can spread an array of items equally across a given number of threads/web-workers...
allPassed = true;
testOutputText = "";
for(let webWorkers = 3; webWorkers < 14; webWorkers+=4) for(let objectCount = 0; objectCount < 30; objectCount+=5) testWebWorkerBalance(objectCount, webWorkers);
if(allPassed) document.write("All the tests below passed!");
else document.write("At least one test below failed - please report which one!");
document.write("<br /><br /><hr /><br />");
document.write(testOutputText);
/*
This is the levelling function.
It returns an array of objects that describe the start, end index and count of objects that each Web-Worker needs to work on.
var indexes = distribute({objects: 10, threads: 3});
indexes = [ { startIndex: 0, endIndex: 3, objects: 4 } { startIndex: 4, endIndex: 6, objects: 3 } { startIndex: 7, endIndex: 9, objects: 3 }
]
*/
function webWorkerBalance(params){ let t = 0, tn = 0, d = [], o = params.objects, c = params.webWorkers, groupSize = Math.floor(o/c); for(let i = 0; i < c; i++){ tn = t + groupSize + (i < (o % c) ? 1 : 0); if(t < tn) d.push({startIndex: t, endIndex: tn - 1, objects: (tn-t)}); else d.push({startIndex: 0, endIndex: 0, objects: 0}); t = tn; } return d;
}
//Run a test...
function testWebWorkerBalance(objectCount, webWorkers){ testOutputText += "Levelling " + objectCount + " objects of work across " + webWorkers + " Web-Workers.<br /><br />"; var indexes = webWorkerBalance({objects: objectCount, webWorkers: webWorkers}); let totalObjectsInArray = 0; for(let i=0; i < indexes.length; i++){ let currentCoreDetails = indexes[i]; totalObjectsInArray += currentCoreDetails.objects; testOutputText += "Web-Worker Id: " + i + "...Start index: " + currentCoreDetails.startIndex + "...End index: " + currentCoreDetails.endIndex; if(currentCoreDetails.objects > 0) testOutputText += "...Total objects on this Web-Worker: " + currentCoreDetails.objects + "<br />"; else testOutputText += "...No objects on this Web-Worker<br />"; } testOutputText += "<br />Objects mentioned in array: " + totalObjectsInArray + "<br />"; if(totalObjectsInArray === objectCount) testOutputText += "TEST PASSED - All objects accounted for!<br />"; else{ testOutputText += "TEST FAILED - Woops... The object count in the array doesn't match the required count!<br />"; allPassed = false; } testOutputText += "<br /><hr /><br />";
}
Web-worker workload levelling - Script Codes
Web-worker workload levelling - Script Codes
Home Page Home
Developer Sarah Cartwright
Username SarahC
Uploaded December 05, 2022
Rating 3
Size 2,797 Kb
Views 8,096
Do you need developer help for Web-worker workload levelling?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Sarah Cartwright (SarahC) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!