Skip to content

Instantly share code, notes, and snippets.

@false-git
Last active May 14, 2023 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save false-git/d54067f536c5ff99291e5c6d141a60a1 to your computer and use it in GitHub Desktop.
Save false-git/d54067f536c5ff99291e5c6d141a60a1 to your computer and use it in GitHub Desktop.
UUIDの衝突が起きるかテストするプログラム (バージョン2)
// c++ -std=c++20 -O2 uuid_test.cc
#include <chrono>
#include <iostream>
#include <thread>
#include <memory>
#include <set>
#include <vector>
#include <uuid/uuid.h>
struct Uuid {
uuid_t uuid;
Uuid() = default;
Uuid(const Uuid &other) { uuid_copy(uuid, other.uuid); }
Uuid(const uuid_t other_uuid) { uuid_copy(uuid, other_uuid); }
void generateInplace() { uuid_generate(uuid); }
static Uuid generate() { Uuid wrapped; uuid_generate(wrapped.uuid); return wrapped; }
bool operator<(const Uuid &other) const { return uuid_compare(uuid, other.uuid) < 0; }
bool operator==(const Uuid &other) const { return uuid_compare(uuid, other.uuid) == 0; }
};
void gen(int id, int num, std::shared_ptr<uuid_t[]> buf) {
for (int i = 0; i < num; i++) {
uuid_generate(buf[i]);
}
}
// 同時に動かすスレッドの数
const auto NUM_THREAD = 8;
// 1スレッドが1回に生成するUUIDの数
const auto NUM_UUID = 100000;
// setの更新にかかる時間が以下を超えたらsetをリセットする閾値
const auto THRESHOLD = 6;
int main(int argc, char *argv[])
{
std::set<Uuid> set;
std::vector<std::shared_ptr<uuid_t[]>> uuids;
for (int i = 0; i < NUM_THREAD; i++) {
uuids.push_back(std::make_shared<uuid_t[]>(NUM_UUID));
}
std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point previous;
previous = start = std::chrono::system_clock::now();
size_t count = 0;
while(true) {
std::vector<std::thread> threads;
for (int i = 0; i < NUM_THREAD; i++) {
threads.push_back(std::thread(gen, i, NUM_UUID, uuids[i]));
}
for (auto &t : threads) {
t.join();
}
auto now = std::chrono::system_clock::now();
auto elapsed1 = std::chrono::duration_cast<std::chrono::milliseconds>(now - previous).count() / 1000.;
previous = now;
for (int i = 0; i < NUM_THREAD; i++) {
for (int j = 0; j < NUM_UUID; j++) {
if (set.find(uuids[i][j]) != set.end()) {
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count() / 1000.;
uuid_string_t u1str;
uuid_unparse(uuids[i][j], u1str);
std::cout << count << "," << elapsed << "," << set.size() << "," << u1str << std::endl;
exit(1);
}
set.insert(uuids[i][j]);
}
}
count++;
auto size = set.size();
now = std::chrono::system_clock::now();
auto elapsed2 = std::chrono::duration_cast<std::chrono::milliseconds>(now - previous).count() / 1000.;
if (elapsed2 > THRESHOLD) {
std::cout << count << "," << elapsed1 << "," << elapsed2 << "," << size << ",reset" << std::endl;
set.clear();
now = std::chrono::system_clock::now();
} else {
std::cout << count << "," << elapsed1 << "," << elapsed2 << "," << size << std::endl;
}
previous = now;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment