github.com/observiq/carbon@v0.9.11-0.20200820160507-1b872e368a5e/operator/builtin/builtin_test.go (about)

     1  package builtin
     2  
     3  import (
     4  	"crypto/rand"
     5  	"encoding/hex"
     6  	"fmt"
     7  )
     8  
     9  type inputterBenchmark struct {
    10  	fields      int
    11  	depth       int
    12  	fieldLength int
    13  }
    14  
    15  func (b inputterBenchmark) String() string {
    16  	return fmt.Sprintf("Fields=%d,Depth=%d,Length=%d", b.fields, b.depth, b.fieldLength)
    17  }
    18  
    19  func (b inputterBenchmark) EstimatedBytes() int64 {
    20  	pow := func(a, b int) int {
    21  		n := 1
    22  		for i := 0; i < b; i++ {
    23  			n = n * a
    24  		}
    25  		return n
    26  	}
    27  
    28  	bytes := 0
    29  	for i := 1; i < b.depth+2; i++ {
    30  		bytes += pow(b.fields, i) * b.fieldLength
    31  	}
    32  	bytes += pow(b.fields, b.depth+1) * b.fieldLength
    33  
    34  	return int64(bytes)
    35  }
    36  
    37  var standardInputterBenchmarks = []inputterBenchmark{
    38  	{0, 0, 10},
    39  	{1, 0, 10},
    40  	{1, 0, 100},
    41  	{1, 0, 1000},
    42  	{10, 0, 10},
    43  	{2, 2, 10},
    44  	{2, 10, 10},
    45  }
    46  
    47  // generateEntry creates an entry with a configurable number
    48  // of fields per level of the map, as well as a configurable
    49  // number of nested fields for a total of fields ^ depth leaf values
    50  // Example: fields = 1, depth = 2
    51  // {
    52  // 	"asdf1": {
    53  // 		"asdf2": "asdf3",
    54  // 	},
    55  // }
    56  func generateRandomNestedMap(fields int, depth int, bytes int) map[string]interface{} {
    57  	generated := make(map[string]interface{})
    58  	buffer := make([]byte, bytes)
    59  	for i := 0; i < fields; i++ {
    60  		_, _ = rand.Read(buffer)
    61  		field := hex.EncodeToString(buffer)
    62  		if depth == 0 {
    63  			_, _ = rand.Read(buffer)
    64  			value := hex.EncodeToString(buffer)
    65  			generated[field] = value
    66  		} else {
    67  			generated[field] = generateRandomNestedMap(fields, depth-1, bytes)
    68  		}
    69  	}
    70  
    71  	return generated
    72  }