diff options
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/test/IceUtil/uuid/Client.cpp | 97 |
1 files changed, 89 insertions, 8 deletions
diff --git a/cpp/test/IceUtil/uuid/Client.cpp b/cpp/test/IceUtil/uuid/Client.cpp index 7e2e096adfc..2b62a4554e7 100644 --- a/cpp/test/IceUtil/uuid/Client.cpp +++ b/cpp/test/IceUtil/uuid/Client.cpp @@ -9,30 +9,81 @@ #include <IceUtil/UUID.h> #include <IceUtil/Time.h> +#include <IceUtil/Thread.h> +#include <IceUtil/StaticMutex.h> #include <TestCommon.h> #include <set> +#include <vector> using namespace IceUtil; using namespace std; +static StaticMutex staticMutex = ICE_STATIC_MUTEX_INITIALIZER; + inline void usage(const char* myName) { - cerr << "Usage: " << myName << " [number of UUIDs to generate]" << endl; + cerr << "Usage: " << myName << " [number of UUIDs to generate] [number of threads]" << endl; } + +class InsertThread : public Thread +{ +public: + + + InsertThread(int threadId, set<string>& uuidSet, long howMany, bool verbose) + : _threadId(threadId), _uuidSet(uuidSet), _howMany(howMany), _verbose(verbose) + { + } + + + void run() + { + for(long i = 0; i < _howMany; i++) + { + StaticMutex::Lock lock(staticMutex); + + string uuid = generateUUID(); + + pair<set<string>::iterator, bool> ok = _uuidSet.insert(uuid); + if(!ok.second) + { + cerr << "******* iteration " << i << endl; + cerr << "******* Duplicate UUID: " << *ok.first << endl; + } + + test(ok.second); + + if(_verbose && i > 0 && (i % 100000 == 0)) + { + cout << "Thread " << _threadId << ": generated " << i << " UUIDs." << endl; + } + } + } + + +private: + int _threadId; + set<string>& _uuidSet; + long _howMany; + bool _verbose; +}; + + int main(int argc, char* argv[]) { long howMany = 10000; + int threadCount = 1; bool verbose = false; - if(argc > 2) + if(argc > 3) { usage(argv[0]); return EXIT_FAILURE; } - else if(argc == 2) + else if(argc == 3) { howMany = atol(argv[1]); if (howMany == 0) @@ -40,19 +91,49 @@ int main(int argc, char* argv[]) usage(argv[0]); return EXIT_FAILURE; } + threadCount = atoi(argv[2]); + if(threadCount <= 0) + { + usage(argv[0]); + return EXIT_FAILURE; + } verbose = true; } + else if(argc == 2) + { + howMany = atol(argv[1]); + if (howMany == 0) + { + usage(argv[0]); + return EXIT_FAILURE; + } + } - cout << "Generating " << howMany << " UUIds ... "; + cout << "Generating " << howMany << " UUIds using " << threadCount << " thread"; + if(threadCount > 1) + { + cout << "s"; + } + cout << "... "; + + if(verbose) + { + cout << endl; + } set<string> uuidSet; + vector<ThreadControl> threads; + Time start = Time::now(); - for(long i = 0; i < howMany; i++) + for(int i = 0; i < threadCount; i++) + { + ThreadPtr t = new InsertThread(i, uuidSet, howMany / threadCount, verbose); + threads.push_back(t->start()); + } + for(vector<ThreadControl>::iterator p = threads.begin(); p != threads.end(); ++p) { - pair<set<string>::iterator, bool> ok - = uuidSet.insert(generateUUID()); - test(ok.second); + p->join(); } Time finish = Time::now(); |