ApiTester: Use thread-local random generators

This commit is contained in:
Vaidas Gasiunas 2022-03-09 13:28:12 +01:00
parent 892538e233
commit 8d2c3a4773
5 changed files with 17 additions and 12 deletions

View File

@ -86,9 +86,9 @@ public:
}
private:
std::string randomKeyName() { return keyPrefix + random.randomStringLowerCase(minKeyLength, maxKeyLength); }
std::string randomKeyName() { return keyPrefix + Random::get().randomStringLowerCase(minKeyLength, maxKeyLength); }
std::string randomValue() { return random.randomStringLowerCase(minValueLength, maxValueLength); }
std::string randomValue() { return Random::get().randomStringLowerCase(minValueLength, maxValueLength); }
std::string randomNotExistingKey() {
while (true) {
@ -114,7 +114,7 @@ private:
}
std::string randomKey(double existingKeyRatio) {
if (random.randomBool(existingKeyRatio)) {
if (Random::get().randomBool(existingKeyRatio)) {
return randomExistingKey();
} else {
return randomNotExistingKey();
@ -122,7 +122,7 @@ private:
}
void randomInsertOp(TTaskFct cont) {
int numKeys = random.randomInt(1, maxKeysPerTransaction);
int numKeys = Random::get().randomInt(1, maxKeysPerTransaction);
auto kvPairs = std::make_shared<std::vector<KeyValue>>();
for (int i = 0; i < numKeys; i++) {
kvPairs->push_back(KeyValue{ randomNotExistingKey(), randomValue() });
@ -143,7 +143,7 @@ private:
}
void randomCommitReadOp(TTaskFct cont) {
int numKeys = random.randomInt(1, maxKeysPerTransaction);
int numKeys = Random::get().randomInt(1, maxKeysPerTransaction);
auto kvPairs = std::make_shared<std::vector<KeyValue>>();
for (int i = 0; i < numKeys; i++) {
kvPairs->push_back(KeyValue{ randomKey(readExistingKeysRatio), randomValue() });
@ -193,7 +193,7 @@ private:
}
void randomGetOp(TTaskFct cont) {
int numKeys = random.randomInt(1, maxKeysPerTransaction);
int numKeys = Random::get().randomInt(1, maxKeysPerTransaction);
auto keys = std::make_shared<std::vector<std::string>>();
auto results = std::make_shared<std::vector<std::optional<std::string>>>();
for (int i = 0; i < numKeys; i++) {
@ -228,7 +228,7 @@ private:
}
void randomClearOp(TTaskFct cont) {
int numKeys = random.randomInt(1, maxKeysPerTransaction);
int numKeys = Random::get().randomInt(1, maxKeysPerTransaction);
auto keys = std::make_shared<std::vector<std::string>>();
for (int i = 0; i < numKeys; i++) {
keys->push_back(randomExistingKey());
@ -266,7 +266,7 @@ private:
}
void randomOperation(TTaskFct cont) {
OpType txType = (store.size() == 0) ? OP_INSERT : (OpType)random.randomInt(0, OP_LAST);
OpType txType = (store.size() == 0) ? OP_INSERT : (OpType)Random::get().randomInt(0, OP_LAST);
switch (txType) {
case OP_INSERT:
randomInsertOp(cont);
@ -313,7 +313,6 @@ private:
}
int numOpLeft;
Random random;
KeyValueStore store;
};

View File

@ -238,7 +238,7 @@ public:
}
void execute(std::shared_ptr<ITransactionActor> txActor, TTaskFct cont) override {
int idx = random.randomInt(0, options.numDatabases - 1);
int idx = Random::get().randomInt(0, options.numDatabases - 1);
executeWithDatabase(databases[idx], txActor, cont);
}
@ -250,7 +250,6 @@ public:
private:
std::vector<FDBDatabase*> databases;
Random random;
};
class DBPerTransactionExecutor : public TransactionExecutorBase {

View File

@ -32,6 +32,11 @@ int Random::randomInt(int min, int max) {
return std::uniform_int_distribution<int>(min, max)(random);
}
Random& Random::get() {
static thread_local Random random;
return random;
}
std::string Random::randomStringLowerCase(int minLength, int maxLength) {
int length = randomInt(minLength, maxLength);
std::string str;

View File

@ -51,6 +51,8 @@ class Random {
public:
Random();
static Random& get();
int randomInt(int min, int max);
std::string randomStringLowerCase(int minLength, int maxLength);

View File

@ -212,7 +212,7 @@ void applyNetworkOptions(TesterOptions& options) {
}
void randomizeOptions(TesterOptions& options) {
Random random;
Random& random = Random::get();
options.numFdbThreads = random.randomInt(options.testSpec.minFdbThreads, options.testSpec.maxFdbThreads);
options.numClientThreads = random.randomInt(options.testSpec.minClientThreads, options.testSpec.maxClientThreads);
options.numDatabases = random.randomInt(options.testSpec.minDatabases, options.testSpec.maxDatabases);