X-Git-Url: http://plrg.eecs.uci.edu/git/?p=c11concurrency-benchmarks.git;a=blobdiff_plain;f=gdax-orderbook-hpp%2Fdemo%2Fdemo.cpp;h=bcef9f2284603f5f4c4808b7cf30b65224434dc1;hp=4d3dc61df03613cb6e94a646de9f1938fba2fcb6;hb=80b9bd33985bec36b89fca167c7bc87e7e4e8995;hpb=1491fc3840762ae57661f247ae363be83e0132a2 diff --git a/gdax-orderbook-hpp/demo/demo.cpp b/gdax-orderbook-hpp/demo/demo.cpp index 4d3dc61..bcef9f2 100644 --- a/gdax-orderbook-hpp/demo/demo.cpp +++ b/gdax-orderbook-hpp/demo/demo.cpp @@ -1,9 +1,16 @@ #include #include #include +#include +#include #include "gdax-orderbook.hpp" +#define FACTOR 125 +#define HIST_SIZE 60 + +std::atomic stop; + void printBestBidAndOffer(GDAXOrderBook & book) { std::cout << "current best bid: " << book.bids.begin()->second << " @ $" @@ -16,24 +23,30 @@ void printBestBidAndOffer(GDAXOrderBook & book) int main(int argc, char* argv[]) { GDAXOrderBook book("ETH-USD"); - printBestBidAndOffer(book); +// printBestBidAndOffer(book); - size_t secondsToSleep = 5; + size_t secondsToSleep = 30; + if (argc == 2) { + secondsToSleep = atoi(argv[1]); + } +/* std::cout << "waiting " << secondsToSleep << " seconds for the market to " "shift" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep)); printBestBidAndOffer(book); +*/ - size_t histogram[30]; - for ( auto bucket : histogram ) bucket = 0; + size_t histogram[HIST_SIZE]; + for ( size_t i = 0; i < HIST_SIZE; i++ ) + histogram[i] = 0; size_t numThreads = 5; - secondsToSleep = 10; std::cout << "running for " << secondsToSleep << " seconds, with " << numThreads << " threads constantly iterating over the whole order " "book." << std::endl; bool keepIterating = true; + stop.store(false); { std::vector> futures; for (size_t i = 0 ; i < numThreads ; ++i) @@ -60,12 +73,19 @@ int main(int argc, char* argv[]) { finish = std::chrono::steady_clock::now(); - int index = static_cast( + int index = + static_cast( std::chrono::duration( std::chrono::steady_clock::now() - start - ).count()/500); - - histogram[index]++; + ).count()/FACTOR + ); + + if (!stop.load()) { + if (index > HIST_SIZE) + fprintf(stderr, "index overflow!\n"); + else + histogram[index]++; + } } })); } @@ -73,6 +93,7 @@ int main(int argc, char* argv[]) { std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep)); keepIterating = false; + stop.store(true); } // find the largest histogram bucket so we can determine the scale factor @@ -82,22 +103,25 @@ int main(int argc, char* argv[]) { size_t countOfBiggestBucket = 0; for ( size_t & i : histogram ) { - countOfBiggestBucket = std::max(i, countOfBiggestBucket); + countOfBiggestBucket = std::max(i, countOfBiggestBucket); } return (double)countOfBiggestBucket; }()/68.0; // 80 column display, minus chars used for row headers, =68 std::cout << "histogram of times to iterate over the whole book:" << std::endl; - for ( int i=0 ; i < sizeof(histogram)/sizeof(histogram[0]) ; ++i ) + for ( int i=0 ; i < HIST_SIZE ; ++i ) { std::cout - << std::right << std::setw(3) << std::setfill(' ') << i*5 + << std::right << std::setw(5) << std::setfill(' ') << i*FACTOR / 1000.0 << "-" - << std::right << std::setw(3) << std::setfill(' ') << (i+1)*5-1 - << " ms: "; + << std::right << std::setw(5) << std::setfill(' ') << (i+1)*FACTOR / 1000.0 - 0.001 + << " s: "; + std::cout << histogram[i] << "\n"; +/* for ( int j=0 ; j