github.com/rajeev159/opa@v0.45.0/ast/compile_bench_test.go (about)

     1  package ast
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func BenchmarkRewriteDynamics(b *testing.B) {
     9  
    10  	// The choice of query to use is somewhat arbitrary. This query is
    11  	// representative of the ones that result from partial evaluation on IAM
    12  	// data models (e.g., a triple glob match on subject/action/resource.)
    13  	body := MustParseBody(`
    14  		glob.match("a:*", [":"], input.abcdef.x12345);
    15  		glob.match("a:*", [":"], input.abcdef.y12345);
    16  		glob.match("a:*", [":"], input.abcdef.z12345)
    17  	`)
    18  	sizes := []int{1, 10, 100, 1000, 10000, 100000}
    19  	queries := makeQueriesForRewriteDynamicsBenchmark(sizes, body)
    20  
    21  	for i := range sizes {
    22  		b.Run(fmt.Sprint(sizes[i]), func(b *testing.B) {
    23  			factory := newEqualityFactory(newLocalVarGenerator("q", nil))
    24  			b.ResetTimer()
    25  			for n := 0; n < b.N; n++ {
    26  				for _, body := range queries[i] {
    27  					rewriteDynamics(factory, body)
    28  				}
    29  			}
    30  		})
    31  	}
    32  
    33  }
    34  
    35  func makeQueriesForRewriteDynamicsBenchmark(sizes []int, body Body) [][]Body {
    36  
    37  	queries := make([][]Body, len(sizes))
    38  
    39  	for i := range queries {
    40  		queries[i] = make([]Body, sizes[i])
    41  		for j := 0; j < sizes[i]; j++ {
    42  			queries[i][j] = body.Copy()
    43  		}
    44  	}
    45  
    46  	return queries
    47  }