YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/random_util.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/util/random_util.h"
34
35
#include <cmath>
36
#include <cstdlib>
37
#include <random>
38
39
#include "yb/util/random.h"
40
41
namespace yb {
42
43
namespace {
44
45
const std::string kHumanReadableCharacters =
46
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
47
48
}
49
50
313
void RandomString(void* dest, size_t n, Random* rng) {
51
313
  size_t i = 0;
52
313
  uint32_t random = rng->Next();
53
313
  char* cdest = static_cast<char*>(dest);
54
313
  static const size_t sz = sizeof(random);
55
313
  if (n >= sz) {
56
144M
    for (i = 0; i <= n - sz; i += sz) {
57
144M
      memcpy(&cdest[i], &random, sizeof(random));
58
144M
      random = rng->Next();
59
144M
    }
60
294
  }
61
313
  memcpy(cdest + i, &random, n - i);
62
313
}
63
64
2.00M
uint32_t GetRandomSeed32() {
65
2.00M
  std::random_device device;
66
2.00M
  return device();
67
2.00M
}
68
69
8
std::vector<uint8_t> RandomBytes(size_t len, std::mt19937_64* rng) {
70
8
  std::vector<uint8_t> data(len);
71
13.1k
  std::generate(data.begin(), data.end(), [=] { return RandomUniformInt(0, UCHAR_MAX, rng); });
72
8
  return data;
73
8
}
74
75
465k
std::string RandomString(size_t len, std::mt19937_64* rng) {
76
465k
  std::string str;
77
465k
  str.reserve(len);
78
193M
  while (len > 0) {
79
193M
    str += yb::RandomUniformInt<char>();
80
193M
    len--;
81
193M
  }
82
465k
  return str;
83
465k
}
84
85
0
std::string RandomHumanReadableString(size_t len, Random* rnd) {
86
  // TODO: https://yugabyte.atlassian.net/browse/ENG-1508: Avoid code duplication in yb::Random and
87
  // rocksdb::Random. Currently this does not allow to reuse the same function in both code bases.
88
0
  std::string ret;
89
0
  ret.resize(len);
90
0
  for (size_t i = 0; i < len; ++i) {
91
0
    ret[i] = static_cast<char>('a' + rnd->Uniform(26));
92
0
  }
93
0
  return ret;
94
0
}
95
96
35.1k
std::string RandomHumanReadableString(size_t len, std::mt19937_64* rng) {
97
35.1k
  std::string ret(len, 0);
98
234M
  for (size_t i = 0; i != len; ++i) {
99
234M
    ret[i] = RandomElement(kHumanReadableCharacters, rng);
100
234M
  }
101
35.1k
  return ret;
102
35.1k
}
103
104
namespace {
105
thread_local std::unique_ptr<std::mt19937_64> thread_local_random_ptr;
106
}
107
108
476M
std::mt19937_64& ThreadLocalRandom() {
109
476M
  auto* result = thread_local_random_ptr.get();
110
485M
  if (result) {
111
485M
    return *result;
112
485M
  }
113
18.4E
  thread_local_random_ptr.reset(result = new std::mt19937_64);
114
18.4E
  Seed(result);
115
18.4E
  return *result;
116
18.4E
}
117
118
697k
bool RandomUniformBool(std::mt19937_64* rng) {
119
697k
  return RandomUniformInt(0, 1, rng) != 0;
120
697k
}
121
122
} // namespace yb