Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: apps/benchmark/event.cc

Issue 1381933002: Fix the benchmark app to handle real-world events (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | apps/benchmark/event_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "apps/benchmark/event.h" 5 #include "apps/benchmark/event.h"
6 6
7 #include <map> 7 #include <map>
8 #include <stack> 8 #include <stack>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
13 #include "base/values.h" 14 #include "base/values.h"
14 15
15 namespace benchmark { 16 namespace benchmark {
16 17
17 Event::Event() {} 18 Event::Event() {}
18 19
19 Event::Event(EventType type, 20 Event::Event(EventType type,
20 std::string name, 21 std::string name,
21 std::string categories, 22 std::string categories,
22 base::TimeTicks timestamp, 23 base::TimeTicks timestamp,
23 base::TimeDelta duration) 24 base::TimeDelta duration)
24 : type(type), 25 : type(type),
25 name(name), 26 name(name),
26 categories(categories), 27 categories(categories),
27 timestamp(timestamp), 28 timestamp(timestamp),
28 duration(duration) {} 29 duration(duration) {}
29 30
30 Event::~Event() {} 31 Event::~Event() {}
31 32
32 namespace { 33 namespace {
33 // ID uniquely identifying an asynchronous event stack. 34 // ID uniquely identifying an asynchronous event stack.
34 struct AsyncEventStackId { 35 struct AsyncEventStackId {
35 int id; 36 std::string id;
36 std::string cat; 37 std::string cat;
37 }; 38 };
38 39
39 bool operator<(const AsyncEventStackId& t, const AsyncEventStackId& o) { 40 bool operator<(const AsyncEventStackId& t, const AsyncEventStackId& o) {
40 return t.id < o.id || (t.id == o.id && t.cat < o.cat); 41 return t.id < o.id || (t.id == o.id && t.cat < o.cat);
41 } 42 }
42 43
43 // ID uniquely identifying a duration event stack. 44 // ID uniquely identifying a duration event stack.
44 typedef int DurationEventStackId; 45 typedef std::string DurationEventStackId;
45 46
46 // Makes a unique id for a duration event stack. 47 bool ExtractKeyAsString(base::DictionaryValue* event_dict,
47 bool GetDurationEventStackId(base::DictionaryValue* event_dict, 48 const std::string& key,
48 DurationEventStackId* durationId) { 49 std::string* output) {
49 if (!event_dict->GetInteger("tid", durationId)) { 50 const base::Value* value;
50 LOG(ERROR) << "Incorrect trace event (missing tid)."; 51 if (!event_dict->Get(key, &value)) {
52 LOG(ERROR) << "Incorrect trace event (missing " + key + "): "
53 << *event_dict;
54 return false;
55 }
56
57 if (value->IsType(base::Value::TYPE_INTEGER)) {
58 int id_int;
59 // We already verified the type, so it should be an integer.
60 DCHECK(value->GetAsInteger(&id_int));
61 *output = base::IntToString(id_int);
62 } else if (value->IsType(base::Value::TYPE_STRING)) {
63 DCHECK(value->GetAsString(output));
64 } else {
65 LOG(ERROR) << "Incorrect trace event (" + key +
66 " not a string or integer): "
67 << *event_dict;
51 return false; 68 return false;
52 } 69 }
53 return true; 70 return true;
54 } 71 }
55 72
73 // Makes a unique id for a duration event stack.
74 bool GetDurationEventStackId(base::DictionaryValue* event_dict,
75 DurationEventStackId* durationId) {
76 return ExtractKeyAsString(event_dict, "tid", durationId);
77 }
78
56 // Makes a unique key for an async event stack. 79 // Makes a unique key for an async event stack.
57 bool GetAsyncEventStackId(base::DictionaryValue* event_dict, 80 bool GetAsyncEventStackId(base::DictionaryValue* event_dict,
58 AsyncEventStackId* asyncId) { 81 AsyncEventStackId* asyncId) {
59 if (!event_dict->GetInteger("id", &asyncId->id)) { 82 if (!ExtractKeyAsString(event_dict, "id", &asyncId->id)) {
60 LOG(ERROR) << "Incorrect async trace event (missing id).";
61 return false; 83 return false;
62 } 84 }
63 85
64 // We can have an empty category, but it is still relevant for event merging, 86 // We can have an empty category, but it is still relevant for event
65 // per the documentation. 87 // merging per the documentation.
66 std::string cat; 88 std::string cat;
67 event_dict->GetString("cat", &asyncId->cat); 89 event_dict->GetString("cat", &asyncId->cat);
68 90
69 return true; 91 return true;
70 } 92 }
71 93
72 // Given a beginning event, registers its beginning for future merging. 94 // Given a beginning event, registers its beginning for future merging.
73 template <typename T> 95 template <typename T>
74 void RegisterEventBegin( 96 void RegisterEventBegin(
75 T key, 97 T key,
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } else { 210 } else {
189 // Skip all event types we do not handle. 211 // Skip all event types we do not handle.
190 continue; 212 continue;
191 } 213 }
192 214
193 if (!event_dict->GetString("name", &event.name)) { 215 if (!event_dict->GetString("name", &event.name)) {
194 LOG(ERROR) << "Incorrect trace event (no name)"; 216 LOG(ERROR) << "Incorrect trace event (no name)";
195 return false; 217 return false;
196 } 218 }
197 219
198 if (!event_dict->GetString("cat", &event.categories)) { 220 // Some clients do not add categories to events, but we don't want to fail
199 LOG(WARNING) << "Ignoring incorrect trace event (no categories)"; 221 // nor skip the event.
200 continue; 222 event_dict->GetString("cat", &event.categories);
201 }
202 223
203 double timestamp; 224 double timestamp;
204 if (!event_dict->GetDouble("ts", &timestamp)) { 225 if (!event_dict->GetDouble("ts", &timestamp)) {
205 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)"; 226 LOG(WARNING) << "Ingoring incorrect trace event (no timestamp)";
206 continue; 227 continue;
207 } 228 }
208 event.timestamp = base::TimeTicks::FromInternalValue(timestamp); 229 event.timestamp = base::TimeTicks::FromInternalValue(timestamp);
209 230
210 if (event.type == EventType::COMPLETE) { 231 if (event.type == EventType::COMPLETE) {
211 double duration; 232 double duration;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 } 264 }
244 265
245 if (!JoinEvents(event_list)) 266 if (!JoinEvents(event_list))
246 return false; 267 return false;
247 268
248 if (!ParseEvents(event_list, result)) 269 if (!ParseEvents(event_list, result))
249 return false; 270 return false;
250 return true; 271 return true;
251 } 272 }
252 } // namespace benchmark 273 } // namespace benchmark
OLDNEW
« no previous file with comments | « no previous file | apps/benchmark/event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698