edit
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / demo.cpp
1 #include <chrono>
2 #include <iostream>
3 #include <thread>
4 #include <atomic>
5 #include <cstdlib>
6
7 #include "gdax-orderbook.hpp"
8
9 #define FACTOR 125
10 #define HIST_SIZE 60
11
12 std::atomic<bool> stop;
13
14 void printBestBidAndOffer(GDAXOrderBook & book)
15 {
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 << " ; "
20                                  << std::endl;
21 }
22
23 int main(int argc, char* argv[]) {
24     GDAXOrderBook book("ETH-USD");
25
26 //    printBestBidAndOffer(book);
27
28     size_t secondsToSleep = 30;
29     if (argc == 2) {
30         secondsToSleep = atoi(argv[1]);
31     }
32 /*
33     std::cout << "waiting " << secondsToSleep << " seconds for the market to "
34         "shift" << std::endl;
35     std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
36
37     printBestBidAndOffer(book);
38 */
39
40     size_t histogram[HIST_SIZE];
41     for ( size_t i = 0; i < HIST_SIZE; i++ )
42         histogram[i] = 0;
43
44     size_t numThreads = 5;
45     std::cout << "running for " << secondsToSleep << " seconds, with " <<
46         numThreads << " threads constantly iterating over the whole order "
47         "book." << std::endl;
48     bool keepIterating = true;
49     stop.store(false);
50     {
51         std::vector<std::future<void>> futures;
52         for (size_t i = 0 ; i < numThreads ; ++i)
53         {
54             futures.emplace_back(
55                 std::async(std::launch::async,
56                            [&book, &keepIterating, &histogram] ()
57             {
58                 GDAXOrderBook::ensureThreadAttached();
59
60                 GDAXOrderBook::bids_map_t::iterator bidIter;
61                 GDAXOrderBook::offers_map_t::iterator offerIter;
62                 std::chrono::steady_clock::time_point start, finish;
63
64                 while(keepIterating == true)
65                 {
66                     start = std::chrono::steady_clock::now();
67
68                     bidIter = book.bids.begin();
69                     while(bidIter != book.bids.end()) { ++bidIter; }
70
71                     offerIter = book.offers.begin();
72                     while(offerIter != book.offers.end()) { ++offerIter; }
73
74                     finish = std::chrono::steady_clock::now();
75
76                     int index =
77                         static_cast<size_t>(
78                             std::chrono::duration<double, std::milli>(
79                                 std::chrono::steady_clock::now() - start
80                             ).count()/FACTOR
81                         );
82
83                     if (!stop.load()) {
84                         if (index > HIST_SIZE)
85                             fprintf(stderr, "index overflow!\n");
86                         else
87                             histogram[index]++;
88                     }
89                 }
90             }));
91         }
92
93         std::this_thread::sleep_for(std::chrono::seconds(secondsToSleep));
94
95         keepIterating = false;
96         stop.store(true);
97     }
98
99     // find the largest histogram bucket so we can determine the scale factor
100     // for all of the buckets
101     double scaleFactor =
102         [&histogram]() {
103             size_t countOfBiggestBucket = 0;
104             for ( size_t & i : histogram )
105             {
106           countOfBiggestBucket = std::max(i, countOfBiggestBucket);
107             }
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 )
112     {
113         std::cout
114             << std::right << std::setw(5) << std::setfill(' ') << i*FACTOR / 1000.0
115             << "-"
116             << std::right << std::setw(5) << std::setfill(' ') << (i+1)*FACTOR / 1000.0 - 0.001
117             << " s: ";
118         std::cout << histogram[i] << "\n";
119 /*
120         for ( int j=0 ; j<histogram[i]/scaleFactor ; ++j )
121         {
122             std::cout << "*";
123         }
124         std::cout << std::endl;
125 */
126     }
127 }