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

Side by Side Diff: base/test/trace_event_analyzer.cc

Issue 8900001: Add searching features to new TraceEventVector class for tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/test/trace_event_analyzer.h" 5 #include "base/test/trace_event_analyzer.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <math.h> 8 #include <math.h>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 number_(0) { 608 number_(0) {
609 type_ = (unary_op < OP_ADD ? 609 type_ = (unary_op < OP_ADD ?
610 QUERY_BOOLEAN_OPERATOR : QUERY_ARITHMETIC_OPERATOR); 610 QUERY_BOOLEAN_OPERATOR : QUERY_ARITHMETIC_OPERATOR);
611 } 611 }
612 612
613 namespace { 613 namespace {
614 614
615 // Search |events| for |query| and add matches to |output|. 615 // Search |events| for |query| and add matches to |output|.
616 size_t FindMatchingEvents(const std::vector<TraceEvent>& events, 616 size_t FindMatchingEvents(const std::vector<TraceEvent>& events,
617 const Query& query, 617 const Query& query,
618 TraceAnalyzer::TraceEventVector* output) { 618 TraceEventVector* output) {
619 for (size_t i = 0; i < events.size(); ++i) { 619 for (size_t i = 0; i < events.size(); ++i) {
620 if (query.Evaluate(events[i])) 620 if (query.Evaluate(events[i]))
621 output->push_back(&events[i]); 621 output->push_back(&events[i]);
622 } 622 }
623 return output->size(); 623 return output->size();
624 } 624 }
625 625
626 bool ParseEventsFromJson(const std::string& json, 626 bool ParseEventsFromJson(const std::string& json,
627 std::vector<TraceEvent>* output) { 627 std::vector<TraceEvent>* output) {
628 scoped_ptr<base::Value> root; 628 scoped_ptr<base::Value> root;
(...skipping 12 matching lines...) Expand all
641 else 641 else
642 return false; 642 return false;
643 } 643 }
644 } 644 }
645 645
646 return true; 646 return true;
647 } 647 }
648 648
649 } // namespace 649 } // namespace
650 650
651 // TraceEventVector
652
653 bool TraceEventVector::GetRateStats(RateStats* stats) {
654 // Need at least 3 events to calculate rate stats.
655 if (size() < 3) {
656 LOG(ERROR) << "Not enough events: " << size();
657 return false;
658 }
659
660 std::vector<double> deltas;
661 double delta_sum = 0.0;
662 size_t num_deltas = size() - 1;
663 for (size_t i = 0; i < num_deltas; ++i) {
664 double delta = at(i + 1)->timestamp - at(i)->timestamp;
665 if (delta < 0.0) {
666 LOG(ERROR) << "Events are out of order";
667 return false;
668 }
669 deltas.push_back(delta);
670 delta_sum += delta;
671 }
672
673 stats->min_us = *std::min_element(deltas.begin(), deltas.end());
674 stats->max_us = *std::max_element(deltas.begin(), deltas.end());
675 stats->mean_us = delta_sum / static_cast<double>(num_deltas);
676
677 double sum_mean_offsets_squared = 0.0;
678 for (size_t i = 0; i < num_deltas; ++i) {
679 double offset = fabs(deltas[i] - stats->mean_us);
680 sum_mean_offsets_squared += offset * offset;
681 }
682 stats->standard_deviation_us =
Paweł Hajdan Jr. 2011/12/12 17:21:51 Isn't this standard deviation code being duplicate
jbates 2011/12/12 22:00:32 This is just a code move, so no duplication.
683 sum_mean_offsets_squared / static_cast<double>(num_deltas - 1);
684
685 return true;
686 }
687
688 bool TraceEventVector::FindFirstOf(Query query,
689 size_t position,
690 size_t* return_index) const {
691 for (size_t i = position; i < size(); ++i) {
692 if (query.Evaluate(*at(i))) {
693 *return_index = i;
694 return true;
695 }
696 }
697 return false;
698 }
699
700 bool TraceEventVector::FindLastOf(Query query,
701 size_t position,
702 size_t* return_index) const {
703 if (empty())
704 return false;
705 position = (position < size()) ? position : size() - 1;
706 for (;;) {
Paweł Hajdan Jr. 2011/12/12 17:21:51 nit: Why not "while (true)"? It's generally more d
jbates 2011/12/12 22:00:32 Grepping base/ for 'for (;;)' turns up slightly mo
707 if (query.Evaluate(*at(position))) {
708 *return_index = position;
709 return true;
710 }
711 if (position == 0)
712 return false;
713 --position;
714 }
715 return false;
716 }
717
718 bool TraceEventVector::FindClosest(Query query,
719 size_t position,
720 size_t* return_closest,
721 size_t* return_second_closest) const {
722 if (empty() || position >= size())
723 return false;
724 size_t closest = size();
725 size_t second_closest = size();
726 for (size_t i = 0; i < size(); ++i) {
727 if (!query.Evaluate(*at(i)))
728 continue;
729 if (closest == size()) {
730 closest = i;
731 continue;
732 }
733 if (fabs(at(i)->timestamp - at(position)->timestamp) <
734 fabs(at(closest)->timestamp - at(position)->timestamp)) {
735 second_closest = closest;
736 closest = i;
737 } else if (second_closest == size()) {
738 second_closest = i;
739 }
740 }
741
742 if (closest < size() &&
743 (!return_second_closest || second_closest < size())) {
744 *return_closest = closest;
745 if (return_second_closest)
746 *return_second_closest = second_closest;
747 return true;
748 }
749
750 return false;
751 }
752
753 int TraceEventVector::CountMatches(Query query,
754 size_t begin_position,
755 size_t end_position) const {
756 if (begin_position >= size())
757 return 0;
758 end_position = (end_position < size()) ? end_position : size();
759 int count = 0;
760 for (size_t i = begin_position; i < end_position; ++i) {
761 if (query.Evaluate(*at(i)))
762 ++count;
763 }
764 return count;
765 }
766
651 // TraceAnalyzer 767 // TraceAnalyzer
652 768
653 TraceAnalyzer::TraceAnalyzer() : allow_assocation_changes_(true) { 769 TraceAnalyzer::TraceAnalyzer() : allow_assocation_changes_(true) {
654 } 770 }
655 771
656 TraceAnalyzer::~TraceAnalyzer() { 772 TraceAnalyzer::~TraceAnalyzer() {
657 } 773 }
658 774
659 // static 775 // static
660 TraceAnalyzer* TraceAnalyzer::Create(const std::string& json_events) { 776 TraceAnalyzer* TraceAnalyzer::Create(const std::string& json_events) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 break; 848 break;
733 } 849 }
734 850
735 // Not a match, restore original other and continue. 851 // Not a match, restore original other and continue.
736 begin_event.other_event = other_backup; 852 begin_event.other_event = other_backup;
737 } 853 }
738 } 854 }
739 } 855 }
740 } 856 }
741 857
858 void TraceAnalyzer::MergeAssociatedEventArgs() {
859 for (size_t i = 0; i < raw_events_.size(); ++i) {
860 if (raw_events_[i].other_event) {
861 raw_events_[i].arg_numbers.insert(
862 raw_events_[i].other_event->arg_numbers.begin(),
863 raw_events_[i].other_event->arg_numbers.end());
864 raw_events_[i].arg_strings.insert(
865 raw_events_[i].other_event->arg_strings.begin(),
866 raw_events_[i].other_event->arg_strings.end());
867 }
868 }
869 }
870
742 size_t TraceAnalyzer::FindEvents(const Query& query, TraceEventVector* output) { 871 size_t TraceAnalyzer::FindEvents(const Query& query, TraceEventVector* output) {
743 allow_assocation_changes_ = false; 872 allow_assocation_changes_ = false;
744 output->clear(); 873 output->clear();
745 return FindMatchingEvents(raw_events_, query, output); 874 return FindMatchingEvents(raw_events_, query, output);
746 } 875 }
747 876
748 const TraceEvent* TraceAnalyzer::FindOneEvent(const Query& query) { 877 const TraceEvent* TraceAnalyzer::FindOneEvent(const Query& query) {
749 TraceEventVector output; 878 TraceEventVector output;
750 if (FindEvents(query, &output) > 0) 879 if (FindEvents(query, &output) > 0)
751 return output.front(); 880 return output.front();
752 return NULL; 881 return NULL;
753 } 882 }
754 883
755 const std::string& TraceAnalyzer::GetThreadName( 884 const std::string& TraceAnalyzer::GetThreadName(
756 const TraceEvent::ProcessThreadID& thread) { 885 const TraceEvent::ProcessThreadID& thread) {
757 // If thread is not found, just add and return empty string. 886 // If thread is not found, just add and return empty string.
758 return thread_names_[thread]; 887 return thread_names_[thread];
759 } 888 }
760 889
761 bool TraceAnalyzer::GetRateStats(const TraceEventVector& events, Stats* stats) {
762 // Need at least 3 events to calculate rate stats.
763 if (events.size() < 3) {
764 LOG(ERROR) << "Not enough events: " << events.size();
765 return false;
766 }
767
768 std::vector<double> deltas;
769 double delta_sum = 0.0;
770 size_t num_deltas = events.size() - 1;
771 for (size_t i = 0; i < num_deltas; ++i) {
772 double delta = events[i + 1]->timestamp - events[i]->timestamp;
773 if (delta < 0.0) {
774 LOG(ERROR) << "Events are out of order";
775 return false;
776 }
777 deltas.push_back(delta);
778 delta_sum += delta;
779 }
780
781 stats->min_us = *std::min_element(deltas.begin(), deltas.end());
782 stats->max_us = *std::max_element(deltas.begin(), deltas.end());
783 stats->mean_us = delta_sum / static_cast<double>(num_deltas);
784
785 double sum_mean_offsets_squared = 0.0;
786 for (size_t i = 0; i < num_deltas; ++i) {
787 double offset = fabs(deltas[i] - stats->mean_us);
788 sum_mean_offsets_squared += offset * offset;
789 }
790 stats->standard_deviation_us =
791 sum_mean_offsets_squared / static_cast<double>(num_deltas - 1);
792
793 return true;
794 }
795
796 void TraceAnalyzer::ParseMetadata() { 890 void TraceAnalyzer::ParseMetadata() {
797 for (size_t i = 0; i < raw_events_.size(); ++i) { 891 for (size_t i = 0; i < raw_events_.size(); ++i) {
798 TraceEvent& this_event = raw_events_[i]; 892 TraceEvent& this_event = raw_events_[i];
799 // Check for thread name metadata. 893 // Check for thread name metadata.
800 if (this_event.phase != TRACE_EVENT_PHASE_METADATA || 894 if (this_event.phase != TRACE_EVENT_PHASE_METADATA ||
801 this_event.name != "thread_name") 895 this_event.name != "thread_name")
802 continue; 896 continue;
803 std::map<std::string, std::string>::const_iterator string_it = 897 std::map<std::string, std::string>::const_iterator string_it =
804 this_event.arg_strings.find("name"); 898 this_event.arg_strings.find("name");
805 if (string_it != this_event.arg_strings.end()) 899 if (string_it != this_event.arg_strings.end())
806 thread_names_[this_event.thread] = string_it->second; 900 thread_names_[this_event.thread] = string_it->second;
807 } 901 }
808 } 902 }
809 903
810 } // namespace trace_analyzer 904 } // namespace trace_analyzer
811 905
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698