Groovy Programmer : How to split loads between webbots?

Lately, I am coding a lot Webbots. Sometimes you need to split the load between different machines. Actual physical machines.

Lets same you you have 6 machines at your disposal. Also, lets assume you know the total size of the elements which you want to process in parallel. For this example lets say 50 items of work.  We are going to give each worker an index. Sino we are programmers lets start from index 0. How do I get the collection which the 4th worker ( index 3 ) should take care of?

screenshots

With Groovy It is easy thanks to the collate function. This is how it looks:


static Iterable subcollectionCollate(Iterable l, int webbotIndex, int webbotsInParallel) {
        Integer total = l?.size()
        if ( !total) {
            return []
        }
        if ( webbotsInParallel == 1) {
            return l
        }
        def sublistsSize = (total / (webbotsInParallel)) as int
        l.collate(sublistsSize)[webbotIndex] ?: []
    }


def ids = 1..50
def webbotIndex = 3
def webbotsInParallel = 6
def s = subcollectionCollate(ids, webbotIndex, webbotsInParallel)
assert s == [25, 26, 27, 28, 29, 30, 31, 32]

Do you like to read about Geb development? Yes, then Subscribe to Groovy Calamari a weekly curated email newsletter about the Groovy ecosystem which I write 

Leave a Reply

Your email address will not be published. Required fields are marked *