1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#include <IceUtil/UUID.h>
#include <IceUtil/Random.h>
#include <IceUtil/Time.h>
#include <IceUtil/Thread.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/MutexPtrLock.h>
#include <TestHelper.h>
#include <set>
#include <vector>
using namespace IceUtil;
using namespace std;
namespace
{
Mutex* staticMutex = 0;
class Init
{
public:
Init()
{
staticMutex = new IceUtil::Mutex;
}
~Init()
{
delete staticMutex;
staticMutex = 0;
}
};
Init init;
}
inline void usage(const char* myName)
{
cerr << "Usage: " << myName << " [number of UUIDs to generate] [number of threads]" << endl;
}
template<typename T, typename GenerateFunc> class InsertThread : public Thread
{
public:
typedef set<T> ItemSet;
InsertThread(int threadId, ItemSet& itemSet, GenerateFunc func, long howMany, bool verbose)
: _threadId(threadId), _itemSet(itemSet), _func(func), _howMany(howMany), _verbose(verbose)
{
}
void run()
{
for(long i = 0; i < _howMany; i++)
{
T item = _func();
IceUtilInternal::MutexPtrLock<IceUtil::Mutex> lock(staticMutex);
pair<typename ItemSet::iterator, bool> ok = _itemSet.insert(item);
if(!ok.second)
{
cerr << "******* iteration " << i << endl;
cerr << "******* Duplicate item: " << *ok.first << endl;
}
test(ok.second);
if(_verbose && i > 0 && (i % 100000 == 0))
{
cout << "Thread " << _threadId << ": generated " << i << " UUIDs." << endl;
}
}
}
private:
int _threadId;
ItemSet& _itemSet;
GenerateFunc _func;
long _howMany;
bool _verbose;
};
struct GenerateUUID
{
string
operator()()
{
return generateUUID();
}
};
struct GenerateRandomString
{
string
operator()()
{
string s;
s.resize(21);
char buf[21];
IceUtilInternal::generateRandom(buf, sizeof(buf));
for(unsigned int i = 0; i < sizeof(buf); ++i)
{
s[i] = 33 + static_cast<unsigned char>(buf[i]) % (127 - 33); // We use ASCII 33-126 (from ! to ~, w/o space).
}
// cerr << s << endl;
return s;
}
};
struct GenerateRandomInt
{
public:
int
operator()()
{
return static_cast<int>(IceUtilInternal::random());
}
};
template<typename T, typename GenerateFunc> void
runTest(int threadCount, GenerateFunc func, long howMany, bool verbose, string name)
{
cout << "Generating " << howMany << " " << name << "s using " << threadCount << " thread";
if(threadCount > 1)
{
cout << "s";
}
cout << "... ";
if(verbose)
{
cout << endl;
}
else
{
cout << flush;
}
set<T> itemSet;
vector<ThreadControl> threads;
Time start = Time::now();
for(int i = 0; i < threadCount; i++)
{
ThreadPtr t = new InsertThread<T, GenerateFunc>(i, itemSet, func, howMany / threadCount, verbose);
threads.push_back(t->start());
}
for(vector<ThreadControl>::iterator p = threads.begin(); p != threads.end(); ++p)
{
p->join();
}
Time finish = Time::now();
cout << "ok" << endl;
if(verbose)
{
cout << "Each " << name << " took an average of "
<< (double) ((finish - start).toMicroSeconds()) / howMany
<< " micro seconds to generate and insert into a set."
<< endl;
}
}
class Client : public Test::TestHelper
{
public:
virtual void run(int argc, char* argv[]);
};
void
Client::run(int argc, char* argv[])
{
long howMany = 300000;
int threadCount = 3;
bool verbose = false;
if(argc > 3)
{
usage(argv[0]);
throw std::invalid_argument("too many arguments");
}
if(argc > 1)
{
howMany = atol(argv[1]);
if(howMany == 0)
{
usage(argv[0]);
throw invalid_argument("argv[1] howMany is not a number");
}
}
if(argc > 2)
{
threadCount = atoi(argv[2]);
if(threadCount <= 0)
{
usage(argv[0]);
throw invalid_argument("argv[2] threadCount is not a number");
}
verbose = true;
}
runTest<string, GenerateUUID>(threadCount, GenerateUUID(), howMany, verbose, "UUID");
runTest<string, GenerateRandomString>(threadCount, GenerateRandomString(), howMany, verbose, "string");
}
DEFINE_TEST(Client);
|