TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
metrics.h
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <chrono>
11 #include <cstdio>
12 #include <functional>
13 #include <memory>
14 #include <mutex>
15 #include <unordered_map>
16 #include <valarray>
17 #include <vector>
18 
19 namespace cpid {
21  using hires_clock = std::chrono::steady_clock;
22 
23  public:
24  using Timestamp = uint64_t; // ms since epoch, for simple json dump
25  using Event = std::pair<Timestamp, float>;
26  using Events = std::pair<Timestamp, std::vector<float>>;
27  using TimeInterval = double; // ms
28 
29  void pushEvent(std::string const& key, float value = 1.0);
30  void pushEvents(std::string const& key, std::vector<float> values);
31  Event getLastEvent(std::string const& key) const;
32  std::vector<Event> getLastEvents(std::string const& key, size_t n) const;
33  float getLastEventValue(std::string const& key) const;
34  bool hasEvent(std::string const& key) const;
35  std::unordered_map<std::string, float> getMeanEventValues() const;
36  using Reducer = std::function<float(float, float)>;
37  /// Behaves exactly as std::accumulate on all the events streams
38  std::unordered_map<std::string, float> reduceEventValues(
39  const Reducer& reducer,
40  float initValue) const;
41  void incCounter(std::string const& key, float amount = 1.0);
42  void setCounter(std::string const& key, float amount);
43 
44  bool hasCounter(std::string const& key) const;
45  float getCounter(std::string const& key) const;
46  float getCounter(std::string const& key, float defaultValue) const;
47  void snapshotCounter(
48  std::string const& counterKey,
49  std::string const& eventKey,
50  float defaultValue);
51  TimeInterval getLastInterval(std::string const& key) const;
52  std::unordered_map<std::string, float> getMeanIntervals() const;
53  /// Behaves exactly as std::accumulate on all the intervals streams
54  std::unordered_map<std::string, float> reduceIntervals(
55  const Reducer& reducer,
56  float initValue) const;
57  void dumpJson(std::string const& path) const;
58  void dumpJson(std::ostream&) const;
59  void loadJson(std::string const& path);
60  void loadJson(std::istream&);
61 
62  void clear();
63 
64  bool operator==(const MetricsContext&) const; // Mostly for testing
65 
66  class Timer {
67  hires_clock::time_point start_;
68  std::shared_ptr<MetricsContext> metrics_;
69  std::string key_;
70  unsigned long subsampleFactor_;
71 
72  public:
73  /// Only subsampleRatio events are stored; it is expected that
74  // 0 <= subsampleRatio <= 1.
75  Timer(
76  std::shared_ptr<MetricsContext> metrics,
77  std::string key,
78  float subsampleRatio = 1);
79  ~Timer();
80  };
81 
82  protected:
83  std::unordered_map<std::string, std::vector<Event>> timeSeries_;
84  std::unordered_map<std::string, std::vector<Events>> timeSeriesS_;
85  std::unordered_map<std::string, float> counters_;
86  std::unordered_map<std::string, std::vector<TimeInterval>> intervals_;
87  mutable std::mutex mutex_;
88 };
89 } // namespace cpid
~Timer()
Definition: metrics.cpp:256
uint64_t Timestamp
Definition: metrics.h:24
void setCounter(std::string const &key, float amount)
Definition: metrics.cpp:118
std::pair< Timestamp, std::vector< float >> Events
Definition: metrics.h:26
std::unordered_map< std::string, float > reduceEventValues(const Reducer &reducer, float initValue) const
Behaves exactly as std::accumulate on all the events streams.
Definition: metrics.cpp:81
bool hasCounter(std::string const &key) const
Definition: metrics.cpp:104
std::unordered_map< std::string, std::vector< Event > > timeSeries_
Definition: metrics.h:83
std::function< float(float, float)> Reducer
Definition: metrics.h:36
double TimeInterval
Definition: metrics.h:27
std::unordered_map< std::string, std::vector< TimeInterval > > intervals_
Definition: metrics.h:86
std::pair< Timestamp, float > Event
Definition: metrics.h:25
Definition: metrics.h:66
void pushEvent(std::string const &key, float value=1.0)
Definition: metrics.cpp:14
TimeInterval getLastInterval(std::string const &key) const
Definition: metrics.cpp:149
void snapshotCounter(std::string const &counterKey, std::string const &eventKey, float defaultValue)
Definition: metrics.cpp:133
std::unordered_map< std::string, float > counters_
Definition: metrics.h:85
std::mutex mutex_
Definition: metrics.h:87
std::unordered_map< std::string, float > getMeanEventValues() const
Definition: metrics.cpp:64
void dumpJson(std::string const &path) const
Definition: metrics.cpp:190
bool hasEvent(std::string const &key) const
Definition: metrics.cpp:59
std::vector< Event > getLastEvents(std::string const &key, size_t n) const
Definition: metrics.cpp:42
float getLastEventValue(std::string const &key) const
Definition: metrics.cpp:55
float getCounter(std::string const &key) const
Definition: metrics.cpp:109
void incCounter(std::string const &key, float amount=1.0)
Definition: metrics.cpp:99
The TorchCraftAI training library.
Definition: batcher.cpp:15
std::unordered_map< std::string, std::vector< Events > > timeSeriesS_
Definition: metrics.h:84
Timer(std::shared_ptr< MetricsContext > metrics, std::string key, float subsampleRatio=1)
Only subsampleRatio events are stored; it is expected that.
Definition: metrics.cpp:237
std::unordered_map< std::string, float > reduceIntervals(const Reducer &reducer, float initValue) const
Behaves exactly as std::accumulate on all the intervals streams.
Definition: metrics.cpp:176
Event getLastEvent(std::string const &key) const
Definition: metrics.cpp:32
Definition: metrics.h:20
void pushEvents(std::string const &key, std::vector< float > values)
Definition: metrics.cpp:22
bool operator==(const MetricsContext &) const
Definition: metrics.cpp:231
void clear()
Definition: metrics.cpp:223
std::unordered_map< std::string, float > getMeanIntervals() const
Definition: metrics.cpp:159
void loadJson(std::string const &path)
Definition: metrics.cpp:208