edits
[c11concurrency-benchmarks.git] / gdax-orderbook-hpp / demo / demo.cpp
index 4d3dc61df03613cb6e94a646de9f1938fba2fcb6..bcef9f2284603f5f4c4808b7cf30b65224434dc1 100644 (file)
@@ -1,9 +1,16 @@
 #include <chrono>
 #include <iostream>
 #include <thread>
+#include <atomic>
+#include <cstdlib>
 
 #include "gdax-orderbook.hpp"
 
+#define FACTOR 125
+#define HIST_SIZE 60
+
+std::atomic<bool> 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<std::future<void>> 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<size_t>(
+                    int index =
+                        static_cast<size_t>(
                             std::chrono::duration<double, std::milli>(
                                 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<histogram[i]/scaleFactor ; ++j )
         {
             std::cout << "*";
         }
         std::cout << std::endl;
+*/
     }
 }