OnlineTimer Class Reference

class OnlineTimer is used to test real-time decoding algorithms and evaluate how long the decoding of a particular utterance would take. More...

#include <online-timing.h>

Collaboration diagram for OnlineTimer:

Public Member Functions

 OnlineTimer (const std::string &utterance_id)
 
void SleepUntil (double cur_utterance_length)
 The call to SleepUntil(t) will sleep until cur_utterance_length seconds after this object was initialized, or return immediately if we've already passed that time. More...
 
void WaitUntil (double cur_utterance_length)
 The call to WaitUntil(t) simulates the effect of sleeping until cur_utterance_length seconds after this object was initialized; but instead of actually sleeping, it increases a counter. More...
 
void OutputStats (OnlineTimingStats *stats)
 This call, which should be made after decoding is done, writes the stats to the object that accumulates them. More...
 
double Elapsed ()
 Returns the simulated time elapsed in seconds since the timer was started; this equals waited_ plus the real time elapsed. More...
 

Private Attributes

std::string utterance_id_
 
Timer timer_
 
double waited_
 
double utterance_length_
 

Detailed Description

class OnlineTimer is used to test real-time decoding algorithms and evaluate how long the decoding of a particular utterance would take.

The 'obvious' way to evaluate this would be to measure the wall-clock time, and if we're processing the data in chunks, to sleep() until a given chunk would become available in a real-time application– e.g. say we need to process a chunk that ends half a second into the utterance, we would sleep until half a second had elapsed since the start of the utterance. In this code we have the option to not actually sleep: we can simulate the effect of sleeping by just incrementing a variable that says how long we would have slept; and we add this to wall-clock times obtained from Timer::Elapsed(). The usage of this class will be something like as follows:

OnlineTimingStats stats;
while (.. process different utterances..) {
OnlineTimer this_utt_timer(utterance_id);
while (...process chunks of this utterance..) {
double num_secs_elapsed = 0.01 * num_frames;
this_utt_timer.WaitUntil(num_secs_elapsed);
}
this_utt_timer.OutputStats(&stats);

This assumes that the value provided to the last WaitUntil() call was the length of the utterance.

Definition at line 88 of file online-timing.h.

Constructor & Destructor Documentation

◆ OnlineTimer()

OnlineTimer ( const std::string &  utterance_id)

Definition at line 62 of file online-timing.cc.

62  :
63  utterance_id_(utterance_id), waited_(0.0), utterance_length_(0.0) { }
std::string utterance_id_

Member Function Documentation

◆ Elapsed()

double Elapsed ( )

Returns the simulated time elapsed in seconds since the timer was started; this equals waited_ plus the real time elapsed.

Definition at line 92 of file online-timing.cc.

References Timer::Elapsed(), OnlineTimer::timer_, and OnlineTimer::waited_.

92  {
93  return timer_.Elapsed() + waited_;
94 }
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74

◆ OutputStats()

void OutputStats ( OnlineTimingStats stats)

This call, which should be made after decoding is done, writes the stats to the object that accumulates them.

Definition at line 96 of file online-timing.cc.

References Timer::Elapsed(), KALDI_VLOG, KALDI_WARN, OnlineTimingStats::max_delay_, OnlineTimingStats::max_delay_utt_, OnlineTimingStats::num_utts_, OnlineTimer::timer_, OnlineTimingStats::total_audio_, OnlineTimingStats::total_time_taken_, OnlineTimingStats::total_time_waited_, OnlineTimer::utterance_id_, OnlineTimer::utterance_length_, and OnlineTimer::waited_.

Referenced by main().

96  {
97  double processing_time = timer_.Elapsed() + waited_,
98  wait_time = processing_time - utterance_length_;
99  if (wait_time < 0.0) {
100  // My first though was to make this a KALDI_ERR, but perhaps
101  // clocks can go backwards under some weird circumstance, so
102  // let's just make it a warning.
103  KALDI_WARN << "Negative wait time " << wait_time
104  << " does not make sense.";
105  }
106  KALDI_VLOG(2) << "Latency " << wait_time << " seconds out of "
107  << utterance_length_ << ", for utterance "
108  << utterance_id_;
109 
110  stats->num_utts_++;
111  stats->total_audio_ += utterance_length_;
112  stats->total_time_taken_ += processing_time;
113  stats->total_time_waited_ += waited_;
114  if (wait_time > stats->max_delay_) {
115  stats->max_delay_ = wait_time;
116  stats->max_delay_utt_ = utterance_id_;
117  }
118 }
std::string utterance_id_
#define KALDI_WARN
Definition: kaldi-error.h:150
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74

◆ SleepUntil()

void SleepUntil ( double  cur_utterance_length)

The call to SleepUntil(t) will sleep until cur_utterance_length seconds after this object was initialized, or return immediately if we've already passed that time.

Definition at line 81 of file online-timing.cc.

References Timer::Elapsed(), KALDI_ASSERT, kaldi::Sleep(), OnlineTimer::timer_, OnlineTimer::utterance_length_, and OnlineTimer::waited_.

Referenced by main().

81  {
82  KALDI_ASSERT(waited_ == 0 && "Do not mix SleepUntil with WaitUntil.");
83  double elapsed = timer_.Elapsed();
84 
85  double to_wait = cur_utterance_length - elapsed;
86  if (to_wait > 0.0) {
87  Sleep(to_wait);
88  }
89  utterance_length_ = cur_utterance_length;
90 }
void Sleep(float seconds)
Definition: kaldi-utils.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74

◆ WaitUntil()

void WaitUntil ( double  cur_utterance_length)

The call to WaitUntil(t) simulates the effect of sleeping until cur_utterance_length seconds after this object was initialized; but instead of actually sleeping, it increases a counter.

Definition at line 65 of file online-timing.cc.

References Timer::Elapsed(), OnlineTimer::timer_, OnlineTimer::utterance_length_, and OnlineTimer::waited_.

Referenced by main().

65  {
66  double elapsed = timer_.Elapsed();
67  // it's been cur_utterance_length seconds since we would have
68  // started processing this utterance, in a real-time decoding
69  // scenario. We've been actually processing it for "elapsed"
70  // seconds, plus we would have been waiting on some kind of
71  // semaphore for waited_ seconds. If we have to wait further
72  // at this point, increase "waited_".
73  // (I have to think of a better way of explaining this).
74  double to_wait = cur_utterance_length - (elapsed + waited_);
75  if (to_wait > 0.0)
76  waited_ += to_wait;
77 
78  utterance_length_ = cur_utterance_length;
79 }
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74

Member Data Documentation

◆ timer_

◆ utterance_id_

std::string utterance_id_
private

Definition at line 111 of file online-timing.h.

Referenced by OnlineTimer::OutputStats().

◆ utterance_length_

double utterance_length_
private

◆ waited_

double waited_
private

The documentation for this class was generated from the following files: