File: perf.py

package info (click to toggle)
python-markdown2 2.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,104 kB
  • sloc: python: 5,416; perl: 1,493; php: 865; makefile: 34
file content (154 lines) | stat: -rwxr-xr-x 5,098 bytes parent folder | download
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
#!/usr/bin/env python

"""Run some performance numbers.  <cases-dir> is a directory with a
number of "*.text" files to process.

Example:
    python gen_perf_cases.py    # generate a couple cases dirs
    python perf.py tmp-test-cases
"""

import os
import sys
import time
from os.path import *
from glob import glob
import optparse

from util import hotshotit


clock = sys.platform == "win32" and time.clock or time.time


@hotshotit
def hotshot_markdown_py(cases_dir, repeat):
    time_markdown_py(cases_dir, repeat)

def time_markdown_py(cases_dir, repeat):
    sys.path.insert(0, join("..", "test"))
    import markdown
    del sys.path[0]
    markdowner = markdown.Markdown()
    times = []
    for i in range(repeat):
        start = clock()
        for path in glob(join(cases_dir, "*.text")):
            f = open(path, 'r')
            content = f.read()
            f.close()
            try:
                markdowner.convert(content)
                markdowner.reset()
            except UnicodeError:
                pass
        end = clock()
        times.append(end - start)
    print("  markdown.py: best of %d: %.3fs" % (repeat, min(times)))

@hotshotit
def hotshot_markdown2_py(cases_dir, repeat):
    time_markdown2_py(cases_dir, repeat)

def time_markdown2_py(cases_dir, repeat):
    sys.path.insert(0, "../lib")
    import markdown2
    del sys.path[0]
    markdowner = markdown2.Markdown()
    times = []
    for i in range(repeat):
        start = clock()
        for path in glob(join(cases_dir, "*.text")):
            f = open(path, 'r')
            content = f.read()
            f.close()
            markdowner.convert(content)
        end = clock()
        times.append(end - start)
    print("  markdown2.py: best of %d: %.3fs" % (repeat, min(times)))

def time_markdown_pl(cases_dir, repeat):
    times = []
    for i in range(repeat):
        start = clock()
        os.system('perl time_markdown_pl.pl "%s"' % cases_dir)
        end = clock()
        times.append(end - start)
    print("  Markdown.pl: best of %d: %.3fs" % (repeat, min(times)))

def time_all(cases_dir, repeat):
    time_markdown_pl(cases_dir, repeat=repeat)
    time_markdown_py(cases_dir, repeat=repeat)
    time_markdown2_py(cases_dir, repeat=repeat)

def time_not_markdown_py(cases_dir, repeat):
    time_markdown_pl(cases_dir, repeat=repeat)
    time_markdown2_py(cases_dir, repeat=repeat)


#---- mainline

class _NoReflowFormatter(optparse.IndentedHelpFormatter):
    """An optparse formatter that does NOT reflow the description."""
    def format_description(self, description):
        return description or ""

def main(args=sys.argv):
    usage = "python perf.py [-i all|markdown.py|markdown2.py|Markdown.pl] [cases-dir]"
    parser = optparse.OptionParser(prog="perf", usage=usage,
        description=__doc__, formatter=_NoReflowFormatter())
    parser.add_option("-r", "--repeat", type="int",
        help="number of times to repeat timing cycle (default 3 if timing, "
             "1 if profiling)")
    parser.add_option("-i", "--implementation",
        help="Markdown implementation(s) to run: all (default), "
             "markdown.py, markdown2.py, Markdown.pl, not-markdown.py")
    parser.add_option("--hotshot", "--profile", dest="hotshot",
        action="store_true",
        help="profile and dump stats about a single run (not supported "
             "for Markdown.pl)")
    parser.set_defaults(implementation="all", hotshot=False, repeat=None)
    opts, args = parser.parse_args()
 
    if len(args) != 1:
        sys.stderr.write("error: incorrect number of args\n")
        sys.stderr.write(__doc__)
        return 1
    cases_dir = args[0]
    if not exists(cases_dir):
        raise OSError("cases dir `%s' does not exist: use "
                      "gen_perf_cases.py to generate some cases dirs" 
                      % cases_dir)
    if opts.repeat is None:
        opts.repeat = opts.hotshot and 1 or 3

    if opts.hotshot:
        assert opts.implementation in ("markdown.py", "markdown2.py")
        timer_name = "hotshot_%s" \
            % opts.implementation.lower().replace('.', '_').replace('-', '_')
        d = sys.modules[__name__].__dict__
        if timer_name not in d:
            raise ValueError("no '%s' timer function" % timer_name)
        timer = d[timer_name]
        print("Profile conversion of %s (plat=%s):" \
              % (os.path.join(cases_dir, "*.text"), sys.platform))
        timer(cases_dir, repeat=opts.repeat)
        print()
        os.system("python show_stats.py %s.prof" % timer_name)

    else:
        timer_name = "time_%s" \
            % opts.implementation.lower().replace('.', '_').replace('-', '_')

        d = sys.modules[__name__].__dict__
        if timer_name not in d:
            raise ValueError("no '%s' timer function" % timer_name)
        timer = d[timer_name]
        print("Time conversion of %s (plat=%s):" \
              % (os.path.join(cases_dir, "*.text"), sys.platform))
        timer(cases_dir, repeat=opts.repeat)
    
if __name__ == "__main__":
    sys.exit( main(sys.argv) )