OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <!-- | |
3 Copyright 2016 The Chromium Authors. All rights reserved. | |
4 Use of this source code is governed by a BSD-style license that can be | |
5 found in the LICENSE file. | |
6 --> | |
7 | |
8 <link rel="import" href="/tracing/value/diagnostics/related_histogram_map.html"> | |
9 | |
10 <script> | |
11 'use strict'; | |
12 | |
13 tr.exportTo('tr.v.d', function() { | |
14 const COLOR_SCHEME_CHROME_USER_FRIENDLY_CATEGORY_DRIVER = | |
15 'ChromeUserFriendlyCategory'; | |
16 | |
17 /** | |
18 * RelatedHistogramBreakdown encapsulates an additive relationship between | |
19 * Histograms: the Histogram that contains this RelatedHistogramBreakdown | |
20 * diagnostic is composed of the Histograms referenced by this | |
21 * RelatedHistogramBreakdown diagnostic. RelatedHistogramBreakdown is a | |
22 * "breakdown" of its containing Histogram into its contained Histograms. This | |
23 * additive relationship can apply to groups of other things besides Events, | |
24 * such as memory allocations. RelatedHistogramBreakdowns over groups of | |
25 * Events is expected to be the most common way of building | |
26 * RelatedHistogramBreakdowns, though it is not the only way. See | |
27 * buildFromEvents() for an example of how to build a | |
28 * RelatedHistogramBreakdown from an EventSet and a grouping function. | |
29 */ | |
30 class RelatedHistogramBreakdown extends tr.v.d.RelatedHistogramMap { | |
31 constructor() { | |
32 super(); | |
33 this.colorScheme = undefined; | |
34 } | |
35 | |
36 clone() { | |
37 const clone = new RelatedHistogramBreakdown(); | |
38 clone.colorScheme = this.colorScheme; | |
39 // RelatedHistogramMap.addDiagnostic() is no-op. | |
40 return clone; | |
41 } | |
42 | |
43 canAddDiagnostic(otherDiagnostic) { | |
44 return otherDiagnostic instanceof RelatedHistogramBreakdown && | |
45 otherDiagnostic.colorScheme === this.colorScheme; | |
46 } | |
47 | |
48 /** | |
49 * Add a Histogram by an explicit name to this map. | |
50 * | |
51 * @param {string} name | |
52 * @param {!(tr.v.d.HistogramRef|tr.v.Histogram)} hist | |
53 */ | |
54 set(name, hist) { | |
55 if (!(hist instanceof tr.v.d.HistogramRef)) { | |
56 if (!(hist instanceof tr.v.Histogram)) { | |
57 throw new Error( | |
58 'RelatedHistogramBreakdown can only contain Histograms'); | |
59 } | |
60 | |
61 if ((this.length > 0) && | |
62 (hist.unit !== | |
63 tr.b.getFirstElement(this)[1].unit)) { | |
64 throw new Error('Units mismatch', tr.b.getFirstElement(this)[1].unit, | |
65 hist.unit); | |
66 } | |
67 } | |
68 | |
69 tr.v.d.RelatedHistogramMap.prototype.set.call(this, name, hist); | |
70 } | |
71 | |
72 asDictInto_(d) { | |
73 tr.v.d.RelatedHistogramMap.prototype.asDictInto_.call(this, d); | |
74 if (this.colorScheme) d.colorScheme = this.colorScheme; | |
75 } | |
76 | |
77 static fromDict(d) { | |
78 const diagnostic = new RelatedHistogramBreakdown(); | |
79 for (const [name, guid] of Object.entries(d.values)) { | |
80 diagnostic.set(name, new tr.v.d.HistogramRef(guid)); | |
81 } | |
82 if (d.colorScheme) diagnostic.colorScheme = d.colorScheme; | |
83 return diagnostic; | |
84 } | |
85 | |
86 /** | |
87 * Build a RelatedHistogramBreakdown and its Histograms from |events|. Group | |
88 * events using |categoryForEvent|. Add the Histograms to |histograms|. | |
89 * Histograms' names are prefixed with |namePrefix|. Histograms are built | |
90 * with |opt_binBoundaries|. The numeric sample for each Event is derived | |
91 * from |opt_sampleForEvent|, which defaults to event.cpuSelfTime. The caller | |
92 * must add the result RelatedHistogramBreakdown to their Histogram's | |
93 * diagnostics. | |
94 * | |
95 * @param {!tr.v.HistogramSet} histograms | |
96 * @param {string} namePrefix | |
97 * @param {!tr.model.EventSet} events | |
98 * @param {!function(!tr.model.Event):string} categoryForEvent | |
99 * @param {!tr.b.Unit} unit | |
100 * @param {!function(!tr.model.Event):number=} opt_sampleForEvent | |
101 * @param {!tr.v.HistogramBinBoundaries=} opt_binBoundaries | |
102 * @param {*=} opt_this | |
103 * @return {!RelatedHistogramBreakdown} | |
104 */ | |
105 static buildFromEvents(histograms, namePrefix, events, categoryForEvent, | |
106 unit, opt_sampleForEvent, opt_binBoundaries, opt_this) { | |
107 const sampleForEvent = opt_sampleForEvent || | |
108 ((event) => event.cpuSelfTime); | |
109 | |
110 const diagnostic = new RelatedHistogramBreakdown(); | |
111 for (const event of events) { | |
112 const sample = sampleForEvent.call(opt_this, event); | |
113 if (sample === undefined) continue; | |
114 | |
115 const eventCategory = categoryForEvent.call(opt_this, event); | |
116 let hist = diagnostic.get(eventCategory); | |
117 if (hist === undefined) { | |
118 hist = new tr.v.Histogram( | |
119 namePrefix + eventCategory, unit, opt_binBoundaries); | |
120 histograms.addHistogram(hist); | |
121 diagnostic.set(eventCategory, hist); | |
122 } | |
123 | |
124 hist.addSample(sample, | |
125 {relatedEvents: new tr.v.d.RelatedEventSet([event])}); | |
126 } | |
127 return diagnostic; | |
128 } | |
129 } | |
130 | |
131 tr.v.d.Diagnostic.register(RelatedHistogramBreakdown, { | |
132 elementName: 'tr-v-ui-breakdown-span' | |
133 }); | |
134 | |
135 return { | |
136 COLOR_SCHEME_CHROME_USER_FRIENDLY_CATEGORY_DRIVER, | |
137 RelatedHistogramBreakdown, | |
138 }; | |
139 }); | |
140 </script> | |
OLD | NEW |