Exemple #1
0
    def generate_index(self, directory):
        index_file = os.path.join(directory, 'index.html')
        with open(index_file, 'w') as html:
            html.write(self.header(title='Profiler Report'))
            html.write('<h2>Usage by file</h2>')

            table = Table()
            table.add_column('Exclusive', style='data')
            table.add_column('Inclusive', style='data')
            table.add_column('File')

            paths = sorted(self.callees.keys(), key=self.get_file_time)
            paths.reverse()

            for path in paths:
                link = Report.output_file(path, 'html')
                table.add_row(
                    '%0.05f' % self.file_time_spent_exc[path],
                    '%0.05f' % self.file_time_spent_inc[path],
                    '<a href="%s">%s</a>' % (link, e(path)),
                )

            html.write(table.to_string())
            html.write(self.footer())
Exemple #2
0
    def generate_file(self, directory, path):
        out_file = os.path.join(directory, Report.output_file(path, 'html'))
        with open(out_file, 'w') as html:
            html.write(self.header(title='Page report: %s' % path))

            # Summary
            table = Table()
            table.add_column('Exclusive', style='data')
            table.add_column('Per call (exc)', style='data')
            table.add_column('Inclusive', style='data')
            table.add_column('Per call (inc)', style='data')
            table.add_column('Line', style='data')
            table.add_column('Callee')

            key_fn = lambda loc: self.call[loc].exclusive
            callees = sorted(self.callees[path], key=key_fn)
            callees.reverse()

            for callee in callees:
                call = self.call[callee]
                table.add_row(
                    '%f' % call.exclusive,
                    '%f' % call.exclusive_time_per_call,
                    '%f' % call.inclusive,
                    '%f' % call.inclusive_time_per_call,
                    '%d' % callee.line,
                    '<a href="#%d">%s</a>' % (callee.line, e(callee.func)),
                )

            html.write(table.to_string())

            # Code
            if os.path.isfile(path):
                html.write('<h2>Source</h2>')

                with open(path, 'r') as src:
                    table = Table()
                    table.add_column('Calls', style='data')
                    table.add_column('Exclusive time', style='data')
                    table.add_column('Per call (exc)', style='data')
                    table.add_column('Inclusive time', style='data')
                    table.add_column('Per call (inc)', style='data')
                    table.add_column('Line', style='data')
                    table.add_column('Source')

                    line_no = 1
                    for line in iter(src):
                        call = None

                        row = []
                        if path in self.call_at and line_no in self.call_at[path]:
                            call = self.call_at[path][line_no]
                            row = ['%d' % call.ncalls,
                                   '%f' % call.exclusive, '%f' % call.exclusive_time_per_call,
                                   '%f' % call.inclusive, '%f' % call.inclusive_time_per_call]
                        else:
                            row = ['&nbsp;'] * 5

                        row.append('%d' % line_no)
                        source = '<a name="%d" />%s' % (line_no, self.colorize(line))

                        if call is not None and call.has_callers:
                            callers = List(style='callers')
                            for call in call.callers:
                                link = Report.output_file(call.get_path(), 'html')
                                callers.add('called by <a href="%s#%d">%s</a>' % (
                                    link,
                                    call.line,
                                    e(call.func),
                                ))

                            source += callers.to_string()

                        if path in self.called_to and line_no in self.called_to[path]:
                            callees = List(style='callees')
                            loc = self.called_to[path][line_no]
                            link = Report.output_file(loc.get_path(), 'html')
                            callees.add('calls <a href="%s#%d">%s</a>' % (
                                    link,
                                    loc.line,
                                    e(loc.func),
                                ))
                            source += callees.to_string()

                        row.append(source)
                        table.add_row(*row)
                        line_no += 1

                    html.write(table.to_string())

            html.write(self.footer())