aboutsummaryrefslogtreecommitdiffstats
path: root/vbench/src/vbench/vbench/qps_analyzer.cpp
blob: 5307fee2fee7995ed244972ce2502825948efb7f (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.


#include "qps_analyzer.h"

namespace vbench {

QpsAnalyzer::QpsAnalyzer(Handler<Request> &next)
    : _next(next),
      _qps(0),
      _samples(0),
      _begin(0),
      _cnt(0)
{
}

void
QpsAnalyzer::handle(Request::UP request)
{
    if (request->status() == Request::STATUS_OK) {
        addEndTime(request->endTime());
    }
    _next.handle(std::move(request));
}

void
QpsAnalyzer::report()
{
    fprintf(stdout, "end qps: %g\n", _qps);
}

void
QpsAnalyzer::addEndTime(double end)
{
    ++_cnt;
    if (end < _begin) {
        _begin = end;
    }
    if ((end - _begin) > 5.0) {
        double newQps = ((double)_cnt) / (end - _begin);
        double factor = (_samples == 0) ? 1.0 : 0.75;
        _qps = (((1 - factor) * _qps) + (factor * newQps));
        ++_samples;
        _begin = end;
        _cnt = 0;
        fprintf(stderr, "qps: %g\n", _qps);
    }
}

} // namespace vbench