1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/base.html">
9<link rel="import" href="/tracing/base/iteration_helpers.html">
10<link rel="import" href="/tracing/base/statistics.html">
11
12<script>
13'use strict';
14tr.exportTo('tr.ui.analysis', function() {
15  function MultiEventSummary(title, events) {
16    this.title = title;
17    this.duration_ = undefined;
18    this.selfTime_ = undefined;
19    this.events_ = events;
20
21    this.cpuTimesComputed_ = false;
22    this.cpuSelfTime_ = undefined;
23    this.cpuDuration_ = undefined;
24
25    this.maxDuration_ = undefined;
26    this.maxCpuDuration_ = undefined;
27    this.maxSelfTime_ = undefined;
28    this.maxCpuSelfTime_ = undefined;
29
30    this.untotallableArgs_ = [];
31    this.totalledArgs_ = undefined;
32  };
33  MultiEventSummary.prototype = {
34
35    set title(title) {
36      if (title == 'Totals')
37        this.totalsRow = true;
38      this.title_ = title;
39    },
40
41    get title() {
42      return this.title_;
43    },
44
45    get duration() {
46      if (this.duration_ === undefined) {
47        this.duration_ = tr.b.Statistics.sum(
48            this.events_, function(event) {
49                return event.duration;
50            });
51      }
52      return this.duration_;
53    },
54
55    get cpuSelfTime() {
56      this.computeCpuTimesIfNeeded_();
57      return this.cpuSelfTime_;
58    },
59
60    get cpuDuration() {
61      this.computeCpuTimesIfNeeded_();
62      return this.cpuDuration_;
63    },
64
65    computeCpuTimesIfNeeded_: function() {
66      if (this.cpuTimesComputed_)
67        return;
68      this.cpuTimesComputed_ = true;
69
70      var cpuSelfTime = 0;
71      var cpuDuration = 0;
72      var hasCpuData = false;
73      for (var i = 0; i < this.events_.length; i++) {
74        var event = this.events_[i];
75        if (event.cpuDuration !== undefined) {
76          cpuDuration += event.cpuDuration;
77          hasCpuData = true;
78        }
79
80        if (event.cpuSelfTime !== undefined) {
81          cpuSelfTime += event.cpuSelfTime;
82          hasCpuData = true;
83        }
84      }
85      if (hasCpuData) {
86        this.cpuDuration_ = cpuDuration;
87        this.cpuSelfTime_ = cpuSelfTime;
88      }
89    },
90
91    get selfTime() {
92      if (this.selfTime_ === undefined) {
93        this.selfTime_ = 0;
94        for (var i = 0; i < this.events_.length; i++) {
95          if (this.events_[i].selfTime !== undefined)
96            this.selfTime_ += this.events[i].selfTime;
97        }
98      }
99      return this.selfTime_;
100    },
101
102    get events() {
103      return this.events_;
104    },
105
106    get numEvents() {
107      return this.events_.length;
108    },
109
110    get numAlerts() {
111      if (this.numAlerts_ === undefined) {
112        this.numAlerts_ = tr.b.Statistics.sum(this.events_, function(event) {
113          return event.associatedAlerts.length;
114        });
115      }
116      return this.numAlerts_;
117    },
118
119    get untotallableArgs() {
120      this.updateArgsIfNeeded_();
121      return this.untotallableArgs_;
122    },
123
124    get totalledArgs() {
125      this.updateArgsIfNeeded_();
126      return this.totalledArgs_;
127    },
128
129
130    get maxDuration() {
131      if (this.maxDuration_ === undefined) {
132        this.maxDuration_ = tr.b.Statistics.max(
133            this.events_, function(event) {
134                return event.duration;
135            });
136      }
137      return this.maxDuration_;
138    },
139
140
141    get maxCpuDuration() {
142      if (this.maxCpuDuration_ === undefined) {
143        this.maxCpuDuration_ = tr.b.Statistics.max(
144            this.events_, function(event) {
145                return event.cpuDuration;
146            });
147      }
148      return this.maxCpuDuration_;
149    },
150
151
152    get maxSelfTime() {
153      if (this.maxSelfTime_ === undefined) {
154        this.maxSelfTime_ = tr.b.Statistics.max(
155            this.events_, function(event) {
156                return event.selfTime;
157            });
158      }
159      return this.maxSelfTime_;
160    },
161
162
163    get maxCpuSelfTime() {
164      if (this.maxCpuSelfTime_ === undefined) {
165        this.maxCpuSelfTime_ = tr.b.Statistics.max(
166            this.events_, function(event) {
167                return event.cpuSelfTime;
168            });
169      }
170      return this.maxCpuSelfTime_;
171    },
172
173
174    updateArgsIfNeeded_: function() {
175      if (this.totalledArgs_ !== undefined)
176        return;
177
178      var untotallableArgs = {};
179      var totalledArgs = {};
180      for (var i = 0; i < this.events_.length; i++) {
181        var event = this.events_[i];
182        for (var argName in event.args) {
183          var argVal = event.args[argName];
184          var type = typeof argVal;
185          if (type !== 'number') {
186            untotallableArgs[argName] = true;
187            delete totalledArgs[argName];
188            continue;
189          }
190          if (untotallableArgs[argName]) {
191            continue;
192          }
193
194          if (totalledArgs[argName] === undefined)
195            totalledArgs[argName] = 0;
196          totalledArgs[argName] += argVal;
197        }
198      }
199      this.untotallableArgs_ = tr.b.dictionaryKeys(untotallableArgs);
200      this.totalledArgs_ = totalledArgs;
201    }
202  };
203
204  return {
205    MultiEventSummary: MultiEventSummary
206  };
207});
208</script>
209