5 #include "gdax-orderbook.hpp"
7 void printBestBidAndOffer(GDAXOrderBook & book)
9 std::cout << "current best bid: " << book.bids.begin()->second << " @ $"
10 << book.bids.begin()->first/100.0 << " ; ";
11 std::cout << "current best offer: " << book.offers.begin()->second << " @ $"
12 << book.offers.begin()->first/100.0 << " ; "
16 int main(int argc, char* argv[]) {
17 GDAXOrderBook book("ETH-USD");
19 printBestBidAndOffer(book);
21 size_t secondsToSleep = 5;
22 std::cout << "waiting " << secondsToSleep << " seconds for the market to "
24 std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
26 printBestBidAndOffer(book);
29 for ( auto bucket : histogram ) bucket = 0;
31 size_t numThreads = 5;
33 std::cout << "running for " << secondsToSleep << " seconds, with " <<
34 numThreads << " threads constantly iterating over the whole order "
36 bool keepIterating = true;
38 std::vector<std::future<void>> futures;
39 for (size_t i = 0 ; i < numThreads ; ++i)
42 std::async(std::launch::async,
43 [&book, &keepIterating, &histogram] ()
45 GDAXOrderBook::ensureThreadAttached();
47 GDAXOrderBook::bids_map_t::iterator bidIter;
48 GDAXOrderBook::offers_map_t::iterator offerIter;
49 std::chrono::steady_clock::time_point start, finish;
51 while(keepIterating == true)
53 start = std::chrono::steady_clock::now();
55 bidIter = book.bids.begin();
56 while(bidIter != book.bids.end()) { ++bidIter; }
58 offerIter = book.offers.begin();
59 while(offerIter != book.offers.end()) { ++offerIter; }
61 finish = std::chrono::steady_clock::now();
63 int index = static_cast<size_t>(
64 std::chrono::duration<double, std::milli>(
65 std::chrono::steady_clock::now() - start
73 std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
75 keepIterating = false;
78 // find the largest histogram bucket so we can determine the scale factor
79 // for all of the buckets
82 size_t countOfBiggestBucket = 0;
83 for ( size_t & i : histogram )
85 countOfBiggestBucket = std::max(i, countOfBiggestBucket);
87 return (double)countOfBiggestBucket;
88 }()/68.0; // 80 column display, minus chars used for row headers, =68
89 std::cout << "histogram of times to iterate over the whole book:" << std::endl;
90 for ( int i=0 ; i < sizeof(histogram)/sizeof(histogram[0]) ; ++i )
93 << std::right << std::setw(3) << std::setfill(' ') << i*5
95 << std::right << std::setw(3) << std::setfill(' ') << (i+1)*5-1
97 for ( int j=0 ; j<histogram[i]/scaleFactor ; ++j )
101 std::cout << std::endl;