summaryrefslogtreecommitdiff
path: root/cpp/demo/Freeze/customEvictor/Client.cpp
blob: 8a971efea1517836fc063881e7b138a557bbaf87 (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
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/IceUtil.h>
#include <Ice/Ice.h>
#include <Item.h>
#include <stdlib.h>

using namespace std;
using namespace Warehouse;

const int readCount = 15000;
const int writeCount = 1500;

const int objectCount = 10000;

class WarehouseClient : public Ice::Application
{
public:

    virtual int run(int, char*[]);
};

int
main(int argc, char* argv[])
{
    WarehouseClient app;
    return app.main(argc, argv, "config.client");
}

class ReaderThread : public IceUtil::Thread
{
public:

    ReaderThread(const ItemPrx& anItem) :
        _anItem(anItem),
        _requestsPerSecond(-1)
    {
    }

    virtual void run()
    {
        //
        // Measures how long it takes to read 'readCount' items at random
        //
        IceUtil::Time start = IceUtil::Time::now(IceUtil::Time::Monotonic);

        try
        {
            for(int i = 0; i < readCount; ++i)
            {
                int id = rand() % objectCount;
                ostringstream os;
                os << "P/N " << id;
                string name = os.str();

                Ice::Identity identity;
                identity.name = name;
                ItemPrx item = ItemPrx::uncheckedCast(_anItem->ice_identity(identity));
                item->getDescription();
            }
            _requestsPerSecond = 
                static_cast<int>(readCount / (IceUtil::Time::now(IceUtil::Time::Monotonic) - start).toSecondsDouble());
        }
        catch(const IceUtil::Exception& e)
        {
            cerr << "Unexpected exception in ReaderThread: " << e << endl;
        }   
    }

    int getRequestsPerSecond() const
    {
        return _requestsPerSecond;
    }

private:

    const ItemPrx _anItem;
    int _requestsPerSecond;
};
typedef IceUtil::Handle<ReaderThread> ReaderThreadPtr;

class WriterThread : public IceUtil::Thread
{
public:

    WriterThread(const ItemPrx& anItem) :
        _anItem(anItem),
        _requestsPerSecond(-1)
    {
    }

    virtual void run()
    {
        //
        // Measure how long it takes to write 'writeCount' items at random.
        //
        IceUtil::Time start = IceUtil::Time::now(IceUtil::Time::Monotonic);

        try
        {
            for(int i = 0; i < writeCount; ++i)
            {
                int id = rand() % objectCount;
                
                ostringstream os;
                os << "P/N " << id;
                string name = os.str();

                Ice::Identity identity;
                identity.name = name;
                ItemPrx item = ItemPrx::uncheckedCast(_anItem->ice_identity(identity));

                item->adjustStock(1);
            }
            _requestsPerSecond = 
                static_cast<int>(writeCount / (IceUtil::Time::now(IceUtil::Time::Monotonic) - start).toSecondsDouble());
        }
        catch(const IceUtil::Exception& e)
        {
            cerr << "Unexpected exception in WriterThread: " << e << endl;
        }   
    }

    int getRequestsPerSecond() const
    {
        return _requestsPerSecond;
    }

private:

    const ItemPrx _anItem;
    int _requestsPerSecond;
};
typedef IceUtil::Handle<WriterThread> WriterThreadPtr;

int
WarehouseClient::run(int argc, char*[])
{
    if(argc > 1)
    {
        cerr << appName() << ": too many arguments" << endl;
        return EXIT_FAILURE;
    }

    //
    // Initialize pseudo-random number generator; here, for fair comparisons,
    // it's useful to get the same sequence for each run.
    //
    srand(1);

    //
    // Retrieve a proxy to one item (any item will do).
    //
    ItemPrx anItem = ItemPrx::checkedCast(communicator()->propertyToProxy("Item.Proxy"));

    //
    // Start 1 writer and 5 readers.
    //
    WriterThreadPtr wt = new WriterThread(anItem);
    wt->start();

    const int readerCount = 5;

    ReaderThreadPtr rt[readerCount];
    int i;
    for(i = 0; i < readerCount; ++i)
    {
        rt[i] = new ReaderThread(anItem);
        rt[i]->start();
    }

    wt->getThreadControl().join();
    for(i = 0; i < readerCount; ++i)
    {
        rt[i]->getThreadControl().join(); 
    }

    //
    // Display results.
    //
    cout.precision(3);
    int rpt = wt->getRequestsPerSecond();
    if(rpt > 0)
    {
        cout << "Writer: " << rpt << " requests per second (" << 1000.0 / rpt << " ms per request)" << endl;
    }

    for(i = 0; i < readerCount; ++i)
    {
        rpt = rt[i]->getRequestsPerSecond();
        if(rpt > 0)
        {
            cout << "Reader " << i << ": " << rpt << " requests per second (" << 1000.0 / rpt
                 << " ms per request)" << endl;
        }
    }

    return EXIT_SUCCESS;
}