File: use_benchmark_in_class_example.cpp

package info (click to toggle)
prometheus-cpp-lite 1.0%2Bgit20251202.48d09c4%2Bds-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 304 kB
  • sloc: cpp: 1,639; ansic: 123; makefile: 2
file content (59 lines) | stat: -rw-r--r-- 1,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <prometheus/registry.h>
#include <prometheus/benchmark.h>
#include <prometheus/text_serializer.h>

#include <array>
#include <chrono>
#include <cstdlib>
#include <memory>
#include <string>
#include <thread>
#include <iostream>

// use prometheus namespace
using namespace prometheus;

// create global registry for use it from our classes
static Registry globalRegistry;

class MyClass {

  Benchmark::Family& benchmarkFamily { Benchmark::Family::Build(globalRegistry,
                                         "benchmark_family", "family for check benchmark functionality") };

  Benchmark& benchmark1 { benchmarkFamily.Add({{"number", "1"}}) };
  Benchmark& benchmark2 { benchmarkFamily.Add({{"number", "2"}}) };

public:

  MyClass() = default;

  void member_to_do_something() {

    benchmark1.start();
    benchmark2.start();
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    benchmark1.stop();
    std::this_thread::sleep_for(std::chrono::milliseconds(500));
    benchmark2.stop();

  }

};

int main() {

  MyClass myClass;

  for (;; ) {

    std::this_thread::sleep_for(std::chrono::seconds(1));

    myClass.member_to_do_something();

    TextSerializer text_serializer;
    text_serializer.Serialize(std::cout, globalRegistry.Collect());

  }
}