diff options
author | Bernard Normier <bernard@zeroc.com> | 2007-03-20 17:29:05 +0000 |
---|---|---|
committer | Bernard Normier <bernard@zeroc.com> | 2007-03-20 17:29:05 +0000 |
commit | df34cb828aa5be1945f4f51d99be5f33a9e4f65d (patch) | |
tree | 3dfd794e8398af65d28c10b6f60828ba41f2651d /cpp/demo/Freeze/customEvictor/Client.cpp | |
parent | Bug 2050. (diff) | |
download | ice-df34cb828aa5be1945f4f51d99be5f33a9e4f65d.tar.bz2 ice-df34cb828aa5be1945f4f51d99be5f33a9e4f65d.tar.xz ice-df34cb828aa5be1945f4f51d99be5f33a9e4f65d.zip |
New demo: IceUtil::Cache-based evictor
Diffstat (limited to 'cpp/demo/Freeze/customEvictor/Client.cpp')
-rw-r--r-- | cpp/demo/Freeze/customEvictor/Client.cpp | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/cpp/demo/Freeze/customEvictor/Client.cpp b/cpp/demo/Freeze/customEvictor/Client.cpp new file mode 100644 index 00000000000..ff795c08d54 --- /dev/null +++ b/cpp/demo/Freeze/customEvictor/Client.cpp @@ -0,0 +1,231 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2007 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 <Ice/Ice.h> +#include <IceUtil/IceUtil.h> +#include <Item.h> + +using namespace std; +using namespace Warehouse; + +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 StopWatch +{ +public: + + void + start() + { + _stopped = false; + _start = IceUtil::Time::now(); + } + + IceUtil::Time + stop() + { + if(!_stopped) + { + _stopped = true; + _stop = IceUtil::Time::now(); + } + + return _stop - _start; + } + +private: + + bool _stopped; + IceUtil::Time _start; + IceUtil::Time _stop; +}; + +class ReaderThread : public IceUtil::Thread +{ +public: + + ReaderThread(ItemPrx anItem) + : _anItem(anItem), + _requestsPerSecond(-1) + { + } + + virtual void run() + { + // + // Measures how long it takes to read 'count' items at random + // + const int count = 50000; + + StopWatch stopWatch; + stopWatch.start(); + + try + { + for(int i = 0; i < count; ++i) + { + int id = IceUtil::random(objectCount); + ostringstream os; + os << "P/N " << id; + string name = os.str(); + + Ice::Identity identity = { name, "" }; + ItemPrx item = ItemPrx::uncheckedCast(_anItem->ice_identity(identity)); + item->getDescription(); + } + _requestsPerSecond = static_cast<int>(count / stopWatch.stop().toSecondsDouble()); + } + catch(const IceUtil::Exception& e) + { + cerr << "Unexpected exception in ReaderThread: " << e << endl; + } + } + + int getRequestsPerSecond() const + { + return _requestsPerSecond; + } + +private: + + ItemPrx _anItem; + int _requestsPerSecond; +}; + +typedef IceUtil::Handle<ReaderThread> ReaderThreadPtr; + + +class WriterThread : public IceUtil::Thread +{ +public: + + WriterThread(ItemPrx anItem) + : _anItem(anItem), + _requestsPerSecond(-1) + { + } + + virtual void run() + { + // + // Measure how long it takes to write 'count' items at random + // + const int count = 5000; + + StopWatch stopWatch; + stopWatch.start(); + + try + { + for(int i = 0; i < count; ++i) + { + int id = IceUtil::random(objectCount); + + ostringstream os; + os << "P/N " << id; + string name = os.str(); + + Ice::Identity identity = { name, "" }; + ItemPrx item = ItemPrx::uncheckedCast(_anItem->ice_identity(identity)); + + item->adjustStock(1); + } + _requestsPerSecond = static_cast<int>(count / stopWatch.stop().toSecondsDouble()); + } + catch(const IceUtil::Exception& e) + { + cerr << "Unexpected exception in WriterThread: " << e << endl; + } + } + + int getRequestsPerSecond() const + { + return _requestsPerSecond; + } + +private: + + ItemPrx _anItem; + int _requestsPerSecond; +}; + +typedef IceUtil::Handle<WriterThread> WriterThreadPtr; + + +int +WarehouseClient::run(int argc, char* argv[]) +{ + // + // 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 << ": " << rt[i]->getRequestsPerSecond() + << " requests per second (" << 1000.0 / rpt << " ms per request)" << endl; + } + } + + return EXIT_SUCCESS; +} + |