JayBeams  0.1
Another project to have fun coding.
book_depth_statistics.cpp
Go to the documentation of this file.
2 
3 #include <jb/as_hhmmss.hpp>
4 #include <jb/log.hpp>
5 
6 #include <iostream>
7 #include <limits>
8 
9 namespace {
10 
11 template <typename book_depth_histogram_t>
12 void csv_rate(std::ostream& os, book_depth_histogram_t const& histo) {
13  os << histo.observed_min() << "," << histo.estimated_quantile(0.25) << ","
14  << histo.estimated_quantile(0.50) << "," << histo.estimated_quantile(0.75)
15  << "," << histo.estimated_quantile(0.90) << ","
16  << histo.estimated_quantile(0.99) << "," << histo.estimated_quantile(0.999)
17  << "," << histo.estimated_quantile(0.9999) << "," << histo.observed_max();
18 }
19 
20 template <typename book_depth_histogram_t>
21 void report_rate(
22  std::chrono::nanoseconds ts, book_depth_histogram_t const& histo) {
23  JB_LOG(info) << ": " << jb::as_hhmmss(ts)
24  << ", NSamples =" << histo.nsamples()
25  << ", min=" << histo.observed_min()
26  << ", p25=" << histo.estimated_quantile(0.25)
27  << ", p50=" << histo.estimated_quantile(0.50)
28  << ", p75=" << histo.estimated_quantile(0.75)
29  << ", p90=" << histo.estimated_quantile(0.90)
30  << ", p99=" << histo.estimated_quantile(0.99)
31  << ", p99.9=" << histo.estimated_quantile(0.999)
32  << ", p99.99=" << histo.estimated_quantile(0.9999)
33  << ", max=" << histo.observed_max();
34 }
35 
36 } // anonymous namespace
37 
39  : book_depth_(
40  book_depth_histogram_t::binning_strategy(0, cfg.max_book_depth())) {
41 }
42 
44  os << "Name,NSamples"
45  << ",minBookDepth,p25BookDepth,p50BookDepth,p75BookDepth"
46  << ",p90BookDepth,p99BookDepth,p999BookDepth,p9999BookDepth"
47  << ",maxBookDepth"
48  << "\n";
49 }
50 
52  std::string const& name, std::ostream& os) const {
53  if (book_depth_.nsamples() == 0) {
54  os << name << ",0";
55  os << ",,,,,,,,,"; // book depth
56  os << "\n";
57  return;
58  }
59  os << name << "," << book_depth_.nsamples() << ",";
60  csv_rate(os, book_depth_);
61  os << std::endl;
62 }
63 
64 namespace jb {
65 namespace defaults {
66 
67 #ifndef JB_BOOK_DEPTH_STATS_DEFAULTS_max_book_depth
68 #define JB_BOOK_DEPTH_STATS_DEFAULTS_max_book_depth 8192
69 #endif
70 
72 
73 } // namespace defaults
74 } // namespace jb
75 
78  desc("max-book-depth")
79  .help(
80  "Configure the book_depth histogram to expect"
81  " no more than this many values"
82  " Higher values consume more memory, but give more accurate"
83  " results for high percentiles."),
84  this, defaults::max_book_depth) {
85 }
86 
88  if (max_book_depth() <= 1) {
89  std::ostringstream os;
90  os << "max_book_depth must be > 1, value=" << max_book_depth();
91  throw jb::usage(os.str(), 1);
92  }
93 }
book_depth_t max_book_depth
Define defaults for program parameters.
static void print_csv_header(std::ostream &os)
Print a CSV header.
#define JB_BOOK_DEPTH_STATS_DEFAULTS_max_book_depth
Helper class to print time durations s in HHMMSS format.
Definition: as_hhmmss.hpp:35
void print_csv(std::string const &name, std::ostream &os) const
Print all the measurements in CSV format.
book_depth_histogram_t book_depth_
jb::config_attribute< config, book_depth_t > max_book_depth
No more than this value is recorded.
Configure a book_depth_statistics object.
A simple class to communicate the result of parsing the options.
Definition: usage.hpp:11
unsigned long int book_depth_t
std::uint64_t nsamples() const
Return the number of samples observed to this point.
Definition: histogram.hpp:79
#define JB_LOG(lvl)
Definition: log.hpp:70
void validate() const override
Validate the configuration.
book_depth_statistics(config const &cfg)
Constructor.
The top-level namespace for the JayBeams library.
Definition: as_hhmmss.hpp:7