summaryrefslogtreecommitdiff
path: root/cppe/test/IceE/uuid/Client.cpp
blob: 52199e95295d86696cf67d46c18bf876a05e0a14 (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceE/UUID.h>
#include <IceE/Time.h>
#include <IceE/Thread.h>
#include <IceE/StaticMutex.h>
#include <IceE/Monitor.h>
#include <TestApplication.h>
#include <set>
#include <vector>

using namespace IceUtil;
using namespace std;

static StaticMutex staticMutex = ICE_STATIC_MUTEX_INITIALIZER;

inline void usage(const char* myName)
{
    tprintf("Usage: %s [number of UUIDs to generate] [number of threads]\n", myName);
}

class CountedBarrier : public Shared, public Monitor<Mutex>
{
public:

    CountedBarrier(int count) :
      _count(count)
    {
    }

    void
    decrement()
    {
	Lock sync(*this);
	--_count;
	if(_count == 0)
	{
	    notifyAll();
	}
    }

    void
    waitZero()
    {
	Lock sync(*this);
	while(_count != 0)
	{
	    wait();
	}
    }

    bool
    isZero() const
    {
	Lock sync(*this);
	return _count == 0;
    }

private:

    int _count;
};
typedef Handle<CountedBarrier> CountedBarrierPtr;

class InsertThread : public Thread, public Mutex
{
public:
    
    InsertThread(const CountedBarrierPtr& start, const CountedBarrierPtr& stop, int threadId, set<string>& uuidSet, long howMany, bool verbose)
	: _start(start), _stop(stop), _threadId(threadId), _uuidSet(uuidSet), _howMany(howMany), _verbose(verbose), _destroyed(false)
    {
    }

    virtual void
    run()
    {
	_start->decrement();
	_start->waitZero();

	for(long i = 0; i < _howMany && !destroyed(); i++)
	{
	    string uuid = generateUUID();
	    {
		StaticMutex::Lock lock(staticMutex);
		pair<set<string>::iterator, bool> ok = _uuidSet.insert(uuid);
		if(!ok.second)
		{
		    tprintf("******* iteration %d\n", i);
		    tprintf("******* Duplicate UUID: %s\n", (*ok.first).c_str());
		}

		test(ok.second);
	    }

#ifdef _WIN32_WCE
	    if(i > 0 && (i % 100) == 0)
	    {
		tprintf(".");
	    }
#else
	    if(_verbose && i > 0 && (i % 100000 == 0))
	    {
		tprintf("Thread %d: generated %d UUIDs.\n", _threadId, i);
	    }
#endif
	}

	_stop->decrement();
#ifdef _WIN32_WCE
	// This will cause the main thread to wake.
	tprintf(".");
#endif
    }

    void
    destroy()
    {
	Lock sync(*this);
	_destroyed = true;
    }

    bool
    destroyed() const
    {
	Lock sync(*this);
	return _destroyed;
    }
 private:

    const CountedBarrierPtr _start;
    const CountedBarrierPtr _stop;
    const int _threadId;
    set<string>& _uuidSet;
    const long _howMany;
    const bool _verbose;
    bool _destroyed;
};
typedef Handle<InsertThread> InsertThreadPtr;

class UuidTestApplication : public TestApplication
{
public:

    UuidTestApplication() :
        TestApplication("uuid test")
    {
    }

    virtual int
    run(int argc, char* argv[])
    {
#ifdef _WIN32_WCE
        long howMany = 3000;
#else
        long howMany = 300000;
#endif
        int threadCount = 3;
        bool verbose = false;

        if(argc > 3)
        {
	    usage(argv[0]);
	    return EXIT_FAILURE;
        }
        else if(argc == 3)
        {
	    howMany = atol(argv[1]);
	    if (howMany == 0)
	    {
	        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;
	    }
        }

	tprintf("Generating %d UUIDs", howMany);
        tprintf("... ");
    
        if(verbose)
        {
	    tprintf("\n");
        }
	//
	// First measure raw time to produce UUIDs.
	//
        Time startTime = Time::now();
	long i;
	for(i = 0; i < howMany; i++)
	{
    	    generateUUID();
	}
#ifdef _WIN32_WCE
	if(terminated())
	{
	    return EXIT_SUCCESS;
	}
#endif

        Time finish = Time::now();

        tprintf("ok\n");

#ifndef _WIN32_WCE
        if(verbose)
#endif
        {
            tprintf("Each UUID took an average of %.04f ms to generate and insert into a set<string>.\n",
		    ((double) ((finish - startTime).toMilliSeconds())) / howMany);
        }

        tprintf("Generating %d UUIDs using %d thread", howMany, threadCount);
        if(threadCount > 1)
        {
	    tprintf("s");
        }
        tprintf("... ");
    
        if(verbose)
        {
	    tprintf("\n");
        }

	set<string> uuidSet;
        
	startTime = Time::now();
	vector<InsertThreadPtr> threads;

	CountedBarrierPtr stop = new CountedBarrier(threadCount);
	CountedBarrierPtr start = new CountedBarrier(threadCount);
        for(i = 0; i < threadCount; i++)
        {
	    InsertThreadPtr t = new InsertThread(start, stop, i, uuidSet, howMany / threadCount, verbose); 
	    t->start();
	    threads.push_back(t);
        }

        vector<InsertThreadPtr>::iterator p;

#ifdef _WIN32_WCE
	while(!stop->isZero() && !terminated())
	{
	    MSG Msg;
	    if(GetMessage(&Msg, NULL, 0, 0))
	    {
		//
		// Process all pending events.
		//
		do
		{
		    TranslateMessage(&Msg);
		    DispatchMessage(&Msg);
		}
		while(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE));
	    }
	}
	
	//
	// If the user terminated the app, the destroy all the
	// threads. This loop will end once all the threads have gone.
	//
	if(terminated())
	{
	    for(p = threads.begin(); p != threads.end(); ++p)
	    {
		(*p)->destroy();
	    }
	}
#endif
	stop->waitZero();
        finish = Time::now();

	for(p = threads.begin(); p != threads.end(); ++p)
        {
	    (*p)->getThreadControl().join();
        }

        tprintf("ok\n");

#ifndef _WIN32_WCE
        if(verbose)
#endif
        {
            tprintf("Each UUID took an average of %.04f ms to generate and insert into a set<string>.\n",
		    ((double) ((finish - startTime).toMilliSeconds())) / howMany);
        }

        return EXIT_SUCCESS;
    }
};

#ifdef _WIN32_WCE

int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    UuidTestApplication app;
    return app.main(hInstance);
}

#else

int
main(int argc, char** argv)
{
    UuidTestApplication app;
    return app.main(argc, argv);
}

#endif