Home | History | Annotate | Download | only in mappers
      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="/tracing/base/range.html">
      9 <link rel="import" href="/tracing/base/unit.html">
     10 <link rel="import" href="/tracing/mre/function_handle.html">
     11 <link rel="import" href="/tracing/value/histogram.html">
     12 
     13 <script>
     14 'use strict';
     15 
     16 tr.exportTo('pi.m', function() {
     17   var COUNT_BOUNDARIES = tr.v.HistogramBinBoundaries.createLinear(0, 5e4, 20);
     18 
     19   function traceStatsFunction(result, model) {
     20     var canonicalUrl = model.canonicalUrl;
     21     var eventCount = 0;
     22     var firstTime = Number.MAX_VALUE;
     23     var lastTime = 0;
     24     var categories = {};
     25 
     26     var seconds_counts = {};
     27     for (var event of model.getDescendantEvents()) {
     28       eventCount += 1;
     29       if (event.start < firstTime)
     30         firstTime = event.start;
     31 
     32       var eventEnd = event.start + event.duration;
     33       if (eventEnd > lastTime)
     34         lastTime = eventEnd;
     35 
     36       if (categories[event.category] === undefined)
     37         categories[event.category] = 0;
     38 
     39       categories[event.category]++;
     40 
     41       var second = Math.round(event.start / 1000);
     42       if (seconds_counts[second] === undefined)
     43         seconds_counts[second] = 0;
     44 
     45       seconds_counts[second]++;
     46     }
     47 
     48     var histogram = new tr.v.Histogram(
     49         tr.b.Unit.byName.count, COUNT_BOUNDARIES);
     50 
     51     for (var second in seconds_counts)
     52       histogram.add(seconds_counts[second]);
     53 
     54     var stats = {
     55       totalEvents: eventCount,
     56       firstTimeInMS: firstTime,
     57       lastTimeInMS: lastTime,
     58       durationInMS: lastTime - firstTime,
     59       eventsPerSecond: eventCount / (lastTime - firstTime) * 1000,
     60       categories: categories,
     61       events_seconds: histogram.asDict()
     62     };
     63 
     64     result.addPair('stats', stats);
     65   }
     66 
     67   tr.mre.FunctionRegistry.register(traceStatsFunction);
     68 
     69   //Exporting for tests.
     70   return {
     71     traceStatsFunctionForTest: traceStatsFunction
     72   };
     73 });
     74 </script>
     75