7 #include "gdax-orderbook.hpp"
12 std::atomic<bool> stop;
14 void printBestBidAndOffer(GDAXOrderBook & book)
16 std::cout << "current best bid: " << book.bids.begin()->second << " @ $"
17 << book.bids.begin()->first/100.0 << " ; ";
18 std::cout << "current best offer: " << book.offers.begin()->second << " @ $"
19 << book.offers.begin()->first/100.0 << " ; "
23 int main(int argc, char* argv[]) {
24 GDAXOrderBook book("ETH-USD");
26 // printBestBidAndOffer(book);
28 size_t secondsToSleep = 30;
30 secondsToSleep = atoi(argv[1]);
33 std::cout << "waiting " << secondsToSleep << " seconds for the market to "
35 std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
37 printBestBidAndOffer(book);
40 size_t histogram[HIST_SIZE];
41 for ( size_t i = 0; i < HIST_SIZE; i++ )
44 size_t numThreads = 5;
45 std::cout << "running for " << secondsToSleep << " seconds, with " <<
46 numThreads << " threads constantly iterating over the whole order "
48 bool keepIterating = true;
51 std::vector<std::future<void>> futures;
52 for (size_t i = 0 ; i < numThreads ; ++i)
55 std::async(std::launch::async,
56 [&book, &keepIterating, &histogram] ()
58 GDAXOrderBook::ensureThreadAttached();
60 GDAXOrderBook::bids_map_t::iterator bidIter;
61 GDAXOrderBook::offers_map_t::iterator offerIter;
62 std::chrono::steady_clock::time_point start, finish;
64 while(keepIterating == true)
66 start = std::chrono::steady_clock::now();
68 bidIter = book.bids.begin();
69 while(bidIter != book.bids.end()) { ++bidIter; }
71 offerIter = book.offers.begin();
72 while(offerIter != book.offers.end()) { ++offerIter; }
74 finish = std::chrono::steady_clock::now();
78 std::chrono::duration<double, std::milli>(
79 std::chrono::steady_clock::now() - start
84 if (index > HIST_SIZE)
85 fprintf(stderr, "index overflow!\n");
93 std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
95 keepIterating = false;
99 // find the largest histogram bucket so we can determine the scale factor
100 // for all of the buckets
103 size_t countOfBiggestBucket = 0;
104 for ( size_t & i : histogram )
106 countOfBiggestBucket = std::max(i, countOfBiggestBucket);
108 return (double)countOfBiggestBucket;
109 }()/68.0; // 80 column display, minus chars used for row headers, =68
110 std::cout << "histogram of times to iterate over the whole book:" << std::endl;
111 for ( int i=0 ; i < HIST_SIZE ; ++i )
114 << std::right << std::setw(5) << std::setfill(' ') << i*FACTOR / 1000.0
116 << std::right << std::setw(5) << std::setfill(' ') << (i+1)*FACTOR / 1000.0 - 0.001
118 std::cout << histogram[i] << "\n";
120 for ( int j=0 ; j<histogram[i]/scaleFactor ; ++j )
124 std::cout << std::endl;