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

Side by Side Diff: chrome/browser/performance_monitor/database.cc

Issue 10860017: Refactor Metrics (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed DCHECK Created 8 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/performance_monitor/database.h" 5 #include "chrome/browser/performance_monitor/database.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 238
239 for (it->Seek(start_key); 239 for (it->Seek(start_key);
240 it->Valid() && it->key().ToString() < end_key; 240 it->Valid() && it->key().ToString() < end_key;
241 it->Next()) { 241 it->Next()) {
242 results.push_back(ActiveIntervalToTimeRange(it->key().ToString(), 242 results.push_back(ActiveIntervalToTimeRange(it->key().ToString(),
243 it->value().ToString())); 243 it->value().ToString()));
244 } 244 }
245 return results; 245 return results;
246 } 246 }
247 247
248 Database::EventList Database::GetEvents(EventType type, const base::Time& start, 248 Database::EventVector Database::GetEvents(EventType type,
249 const base::Time& end) { 249 const base::Time& start,
250 const base::Time& end) {
250 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 251 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
251 EventList events; 252 EventVector events;
252 std::string start_key = CreateEventKey(start, EVENT_UNDEFINED); 253 std::string start_key = CreateEventKey(start, EVENT_UNDEFINED);
253 std::string end_key = CreateEventKey(end, EVENT_NUMBER_OF_EVENTS); 254 std::string end_key = CreateEventKey(end, EVENT_NUMBER_OF_EVENTS);
254 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_)); 255 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_));
255 for (it->Seek(start_key); 256 for (it->Seek(start_key);
256 it->Valid() && it->key().ToString() <= end_key; 257 it->Valid() && it->key().ToString() <= end_key;
257 it->Next()) { 258 it->Next()) {
258 if (type != EVENT_UNDEFINED) { 259 if (type != EVENT_UNDEFINED) {
259 EventType key_type = EventKeyToEventType(it->key().ToString()); 260 EventType key_type = EventKeyToEventType(it->key().ToString());
260 if (key_type != type) 261 if (key_type != type)
261 continue; 262 continue;
(...skipping 22 matching lines...) Expand all
284 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_)); 285 scoped_ptr<leveldb::Iterator> it(event_db_->NewIterator(read_options_));
285 for (it->Seek(start_key); 286 for (it->Seek(start_key);
286 it->Valid() && it->key().ToString() <= end_key; 287 it->Valid() && it->key().ToString() <= end_key;
287 it->Next()) { 288 it->Next()) {
288 EventType key_type = EventKeyToEventType(it->key().ToString()); 289 EventType key_type = EventKeyToEventType(it->key().ToString());
289 results.insert(key_type); 290 results.insert(key_type);
290 } 291 }
291 return results; 292 return results;
292 } 293 }
293 294
294 bool Database::AddMetric(const std::string& activity, MetricType metric, 295 bool Database::AddMetric(const std::string& activity,
296 MetricType metric,
295 const std::string& value) { 297 const std::string& value) {
296 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 298 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
297 UpdateActiveInterval(); 299 UpdateActiveInterval();
298 base::Time timestamp = clock_->GetTime(); 300 base::Time timestamp = clock_->GetTime();
299 std::string recent_key = CreateRecentKey(timestamp, metric, activity); 301 std::string recent_key = CreateRecentKey(timestamp, metric, activity);
300 std::string metric_key = CreateMetricKey(timestamp, metric, activity); 302 std::string metric_key = CreateMetricKey(timestamp, metric, activity);
301 std::string recent_map_key = CreateRecentMapKey(metric, activity); 303 std::string recent_map_key = CreateRecentMapKey(metric, activity);
302 // Use recent_map_ to quickly find the key that must be removed. 304 // Use recent_map_ to quickly find the key that must be removed.
303 RecentMap::iterator old_it = recent_map_.find(recent_map_key); 305 RecentMap::iterator old_it = recent_map_.find(recent_map_key);
304 if (old_it != recent_map_.end()) 306 if (old_it != recent_map_.end())
305 recent_db_->Delete(write_options_, old_it->second); 307 recent_db_->Delete(write_options_, old_it->second);
306 recent_map_[recent_map_key] = recent_key; 308 recent_map_[recent_map_key] = recent_key;
307 leveldb::Status recent_status = 309 leveldb::Status recent_status =
308 recent_db_->Put(write_options_, recent_key, value); 310 recent_db_->Put(write_options_, recent_key, value);
309 leveldb::Status metric_status = 311 leveldb::Status metric_status =
310 metric_db_->Put(write_options_, metric_key, value); 312 metric_db_->Put(write_options_, metric_key, value);
311 return recent_status.ok() && metric_status.ok(); 313 return recent_status.ok() && metric_status.ok();
312 } 314 }
313 315
314 std::vector<const MetricDetails*> Database::GetActiveMetrics( 316 Database::MetricTypeSet Database::GetActiveMetrics(const base::Time& start,
315 const base::Time& start, const base::Time& end) { 317 const base::Time& end) {
316 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 318 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
317 std::vector<const MetricDetails*> results;
318 std::string recent_start_key = CreateRecentKey( 319 std::string recent_start_key = CreateRecentKey(
319 start, static_cast<MetricType>(0), std::string()); 320 start, static_cast<MetricType>(0), std::string());
320 std::string recent_end_key = CreateRecentKey( 321 std::string recent_end_key = CreateRecentKey(
321 end, METRIC_NUMBER_OF_METRICS, std::string()); 322 end, METRIC_NUMBER_OF_METRICS, std::string());
322 std::string recent_end_of_time_key = CreateRecentKey( 323 std::string recent_end_of_time_key = CreateRecentKey(
323 clock_->GetTime(), METRIC_NUMBER_OF_METRICS, std::string()); 324 clock_->GetTime(), METRIC_NUMBER_OF_METRICS, std::string());
324 325
325 std::set<MetricType> active_metrics; 326 MetricTypeSet active_metrics;
326 // Get all the guaranteed metrics. 327 // Get all the guaranteed metrics.
327 scoped_ptr<leveldb::Iterator> recent_it( 328 scoped_ptr<leveldb::Iterator> recent_it(
328 recent_db_->NewIterator(read_options_)); 329 recent_db_->NewIterator(read_options_));
329 for (recent_it->Seek(recent_start_key); 330 for (recent_it->Seek(recent_start_key);
330 recent_it->Valid() && recent_it->key().ToString() <= recent_end_key; 331 recent_it->Valid() && recent_it->key().ToString() <= recent_end_key;
331 recent_it->Next()) { 332 recent_it->Next()) {
332 RecentKey split_key = SplitRecentKey(recent_it->key().ToString()); 333 RecentKey split_key = SplitRecentKey(recent_it->key().ToString());
333 active_metrics.insert(split_key.type); 334 active_metrics.insert(split_key.type);
334 } 335 }
335 // Get all the possible metrics (metrics that may have been updated after 336 // Get all the possible metrics (metrics that may have been updated after
336 // |end|). 337 // |end|).
337 std::set<MetricType> possible_metrics; 338 MetricTypeSet possible_metrics;
338 for (recent_it->Seek(recent_end_key); 339 for (recent_it->Seek(recent_end_key);
339 recent_it->Valid() && 340 recent_it->Valid() &&
340 recent_it->key().ToString() <= recent_end_of_time_key; 341 recent_it->key().ToString() <= recent_end_of_time_key;
341 recent_it->Next()) { 342 recent_it->Next()) {
342 RecentKey split_key = SplitRecentKey(recent_it->key().ToString()); 343 RecentKey split_key = SplitRecentKey(recent_it->key().ToString());
343 possible_metrics.insert(split_key.type); 344 possible_metrics.insert(split_key.type);
344 } 345 }
345 std::set<MetricType>::iterator possible_it; 346 MetricTypeSet::iterator possible_it;
346 scoped_ptr<leveldb::Iterator> metric_it( 347 scoped_ptr<leveldb::Iterator> metric_it(
347 metric_db_->NewIterator(read_options_)); 348 metric_db_->NewIterator(read_options_));
348 for (possible_it = possible_metrics.begin(); 349 for (possible_it = possible_metrics.begin();
349 possible_it != possible_metrics.end(); 350 possible_it != possible_metrics.end();
350 ++possible_it) { 351 ++possible_it) {
351 std::string metric_start_key = CreateMetricKey(start, *possible_it, 352 std::string metric_start_key = CreateMetricKey(start, *possible_it,
352 std::string()); 353 std::string());
353 std::string metric_end_key = CreateMetricKey(end, *possible_it, 354 std::string metric_end_key = CreateMetricKey(end, *possible_it,
354 std::string()); 355 std::string());
355 metric_it->Seek(metric_start_key); 356 metric_it->Seek(metric_start_key);
356 // Stats in the timerange from any activity makes the metric active. 357 // Stats in the timerange from any activity makes the metric active.
357 if (metric_it->Valid() && metric_it->key().ToString() <= metric_end_key) { 358 if (metric_it->Valid() && metric_it->key().ToString() <= metric_end_key) {
358 active_metrics.insert(*possible_it); 359 active_metrics.insert(*possible_it);
359 } 360 }
360 } 361 }
361 std::set<MetricType>::iterator it;
362 for (it = active_metrics.begin(); it != active_metrics.end(); ++it)
363 results.push_back(GetMetricDetails(*it));
364 362
365 return results; 363 return active_metrics;
366 } 364 }
367 365
368 std::vector<std::string> Database::GetActiveActivities( 366 std::set<std::string> Database::GetActiveActivities(MetricType metric_type,
369 MetricType metric_type, const base::Time& start) { 367 const base::Time& start) {
370 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 368 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
371 std::vector<std::string> results; 369 std::set<std::string> results;
372 std::string start_key = CreateRecentKey( 370 std::string start_key = CreateRecentKey(
373 start, static_cast<MetricType>(0), std::string()); 371 start, static_cast<MetricType>(0), std::string());
374 scoped_ptr<leveldb::Iterator> it(recent_db_->NewIterator(read_options_)); 372 scoped_ptr<leveldb::Iterator> it(recent_db_->NewIterator(read_options_));
375 for (it->Seek(start_key); it->Valid(); it->Next()) { 373 for (it->Seek(start_key); it->Valid(); it->Next()) {
376 RecentKey split_key = SplitRecentKey(it->key().ToString()); 374 RecentKey split_key = SplitRecentKey(it->key().ToString());
377 if (split_key.type == metric_type) 375 if (split_key.type == metric_type)
378 results.push_back(split_key.activity); 376 results.insert(split_key.activity);
379 } 377 }
380 return results; 378 return results;
381 } 379 }
382 380
383 bool Database::GetRecentStatsForActivityAndMetric( 381 bool Database::GetRecentStatsForActivityAndMetric(const std::string& activity,
384 const std::string& activity, 382 MetricType metric_type,
385 MetricType metric, 383 Metric* metric) {
386 MetricInfo* info) {
387 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 384 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
388 std::string recent_map_key = CreateRecentMapKey(metric, activity); 385 std::string recent_map_key = CreateRecentMapKey(metric_type, activity);
389 if (!ContainsKey(recent_map_, recent_map_key)) 386 if (!ContainsKey(recent_map_, recent_map_key))
390 return false; 387 return false;
391 std::string recent_key = recent_map_[recent_map_key]; 388 std::string recent_key = recent_map_[recent_map_key];
392 389
393 std::string result; 390 std::string result;
394 leveldb::Status status = recent_db_->Get(read_options_, recent_key, &result); 391 leveldb::Status status = recent_db_->Get(read_options_, recent_key, &result);
395 if (status.ok()) 392 if (status.ok())
396 *info = MetricInfo(SplitRecentKey(recent_key).time, result); 393 *metric = Metric(SplitRecentKey(recent_key).time, result);
397 return status.ok(); 394 return status.ok();
398 } 395 }
399 396
400 Database::MetricInfoVector Database::GetStatsForActivityAndMetric( 397 Database::MetricVector Database::GetStatsForActivityAndMetric(
401 const std::string& activity, MetricType metric_type, 398 const std::string& activity,
402 const base::Time& start, const base::Time& end) { 399 MetricType metric_type,
400 const base::Time& start,
401 const base::Time& end) {
403 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 402 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
404 MetricInfoVector results; 403 MetricVector results;
405 std::string start_key = CreateMetricKey(start, metric_type, activity); 404 std::string start_key = CreateMetricKey(start, metric_type, activity);
406 std::string end_key = CreateMetricKey(end, metric_type, activity); 405 std::string end_key = CreateMetricKey(end, metric_type, activity);
407 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); 406 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_));
408 for (it->Seek(start_key); 407 for (it->Seek(start_key);
409 it->Valid() && it->key().ToString() <= end_key; 408 it->Valid() && it->key().ToString() <= end_key;
410 it->Next()) { 409 it->Next()) {
411 MetricKey split_key = SplitMetricKey(it->key().ToString()); 410 MetricKey split_key = SplitMetricKey(it->key().ToString());
412 if (split_key.activity == activity) 411 if (split_key.activity == activity)
413 results.push_back(MetricInfo(split_key.time, it->value().ToString())); 412 results.push_back(Metric(split_key.time, it->value().ToString()));
414 } 413 }
415 return results; 414 return results;
416 } 415 }
417 416
418 Database::MetricVectorMap Database::GetStatsForMetricByActivity( 417 Database::MetricVectorMap Database::GetStatsForMetricByActivity(
419 MetricType metric_type, const base::Time& start, const base::Time& end) { 418 MetricType metric_type,
419 const base::Time& start,
420 const base::Time& end) {
420 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 421 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
421 MetricVectorMap results; 422 MetricVectorMap results;
422 std::string start_key = CreateMetricKey(start, metric_type, std::string()); 423 std::string start_key = CreateMetricKey(start, metric_type, std::string());
423 std::string end_key = CreateMetricKey(end, metric_type, std::string()); 424 std::string end_key = CreateMetricKey(end, metric_type, std::string());
424 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_)); 425 scoped_ptr<leveldb::Iterator> it(metric_db_->NewIterator(read_options_));
425 for (it->Seek(start_key); 426 for (it->Seek(start_key);
426 it->Valid() && it->key().ToString() <= end_key; 427 it->Valid() && it->key().ToString() <= end_key;
427 it->Next()) { 428 it->Next()) {
428 MetricKey split_key = SplitMetricKey(it->key().ToString()); 429 MetricKey split_key = SplitMetricKey(it->key().ToString());
429 if (!results[split_key.activity].get()) { 430 if (!results[split_key.activity].get()) {
430 results[split_key.activity] = 431 results[split_key.activity] =
431 linked_ptr<MetricInfoVector>(new MetricInfoVector()); 432 linked_ptr<MetricVector >(new MetricVector());
432 } 433 }
433 results[split_key.activity]->push_back( 434 results[split_key.activity]->push_back(
434 MetricInfo(split_key.time, it->value().ToString())); 435 Metric(split_key.time, it->value().ToString()));
435 } 436 }
436 return results; 437 return results;
437 } 438 }
438 439
439 Database::Database(const FilePath& path) 440 Database::Database(const FilePath& path)
440 : path_(path), 441 : path_(path),
441 read_options_(leveldb::ReadOptions()), 442 read_options_(leveldb::ReadOptions()),
442 write_options_(leveldb::WriteOptions()) { 443 write_options_(leveldb::WriteOptions()) {
443 InitDBs(); 444 InitDBs();
444 LoadRecents(); 445 LoadRecents();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 start_time_key_ = CreateActiveIntervalKey(current_time); 524 start_time_key_ = CreateActiveIntervalKey(current_time);
524 end_time = start_time_key_; 525 end_time = start_time_key_;
525 } else { 526 } else {
526 end_time = CreateActiveIntervalKey(clock_->GetTime()); 527 end_time = CreateActiveIntervalKey(clock_->GetTime());
527 } 528 }
528 last_update_time_ = current_time; 529 last_update_time_ = current_time;
529 active_interval_db_->Put(write_options_, start_time_key_, end_time); 530 active_interval_db_->Put(write_options_, start_time_key_, end_time);
530 } 531 }
531 532
532 } // namespace performance_monitor 533 } // namespace performance_monitor
OLDNEW
« no previous file with comments | « chrome/browser/performance_monitor/database.h ('k') | chrome/browser/performance_monitor/database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698