Fix bugs in original code
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / demo.cpp
1 #include <chrono>
2 #include <iostream>
3 #include <thread>
4
5 #include "gdax-orderbook.hpp"
6
7 void printBestBidAndOffer(GDAXOrderBook & book)
8 {
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 << " ; "
13                                  << std::endl;
14 }
15
16 int main(int argc, char* argv[]) {
17     GDAXOrderBook book("ETH-USD");
18
19     printBestBidAndOffer(book);
20
21     size_t secondsToSleep = 5;
22     std::cout << "waiting " << secondsToSleep << " seconds for the market to "
23         "shift" << std::endl;
24     std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
25
26     printBestBidAndOffer(book);
27
28     size_t histogram[30];
29     for ( auto bucket : histogram ) bucket = 0;
30
31     size_t numThreads = 5;
32     secondsToSleep = 10;
33     std::cout << "running for " << secondsToSleep << " seconds, with " <<
34         numThreads << " threads constantly iterating over the whole order "
35         "book." << std::endl;
36     bool keepIterating = true;
37     {
38         std::vector<std::future<void>> futures;
39         for (size_t i = 0 ; i < numThreads ; ++i)
40         {
41             futures.emplace_back(
42                 std::async(std::launch::async,
43                            [&book, &keepIterating, &histogram] ()
44             {
45                 GDAXOrderBook::ensureThreadAttached();
46
47                 GDAXOrderBook::bids_map_t::iterator bidIter;
48                 GDAXOrderBook::offers_map_t::iterator offerIter;
49                 std::chrono::steady_clock::time_point start, finish;
50
51                 while(keepIterating == true)
52                 {
53                     start = std::chrono::steady_clock::now();
54
55                     bidIter = book.bids.begin();
56                     while(bidIter != book.bids.end()) { ++bidIter; }
57
58                     offerIter = book.offers.begin();
59                     while(offerIter != book.offers.end()) { ++offerIter; }
60
61                     finish = std::chrono::steady_clock::now();
62
63                     int index =                         static_cast<size_t>(
64                             std::chrono::duration<double, std::milli>(
65                                 std::chrono::steady_clock::now() - start
66                                                                       ).count()/500);
67
68                             histogram[index]++;
69                 }
70             }));
71         }
72
73         std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
74
75         keepIterating = false;
76     }
77
78     // find the largest histogram bucket so we can determine the scale factor
79     // for all of the buckets
80     double scaleFactor =
81         [&histogram]() {
82             size_t countOfBiggestBucket = 0;
83             for ( size_t & i : histogram )
84             {
85               countOfBiggestBucket = std::max(i, countOfBiggestBucket);
86             }
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 )
91     {
92         std::cout
93             << std::right << std::setw(3) << std::setfill(' ') << i*5
94             << "-"
95             << std::right << std::setw(3) << std::setfill(' ') << (i+1)*5-1
96             << " ms: ";
97         for ( int j=0 ; j<histogram[i]/scaleFactor ; ++j )
98         {
99             std::cout << "*";
100         }
101         std::cout << std::endl;
102     }
103 }