summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/ginkgo/internal/spec_iterator/index_computer.go
blob: 82272554aa8210d1e1daebf06550f736f6c800d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package spec_iterator

func ParallelizedIndexRange(length int, parallelTotal int, parallelNode int) (startIndex int, count int) {
	if length == 0 {
		return 0, 0
	}

	// We have more nodes than tests. Trivial case.
	if parallelTotal >= length {
		if parallelNode > length {
			return 0, 0
		} else {
			return parallelNode - 1, 1
		}
	}

	// This is the minimum amount of tests that a node will be required to run
	minTestsPerNode := length / parallelTotal

	// This is the maximum amount of tests that a node will be required to run
	// The algorithm guarantees that this would be equal to at least the minimum amount
	// and at most one more
	maxTestsPerNode := minTestsPerNode
	if length%parallelTotal != 0 {
		maxTestsPerNode++
	}

	// Number of nodes that will have to run the maximum amount of tests per node
	numMaxLoadNodes := length % parallelTotal

	// Number of nodes that precede the current node and will have to run the maximum amount of tests per node
	var numPrecedingMaxLoadNodes int
	if parallelNode > numMaxLoadNodes {
		numPrecedingMaxLoadNodes = numMaxLoadNodes
	} else {
		numPrecedingMaxLoadNodes = parallelNode - 1
	}

	// Number of nodes that precede the current node and will have to run the minimum amount of tests per node
	var numPrecedingMinLoadNodes int
	if parallelNode <= numMaxLoadNodes {
		numPrecedingMinLoadNodes = 0
	} else {
		numPrecedingMinLoadNodes = parallelNode - numMaxLoadNodes - 1
	}

	// Evaluate the test start index and number of tests to run
	startIndex = numPrecedingMaxLoadNodes*maxTestsPerNode + numPrecedingMinLoadNodes*minTestsPerNode
	if parallelNode > numMaxLoadNodes {
		count = minTestsPerNode
	} else {
		count = maxTestsPerNode
	}
	return
}