Web-worker workload levelling
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 - 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 />";
}
Developer | Sarah Cartwright |
Username | SarahC |
Uploaded | December 05, 2022 |
Rating | 3 |
Size | 2,797 Kb |
Views | 8,096 |
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!
Name | Size |
Circuit Snakes | 5,087 Kb |
OLED Emulator - Horizontal bytes | 5,745 Kb |
3D Fireworks | 5,096 Kb |
Iocaine Powder - by Dan Egnor | 5,731 Kb |
Firework - Smoke types | 7,632 Kb |
A Pen by Sarah Cartwright | 1,738 Kb |
3D Tools | 2,502 Kb |
Three.js particles | 7,333 Kb |
OLED Emulator - Sprites | 7,784 Kb |
OLED Emulator - Vertical bytes | 7,111 Kb |
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!
Name | Username | Size |
Gulpfile | Aimhigherwebdesign-amy | 1,765 Kb |
CSS Org Chart | Appirio-ux | 3,882 Kb |
CSS3 Form Page Design | Rssatnam | 3,613 Kb |
Canvas Orbital Trails v2 | Jackrugile | 3,421 Kb |
Multi column experiment. | Spylefkaditis | 2,805 Kb |
Testing Portfolio Page | Sideshowli | 3,395 Kb |
RWD Conversion Practice | Jxqr97 | 2,743 Kb |
Under construction | GhostRider | 1,642 Kb |
Scroll effect with text with help from Skrollr | Luxonglassing | 2,935 Kb |
Perforated foil | 0x04 | 2,617 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!