File: ola-latency.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (161 lines) | stat: -rw-r--r-- 4,260 bytes parent folder | download | duplicates (3)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * ola-latency.cpp
 * Call FetchDmx and track the latency for each call.
 * Copyright (C) 2005 Simon Newton
 */

#include <stdlib.h>
#include <ola/Callback.h>
#include <ola/Clock.h>
#include <ola/DmxBuffer.h>
#include <ola/Logging.h>
#include <ola/OlaClientWrapper.h>
#include <ola/base/Flags.h>
#include <ola/base/Init.h>
#include <ola/thread/SignalThread.h>

#include <iostream>
#include <string>

using ola::DmxBuffer;
using ola::NewSingleCallback;
using ola::OlaCallbackClientWrapper;
using ola::TimeStamp;
using ola::TimeInterval;
using std::cout;
using std::endl;
using std::string;

DEFINE_s_uint32(universe, u, 1, "The universe to receive data for");
DEFINE_default_bool(send_dmx, false, "Use SendDmx messages, default is GetDmx");
DEFINE_s_uint32(count, c, 0,
    "Exit after this many RPCs, default: infinite (0)");

class Tracker {
 public:
    Tracker()
        : m_count(0),
          m_sum(0) {
      m_buffer.Blackout();
    }

    bool Setup();
    void Start();

    void GotDmx(const DmxBuffer &data, const string &error);
    void SendComplete(const string &error);

 private:
    uint32_t m_count;
    uint64_t m_sum;
    TimeInterval m_max;
    ola::DmxBuffer m_buffer;
    OlaCallbackClientWrapper m_wrapper;
    ola::Clock m_clock;
    ola::thread::SignalThread m_signal_thread;
    TimeStamp m_send_time;

    void SendRequest();
    void LogTime();
    void StartSignalThread();
};

bool Tracker::Setup() {
  return m_wrapper.Setup();
}

void Tracker::Start() {
  ola::io::SelectServer *ss = m_wrapper.GetSelectServer();
  m_signal_thread.InstallSignalHandler(
      SIGINT,
      ola::NewCallback(ss, &ola::io::SelectServer::Terminate));
  m_signal_thread.InstallSignalHandler(
      SIGTERM,
      ola::NewCallback(ss, &ola::io::SelectServer::Terminate));
  SendRequest();

  ss->Execute(ola::NewSingleCallback(this, &Tracker::StartSignalThread));
  ss->Run();

  // Print this via cout to ensure we actually get some output by default
  // It also means you can just see the stats and not each individual request
  // if you want.
  cout << "--------------" << endl;
  cout << "Sent " << m_count << " RPCs" << endl;
  cout << "Max was " << m_max.MicroSeconds() << " microseconds" << endl;
  cout << "Mean " << m_sum / m_count << " microseconds" << endl;
}

void Tracker::GotDmx(const DmxBuffer &, const string &) {
  LogTime();
}

void Tracker::SendComplete(const string &) {
  LogTime();
}

void Tracker::SendRequest() {
  m_clock.CurrentMonotonicTime(&m_send_time);
  if (FLAGS_send_dmx) {
    m_wrapper.GetClient()->SendDmx(
        FLAGS_universe,
        m_buffer,
        NewSingleCallback(this, &Tracker::SendComplete));

  } else {
    m_wrapper.GetClient()->FetchDmx(
        FLAGS_universe,
        NewSingleCallback(this, &Tracker::GotDmx));
  }
}

void Tracker::LogTime() {
  TimeStamp now;
  m_clock.CurrentMonotonicTime(&now);
  TimeInterval delta = now - m_send_time;
  if (delta > m_max) {
    m_max = delta;
  }
  m_sum += delta.MicroSeconds();

  OLA_INFO << "RPC took " << delta;
  if (FLAGS_count == ++m_count) {
    m_wrapper.GetSelectServer()->Terminate();
  } else {
    SendRequest();
  }
}

void Tracker::StartSignalThread() {
  if (!m_signal_thread.Start()) {
    m_wrapper.GetSelectServer()->Terminate();
  }
}

int main(int argc, char *argv[]) {
  ola::AppInit(&argc, argv, "[options]",
               "Measure the latency of RPCs to olad.");

  Tracker tracker;
  if (!tracker.Setup()) {
    OLA_FATAL << "Setup failed";
    exit(1);
  }

  tracker.Start();
  return 0;
}