Home | History | Annotate | Download | only in reducers
      1 <!DOCTYPE html>
      2 <!--
      3 Copyright 2015 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="/perf_insights/function_handle.html">
      9 <link rel="import" href="/tracing/base/range.html">
     10 <link rel="import" href="/tracing/model/source_info/js_source_info.html">
     11 <link rel="import" href="/tracing/value/numeric.html">
     12 <link rel="import" href="/tracing/value/unit.html">
     13 
     14 <script>
     15 'use strict';
     16 
     17 tr.exportTo('pi.r', function() {
     18   function ScriptCostInfo() {
     19     this.threadGroup = undefined;
     20     this.railTypeName = undefined;
     21     this.title = undefined;
     22     this.scriptURL = undefined;
     23     this.scriptURLClean = undefined;
     24     this.framework = undefined;
     25     this.userFriendlyCategory = undefined;
     26 
     27     this.selfTime = 0;
     28     this.cpuSelfTime = 0;
     29 
     30     this.jsTime = 0;
     31     this.jsTimeByState = {};
     32 
     33     this.traceURLs = {};
     34     this.sliceCount = 1;
     35 
     36     this.selfTimeHistogram = tr.v.Numeric.createLinear(
     37          tr.v.Unit.byName.timeDurationInMs,
     38          tr.b.Range.fromExplicitRange(0, 100),
     39          100);
     40 
     41     this.cpuSelfTimeHistogram = tr.v.Numeric.createLinear(
     42          tr.v.Unit.byName.timeDurationInMs,
     43          tr.b.Range.fromExplicitRange(0, 100),
     44          100);
     45   }
     46 
     47   ScriptCostInfo.asReduceTarget = function(firstValue) {
     48     var sliceCostInfo = new ScriptCostInfo();
     49     sliceCostInfo.threadGroup = firstValue.threadGroup;
     50     sliceCostInfo.railTypeName = firstValue.railTypeName;
     51     sliceCostInfo.title = firstValue.title;
     52     sliceCostInfo.scriptURL = firstValue.scriptURL;
     53     sliceCostInfo.scriptURLClean = firstValue.scriptURLClean;
     54     sliceCostInfo.framework = firstValue.framework;
     55     sliceCostInfo.userFriendlyCategory = firstValue.userFriendlyCategory;
     56     sliceCostInfo.traceURLs = {};
     57     if (firstValue.traceURL !== undefined) {
     58       sliceCostInfo.traceURLs[firstValue.traceURL] = true;
     59     }
     60     sliceCostInfo.jsTime = firstValue.jsTime || 0;
     61 
     62     var JSSourceState = tr.model.source_info.JSSourceState;
     63     for (var state in JSSourceState) {
     64       if (firstValue.jsTimeByState === undefined) {
     65         sliceCostInfo.jsTimeByState[JSSourceState[state]] = 0;
     66       } else {
     67         sliceCostInfo.jsTimeByState[JSSourceState[state]] =
     68             firstValue.jsTimeByState[JSSourceState[state]] || 0;
     69       }
     70     }
     71     return sliceCostInfo;
     72   };
     73 
     74   ScriptCostInfo.prototype = {
     75     push: function(threadSlice) {
     76       var JSSourceState = tr.model.source_info.JSSourceState;
     77       if (threadSlice.selfTime !== undefined)
     78         this.selfTime += threadSlice.selfTime;
     79       if (threadSlice.cpuSelfTime !== undefined)
     80         this.cpuSelfTime += threadSlice.cpuSelfTime;
     81       if (threadSlice.jsTime !== undefined)
     82         this.jsTime += threadSlice.jsTime;
     83       if (threadSlice.jsTimeByState !== undefined) {
     84         for (var state in JSSourceState) {
     85           this.jsTimeByState[JSSourceState[state]] +=
     86               threadSlice.jsTimeByState[JSSourceState[state]];
     87         }
     88       }
     89 
     90       if (threadSlice.traceURL !== undefined &&
     91           !threadSlice.traceURL in this.traceURLs) {
     92         this.traceURLs[threadSlice.traceURL] = true;
     93       }
     94 
     95       var sourceInfo = {
     96         traceURL: threadSlice.traceURL,
     97         sourceURL: threadSlice.scriptURLClean
     98       };
     99 
    100       this.selfTimeHistogram.add(threadSlice.selfTime, sourceInfo);
    101       this.cpuSelfTimeHistogram.add(threadSlice.cpuSelfTime, sourceInfo);
    102       this.sliceCount += 1;
    103     }
    104   };
    105 
    106   function v8ReportReduceFunction(key, mapResults) {
    107     var reduceResults = {};
    108     mapResults[key].forEach(function(mapResult) {
    109       var reducingTarget = reduceResults[mapResult.key];
    110       if (!reducingTarget) {
    111         reducingTarget = ScriptCostInfo.asReduceTarget(mapResult.value);
    112         reduceResults[mapResult.key] = reducingTarget;
    113       }
    114       reducingTarget.push(mapResult.value);
    115     });
    116 
    117     //console.log(JSON.stringify(reduceResults, undefined, 2));
    118     return reduceResults;
    119   };
    120 
    121   pi.FunctionRegistry.register(v8ReportReduceFunction);
    122 
    123   return {
    124     v8ReportReduceFunction: v8ReportReduceFunction
    125   };
    126 });
    127 
    128