github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/resolver/solver/bench_test.go (about)

     1  package solver
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  var BenchmarkInput = func() []Variable {
    11  	const (
    12  		length      = 256
    13  		seed        = 9
    14  		pMandatory  = .1
    15  		pDependency = .15
    16  		nDependency = 6
    17  		pConflict   = .05
    18  		nConflict   = 3
    19  	)
    20  
    21  	rnd := rand.New(rand.NewSource(seed))
    22  
    23  	id := func(i int) Identifier {
    24  		return Identifier(strconv.Itoa(i))
    25  	}
    26  
    27  	variable := func(i int) TestVariable {
    28  		var c []Constraint
    29  		if rnd.Float64() < pMandatory {
    30  			c = append(c, Mandatory())
    31  		}
    32  		if rnd.Float64() < pDependency {
    33  			n := rnd.Intn(nDependency-1) + 1
    34  			var d []Identifier
    35  			for x := 0; x < n; x++ {
    36  				y := i
    37  				for y == i {
    38  					y = rnd.Intn(length)
    39  				}
    40  				d = append(d, id(y))
    41  			}
    42  			c = append(c, Dependency(d...))
    43  		}
    44  		if rnd.Float64() < pConflict {
    45  			n := rnd.Intn(nConflict-1) + 1
    46  			for x := 0; x < n; x++ {
    47  				y := i
    48  				for y == i {
    49  					y = rnd.Intn(length)
    50  				}
    51  				c = append(c, Conflict(id(y)))
    52  			}
    53  		}
    54  		return TestVariable{
    55  			identifier:  id(i),
    56  			constraints: c,
    57  		}
    58  	}
    59  
    60  	result := make([]Variable, length)
    61  	for i := range result {
    62  		result[i] = variable(i)
    63  	}
    64  	return result
    65  }()
    66  
    67  func BenchmarkSolve(b *testing.B) {
    68  	for i := 0; i < b.N; i++ {
    69  		s, err := New(WithInput(BenchmarkInput))
    70  		if err != nil {
    71  			b.Fatalf("failed to initialize solver: %s", err)
    72  		}
    73  		s.Solve(context.Background())
    74  	}
    75  }
    76  
    77  func BenchmarkNewInput(b *testing.B) {
    78  	for i := 0; i < b.N; i++ {
    79  		_, err := New(WithInput(BenchmarkInput))
    80  		if err != nil {
    81  			b.Fatalf("failed to initialize solver: %s", err)
    82  		}
    83  	}
    84  }