github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/ledger/random.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package ledger
    12  
    13  import (
    14  	"math/rand"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/timeutil"
    18  	"github.com/cockroachdb/cockroach/pkg/util/uuid"
    19  )
    20  
    21  const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    22  const aChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    23  
    24  // randInt returns a number within [min, max] inclusive.
    25  func randInt(rng *rand.Rand, min, max int) int {
    26  	return rng.Intn(max-min+1) + min
    27  }
    28  
    29  func randStringFromAlphabet(rng *rand.Rand, minLen, maxLen int, alphabet string) string {
    30  	size := maxLen
    31  	if maxLen-minLen != 0 {
    32  		size = randInt(rng, minLen, maxLen)
    33  	}
    34  	if size == 0 {
    35  		return ""
    36  	}
    37  
    38  	b := make([]byte, size)
    39  	for i := range b {
    40  		b[i] = alphabet[rng.Intn(len(alphabet))]
    41  	}
    42  	return string(b)
    43  }
    44  
    45  // randAString generates a random alphanumeric string of length between min and
    46  // max inclusive.
    47  func randAString(rng *rand.Rand, min, max int) string {
    48  	return randStringFromAlphabet(rng, min, max, aChars)
    49  }
    50  
    51  // randPaymentID produces a random payment id string.
    52  func randPaymentID(rng *rand.Rand) string {
    53  	uuidStr := uuid.MakeV4().String()
    54  	return paymentIDPrefix + uuidStr
    55  }
    56  
    57  // randContext produces a random context string.
    58  func randContext(rng *rand.Rand) string {
    59  	return randAString(rng, 56, 56)
    60  }
    61  
    62  // randUsername produces a random username string.
    63  func randUsername(rng *rand.Rand) string {
    64  	return randAString(rng, 18, 20)
    65  }
    66  
    67  // randResponse produces a random transaction response string.
    68  func randResponse(rng *rand.Rand) string {
    69  	return randAString(rng, 400, 400)
    70  }
    71  
    72  // randCurrencyCode produces a random currency code string.
    73  func randCurrencyCode(rng *rand.Rand) string {
    74  	return randStringFromAlphabet(rng, 3, 3, letters)
    75  }
    76  
    77  // randTimestamp produces a random timestamp.
    78  func randTimestamp(rng *rand.Rand) time.Time {
    79  	return timeutil.Unix(rng.Int63n(1600000000), rng.Int63())
    80  }
    81  
    82  // randSessionID produces a random session ID string.
    83  func randSessionID(rng *rand.Rand) string {
    84  	return randAString(rng, 60, 62)
    85  }
    86  
    87  // randSessionData produces a random session data string.
    88  func randSessionData(rng *rand.Rand) string {
    89  	return randAString(rng, 160, 160)
    90  }
    91  
    92  // randAmount produces a random amount string.
    93  func randAmount(rng *rand.Rand) float64 {
    94  	return float64(randInt(rng, 100, 100000)) / 100
    95  }
    96  
    97  // randCustomer returns a random customer ID.
    98  func (w ledger) randCustomer(rng *rand.Rand) int {
    99  	return randInt(rng, 0, w.customers-1)
   100  }