Cutelyst  2.13.0
stats.cpp
1 /*
2  * Copyright (C) 2015-2017 Daniel Nicoletti <dantti12@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 #include "stats_p.h"
19 
20 #include "dispatchtype.h"
21 #include "application.h"
22 #include "enginerequest.h"
23 #include "utils.h"
24 
25 #include "common.h"
26 
27 #include <QtCore/QStringList>
28 
29 using namespace Cutelyst;
30 
31 Stats::Stats(EngineRequest *request) : d_ptr(new StatsPrivate)
32 {
33  Q_D(Stats);
34  d->engineRequest = request;
35 }
36 
37 Stats::~Stats()
38 {
39  delete d_ptr;
40 }
41 
42 void Stats::profileStart(const QString &action)
43 {
44  Q_D(Stats);
45  StatsAction stat;
46  stat.action = action;
47  stat.begin = d->engineRequest->elapsed.nsecsElapsed();
48  d->actions.push_back(stat);
49 }
50 
51 void Stats::profileEnd(const QString &action)
52 {
53  Q_D(Stats);
54  for (auto &stat : d->actions) {
55  if (stat.action == action) {
56  stat.end = d->engineRequest->elapsed.nsecsElapsed();
57  break;
58  }
59  }
60 }
61 
62 QByteArray Stats::report()
63 {
64  Q_D(const Stats);
65 
66  QByteArray ret;
67  if (d->actions.size() == 0) {
68  return ret;
69  }
70 
71  QVector<QStringList> table;
72  for (const auto &stat : d->actions) {
73  table.append({ stat.action,
74  QString::number((stat.end - stat.begin)/1000000000.0, 'f') + QLatin1Char('s') });
75  }
76 
77  ret = Utils::buildTable(table, {
78  QStringLiteral("Action"), QStringLiteral("Time")
79  });
80  return ret;
81 }
Cutelyst::Stats::profileStart
virtual void profileStart(const QString &action)
Definition: stats.cpp:42
Cutelyst::Stats::profileEnd
virtual void profileEnd(const QString &action)
Definition: stats.cpp:51
Cutelyst::EngineRequest
Definition: enginerequest.h:31
Cutelyst::Stats::Stats
Stats(EngineRequest *request)
Definition: stats.cpp:31
Cutelyst::Stats
Definition: stats.h:29
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::Stats::report
virtual QByteArray report()
Definition: stats.cpp:62