summaryrefslogtreecommitdiffstats
path: root/tools/net/netlog.py
blob: 610052bbbd70045b1fdd1b03f717bb62c9f0ce31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
# Copyright (c) 2010 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Parse NetLog output and reformat it to display in Gnuplot."""

import math
import optparse
import os
import re
import sys


class Entry(object):
  """A single NetLog line/entry."""

  def __init__(self, line):
    self.id = -1
    self.info = ''
    self.time = 0

    time_offset = line.find('t=')
    if time_offset >= 0:
      tmp = line[time_offset + 2:].split(None, 1)
      self.time = tmp[0][:-3] + '.' + tmp[0][-3:]
      if len(tmp) > 1:
        self.info = tmp[1]
    else:
      self.info = line
    self.info = re.sub('\[dt=[0-9 ?]*\]', '', self.info)
    self.info = self.info.strip()


class Object(object):
  """A set of Entries that belong to the same NetLog Object."""

  def __init__(self, line, id_offset):
    self.id = line[id_offset + 4:].rstrip(')')
    self.type = line.split(None, 1)[0]
    self.entries = []

  def SpaceOutEntries(self):
    self.__FindGroups(self.__SpaceRange)

  def SetRotation(self):
    self.__FindGroups(self.__SetRotationRange)

  def __FindGroups(self, proc):
    first = 0
    for i in range(len(self.entries)):
      if self.entries[first].time != self.entries[i].time:
        proc(first, i - 1)
        first = i
    proc(first, len(self.entries) - 1)

  def __SpaceRange(self, start, end):
    if end > start:
      gap = math.floor(100/(end - start + 2))
      for i in range(start + 1, end + 1):
        self.entries[i].time += '%02d'%(gap * (i - start))

  def __SetRotationRange(self, start, end):
    for i in range(start, end + 1):
      self.entries[i].rotation = 85 - (i - start) * 15


def Parse(stream):
  """Parse the NetLog into python objects."""

  result = []
  request_section = 0
  obj = None
  entry = None

  for line in stream:
    line = line.strip()
    if line.startswith('Requests'):
      request_section = 1
    elif line.startswith('Http cache'):
      request_section = 0

    if request_section:
      id_offset = line.find('(id=')
      if id_offset >= 0:
        obj = Object(line, id_offset)
        result.append(obj)
      elif line.startswith('(P) t=') or line.startswith('t='):
        entry = Entry(line)
        obj.entries.append(entry)
      elif line.find('source_dependency') >= 0:
        new_entry = Entry(line)
        new_entry.time = entry.time
        new_entry.id = line.split(':')[1].split(',')[0]
        obj.entries.append(new_entry)
        new_entry = Entry('t=%s '%entry.time.replace('.', ''))
        obj.entries.append(new_entry)
      elif line.startswith('-->'):
        entry.info = entry.info + ' ' + line[4:]

  return result


def GnuplotRenderNetlog(netlog, filename, labels):
  """Render a list of NetLog objects into Gnuplot format."""

  output = open(filename, 'w')

  commands = []
  data = []
  types = []

  commands.append('file="%s"'%filename)
  commands.append('set key bottom')
  commands.append('set xdata time')
  commands.append('set timefmt "%s"')
  commands.append('set datafile separator ","')
  commands.append('set xlabel "Time (s)"')
  commands.append('set ylabel "Netlog ID"')

  plot = []
  index = 1
  for obj in netlog:
    if obj.type not in types:
      types.append(obj.type)
    type_num = types.index(obj.type)
    plot.append('file index %d using 1:2 with linespoints lt %d notitle'%(
        index, type_num))
    obj.SetRotation()
    for entry in obj.entries:
      graph_id = obj.id
      if entry.id > 0:
        graph_id = entry.id
      data.append('%s,%s'%(entry.time, graph_id))
      info = entry.info.replace('"', '')
      if info and labels:
        commands.append('set label "%s" at "%s", %s rotate by %d'%(
            info, entry.time, obj.id, entry.rotation))
    data.append('\n')
    index += 1

  for entry_type in types:
    plot.insert(0, '1/0 lt %d title "%s"'%(types.index(entry_type), entry_type))
  commands.append('plot ' + ','.join(plot))
  commands.append('exit')
  result = '\n'.join(commands) + '\n\n\n' + '\n'.join(data)

  output.write(result)
  output.close()
  os.system('gnuplot %s -'%filename)


def main(_):
  parser = optparse.OptionParser('usage: %prog [options] dump_file')
  parser.add_option_group(
      optparse.OptionGroup(parser, 'Additional Info',
                           'When run, the script will generate a file that can '
                           'be passed to gnuplot, but will also start gnuplot '
                           'for you; left click selects a zoom region, u '
                           'unzooms, middle click adds a marker, and q quits.'))
  parser.add_option('-o', '--output', dest='output', help='Output filename')
  parser.add_option('-l', '--labels', action='store_true', help='Output labels')
  options, args = parser.parse_args()
  if not args:
    parser.error('Must specify input dump_file.')
  if not options.output:
    options.output = args[0] + '.gnuplot'

  netlog = Parse(open(args[0]))
  GnuplotRenderNetlog(netlog, options.output, options.labels)
  return 0


if '__main__' == __name__:
  ret = main(sys.argv)
  sys.exit(ret)