Home | History | Annotate | Download | only in performance_monitor
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_PERFORMANCE_MONITOR_KEY_BUILDER_H_
      6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_KEY_BUILDER_H_
      7 
      8 #include <map>
      9 
     10 #include "chrome/browser/performance_monitor/event.h"
     11 #include "chrome/browser/performance_monitor/metric.h"
     12 
     13 namespace performance_monitor {
     14 
     15 struct RecentKey {
     16   RecentKey(const std::string& recent_time,
     17             MetricType recent_type,
     18             const std::string& recent_activity);
     19   ~RecentKey();
     20 
     21   const std::string time;
     22   const MetricType type;
     23   const std::string activity;
     24 };
     25 
     26 struct MaxValueKey {
     27   MaxValueKey(MetricType max_value_type,
     28               const std::string& max_value_activity)
     29       : type(max_value_type),
     30         activity(max_value_activity) {}
     31   ~MaxValueKey() {}
     32 
     33   const MetricType type;
     34   const std::string activity;
     35 };
     36 
     37 struct MetricKey {
     38   MetricKey(const std::string& metric_time,
     39             MetricType metric_type,
     40             const std::string& metric_activity);
     41   ~MetricKey();
     42 
     43   const std::string time;
     44   const MetricType type;
     45   const std::string activity;
     46 };
     47 
     48 // This class is responsible for building the keys which are used internally by
     49 // PerformanceMonitor's database. These keys should only be referenced by the
     50 // database, and should not be used externally.
     51 class KeyBuilder {
     52  public:
     53   KeyBuilder();
     54   ~KeyBuilder();
     55 
     56   // Key Creation: Create the keys for different databases. The schemas are
     57   // listed with the methods. A '-' in the schema represents kDelimeter.
     58 
     59   // Key Schema: <Time>
     60   std::string CreateActiveIntervalKey(const base::Time& time);
     61 
     62   // Key Schema: <Metric>-<Time>-<Activity>
     63   std::string CreateMetricKey(const base::Time& time,
     64                               const MetricType type,
     65                               const std::string& activity);
     66 
     67   // Key Schema: <Time>-<Event Type>
     68   std::string CreateEventKey(const base::Time& time, const EventType type);
     69 
     70   // Key Schema: <Time>-<Metric>-<Activity>
     71   std::string CreateRecentKey(const base::Time& time,
     72                               const MetricType type,
     73                               const std::string& activity);
     74 
     75   // Key Schema: <Activity>-<Metric>
     76   std::string CreateRecentMapKey(const MetricType type,
     77                                  const std::string& activity);
     78 
     79   std::string CreateMaxValueKey(const MetricType type,
     80                                 const std::string& activity);
     81 
     82   EventType EventKeyToEventType(const std::string& key);
     83   RecentKey SplitRecentKey(const std::string& key);
     84   MetricKey SplitMetricKey(const std::string& key);
     85 
     86  private:
     87   // Populate the maps from [Event, Metric]Type to key characters.
     88   void PopulateKeyMaps();
     89 
     90   std::map<EventType, int> event_type_to_event_key_char_;
     91   std::map<int, EventType> event_key_char_to_event_type_;
     92   std::map<MetricType, int> metric_type_to_metric_key_char_;
     93   std::map<int, MetricType> metric_key_char_to_metric_type_;
     94 };
     95 
     96 }  // namespace performance_monitor
     97 
     98 #endif  // CHROME_BROWSER_PERFORMANCE_MONITOR_KEY_BUILDER_H_
     99