Fabcoin Core  0.16.2
P2P Digital Currency
Stats.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum 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
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #include <test/libtesteth/Stats.h>
20 #include <iterator>
21 #include <numeric>
22 #include <fstream>
23 
24 namespace dev
25 {
26 namespace test
27 {
28 
30 {
31  static Stats instance;
32  return instance;
33 }
34 
35 void Stats::suiteStarted(std::string const& _name)
36 {
37  m_currentSuite = _name;
38 }
39 
40 void Stats::testStarted(std::string const& _name)
41 {
42  m_currentTest = _name;
43  m_tp = clock::now();
44 }
45 
46 void Stats::testFinished(int64_t _gasUsed)
47 {
48  m_stats.push_back({clock::now() - m_tp, _gasUsed, m_currentSuite + "/" + m_currentTest});
49 }
50 
51 std::ostream& operator<<(std::ostream& out, Stats::clock::duration const& d)
52 {
53  return out << std::setw(10) << std::right << std::chrono::duration_cast<std::chrono::microseconds>(d).count() << " us";
54 }
55 
57 {
58  if (m_stats.empty())
59  return;
60 
61  std::sort(m_stats.begin(), m_stats.end(), [](Stats::Item const& a, Stats::Item const& b){
62  return a.duration < b.duration;
63  });
64 
65  auto& out = std::cout;
66  auto itr = m_stats.begin();
67  auto min = *itr;
68  auto max = *m_stats.rbegin();
69  std::advance(itr, m_stats.size() / 2);
70  auto med = *itr;
71  auto tot = std::accumulate(m_stats.begin(), m_stats.end(), clock::duration{}, [](clock::duration const& a, Stats::Item const& v)
72  {
73  return a + v.duration;
74  });
75 
76  out << "\nSTATS:\n\n" << std::setfill(' ');
77 
78  if (Options::get().statsOutFile == "out")
79  {
80  for (auto&& s: m_stats)
81  {
82  auto usecs = std::chrono::duration_cast<std::chrono::microseconds>(s.duration).count();
83  out << " " << std::setw(40) << std::left << s.name.substr(0, 40) << s.duration;
84  if (s.gasUsed >= 0)
85  {
86  auto gasRate = uint64_t(double(s.gasUsed) * 1000 / usecs);
87  out << "\t" << std::setw(10) << std::right << gasRate << " gas/ms\n";
88  }
89  else
90  out << "\tOOG\n";
91  }
92  out << "\n";
93  }
94  else if (!Options::get().statsOutFile.empty())
95  {
96  // Output stats to file
97  std::ofstream file{Options::get().statsOutFile};
98  for (auto&& s: m_stats)
99  {
100  auto usecs = std::chrono::duration_cast<std::chrono::microseconds>(s.duration).count();
101  file << s.name << "\t" << usecs;
102  if (s.gasUsed >= 0)
103  {
104  auto gasRate = s.gasUsed / usecs;
105  file << "\t" << gasRate << " gas/us\n";
106  }
107  else
108  file << "\tOOG\n";
109  }
110  }
111 
112  out << " tot: " << tot << "\n"
113  << " avg: " << (tot / m_stats.size()) << "\n\n"
114  << " min: " << min.duration << " (" << min.name << ")\n"
115  << " med: " << med.duration << " (" << med.name << ")\n"
116  << " max: " << max.duration << " (" << max.name << ")\n";
117 }
118 
119 }
120 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
static Options const & get(int argc=0, char **argv=0)
Get reference to options The first time used, options are parsed with argc, argv. ...
Definition: Options.cpp:203
clock::time_point m_tp
Definition: Stats.h:51
std::string m_currentTest
Definition: Stats.h:53
std::string statsOutFile
Stats output file. "out" for standard output.
Definition: Options.h:44
size_t count
Definition: ExecStats.cpp:37
void suiteStarted(std::string const &_name) override
Definition: Stats.cpp:35
std::string m_currentSuite
Definition: Stats.h:52
ExecStats::duration min
Definition: ExecStats.cpp:35
Class for handling testeth custom options.
#define a(i)
ExecStats::duration tot
Definition: ExecStats.cpp:34
clock::duration duration
Definition: bench.h:50
ExecStats::duration max
Definition: ExecStats.cpp:36
#define b(i, j)
void testStarted(std::string const &_name) override
Definition: Stats.cpp:40
static Stats & get()
Definition: Stats.cpp:29
void testFinished(int64_t _gasUsed) override
Definition: Stats.cpp:46
clock::duration duration
Definition: Stats.h:37
#define d(i)
Definition: sha.cpp:732
std::vector< Item > m_stats
Definition: Stats.h:54
std::ostream & operator<<(std::ostream &out, Stats::clock::duration const &d)
Definition: Stats.cpp:51