| 1 | <!DOCTYPE html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <title>Worker pool statistics</title> |
| 5 | <style> |
| 6 | body { |
| 7 | width: 600px; |
| 8 | font: 11pt arial; |
| 9 | } |
| 10 | </style> |
| 11 | |
| 12 | <script src="../../dist/workerpool.js"></script> |
| 13 | </head> |
| 14 | <body> |
| 15 | |
| 16 | <p> |
| 17 | Create some tasks and watch the statistics |
| 18 | </p> |
| 19 | |
| 20 | <input type="button" id="sleep2" value="Create a sleep task (2 seconds)"> |
| 21 | <input type="button" id="sleep5" value="Create a sleep task (5 seconds)"> |
| 22 | |
| 23 | <pre> |
| 24 | <code id="stats"></code> |
| 25 | </pre> |
| 26 | |
| 27 | <script> |
| 28 | var pool = workerpool.pool({maxWorkers: 4}); |
| 29 | |
| 30 | // show current stats of the workerpool on screen |
| 31 | function updateStats () { |
| 32 | document.getElementById('stats').innerHTML = JSON.stringify(pool.stats(), null, 2); |
| 33 | } |
| 34 | |
| 35 | setInterval(updateStats, 200); |
| 36 | |
| 37 | /** |
| 38 | * A dumb task that waits for some time before resolving |
| 39 | * @param {number} duration duration in milliseconds |
| 40 | * @return {Promise} |
| 41 | */ |
| 42 | function sleep (duration) { |
| 43 | return new Promise (function (resolve, reject) { |
| 44 | setTimeout(resolve, duration) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | document.getElementById('sleep2').onclick = function () { |
| 49 | console.log('start sleep(2000)'); |
| 50 | pool.exec(sleep, [2000]) |
| 51 | .then(function () { |
| 52 | console.log('finished sleep(2000)'); |
| 53 | }) |
| 54 | .catch(function (err) { |
| 55 | console.error(err); |
| 56 | }); |
| 57 | |
| 58 | updateStats(); |
| 59 | }; |
| 60 | |
| 61 | document.getElementById('sleep5').onclick = function () { |
| 62 | console.log('start sleep(5000)'); |
| 63 | pool.exec(sleep, [5000]) |
| 64 | .then(function () { |
| 65 | console.log('finished sleep(5000)'); |
| 66 | }) |
| 67 | .catch(function (err) { |
| 68 | console.error(err); |
| 69 | }); |
| 70 | |
| 71 | updateStats(); |
| 72 | }; |
| 73 | |
| 74 | </script> |
| 75 | |
| 76 | </body> |
| 77 | </html> |