Reorganize.
This commit is contained in:
parent
3304d94255
commit
cd23b30e5f
|
@ -1,87 +1,102 @@
|
|||
# coding: utf-8
|
||||
# Source: https://github.com/lepture/mistune/blob/master/tests/bench.py
|
||||
|
||||
import os
|
||||
import time
|
||||
import os.path as path
|
||||
import functools
|
||||
|
||||
|
||||
modules = {}
|
||||
names = ('misaka', 'markdown', 'markdown2', 'cMarkdown', 'discount')
|
||||
for name in names:
|
||||
try:
|
||||
modules[name] = __import__(name)
|
||||
except ImportError:
|
||||
pass
|
||||
class benchmark(object):
|
||||
suites = []
|
||||
|
||||
|
||||
rndr = modules['misaka'].HtmlRenderer()
|
||||
m = modules['misaka'].Markdown(rndr)
|
||||
|
||||
|
||||
class Benchmark(object):
|
||||
def __init__(self, name):
|
||||
self._name = name
|
||||
|
||||
def __call__(self, func):
|
||||
def wrapper(*args, **kwargs):
|
||||
@functools.wraps(func)
|
||||
def wrapper(text, loops=1000):
|
||||
start = time.clock()
|
||||
func(*args, **kwargs)
|
||||
while loops:
|
||||
func(text)
|
||||
loops -= 1
|
||||
end = time.clock()
|
||||
return end - start
|
||||
wrapper.__name__ = func.__name__
|
||||
# register
|
||||
benchmark.suites.append((self._name, wrapper))
|
||||
return wrapper
|
||||
|
||||
@classmethod
|
||||
def bench(cls, text, loops=1000):
|
||||
print('Parsing the Markdown Syntax document %d times...' % loops)
|
||||
for name, func in cls.suites:
|
||||
try:
|
||||
total = func(text, loops=loops)
|
||||
print('{0}: {1}'.format(name, total))
|
||||
except ImportError:
|
||||
print('{0} is not available'.format(name))
|
||||
|
||||
@Benchmark('Misaka')
|
||||
|
||||
@benchmark('mistune')
|
||||
def benchmark_mistune(text):
|
||||
import mistune
|
||||
mistune.markdown(text)
|
||||
|
||||
|
||||
@benchmark('misaka')
|
||||
def benchmark_misaka(text):
|
||||
modules['misaka'].html(text)
|
||||
import misaka as m
|
||||
# mistune has all these features
|
||||
extensions = (
|
||||
m.EXT_NO_INTRA_EMPHASIS | m.EXT_FENCED_CODE | m.EXT_AUTOLINK |
|
||||
m.EXT_TABLES | m.EXT_STRIKETHROUGH
|
||||
)
|
||||
# md = m.Markdown(m.HtmlRenderer(), extensions=extensions)
|
||||
# md.render(text)
|
||||
|
||||
m.html(text, extensions)
|
||||
|
||||
|
||||
@Benchmark('Misaka (classes)')
|
||||
def benchmark_misaka_classes(text):
|
||||
m.render(text)
|
||||
# @benchmark('markdown2')
|
||||
# def benchmark_markdown2(text):
|
||||
# import markdown2
|
||||
# extras = ['code-friendly', 'fenced-code-blocks', 'footnotes']
|
||||
# markdown2.markdown(text, extras=extras)
|
||||
|
||||
|
||||
@Benchmark('markdown2')
|
||||
def benchmark_markdown2(text):
|
||||
modules['markdown2'].markdown(text)
|
||||
# @benchmark('markdown')
|
||||
# def benchmark_markdown(text):
|
||||
# import markdown
|
||||
# markdown.markdown(text, ['extra'])
|
||||
|
||||
|
||||
@Benchmark('Markdown')
|
||||
def benchmark_markdown(text):
|
||||
modules['markdown'].markdown(text)
|
||||
# @benchmark('cMarkdown')
|
||||
# def benchmark_cMarkdown(text):
|
||||
# import cMarkdown
|
||||
# cMarkdown.markdown(text)
|
||||
|
||||
|
||||
@Benchmark('cMarkdown')
|
||||
def benchmark_cMarkdown(text):
|
||||
modules['cMarkdown'].markdown(text)
|
||||
# @benchmark('discount')
|
||||
# def benchmark_discount(text):
|
||||
# import discount
|
||||
# discount.Markdown(text).get_html_content()
|
||||
|
||||
|
||||
@Benchmark('discount')
|
||||
def benchmark_discount(text):
|
||||
modules['discount'].Markdown(text).get_html_content()
|
||||
@benchmark('hoep')
|
||||
def benchmark_hoep(text):
|
||||
import hoep as m
|
||||
# mistune has all these features
|
||||
extensions = (
|
||||
m.EXT_NO_INTRA_EMPHASIS | m.EXT_FENCED_CODE | m.EXT_AUTOLINK |
|
||||
m.EXT_TABLES | m.EXT_STRIKETHROUGH | m.EXT_FOOTNOTES
|
||||
)
|
||||
md = m.Hoep(extensions=extensions)
|
||||
md.render(text.decode('utf-8'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
with open(path.join(path.dirname(__file__), 'markdown-syntax.md'), 'r') as fd:
|
||||
text = fd.read()
|
||||
root = os.path.dirname(__file__)
|
||||
filepath = os.path.join(root, 'markdown-syntax.md')
|
||||
with open(filepath, 'r') as f:
|
||||
text = f.read()
|
||||
|
||||
loops = 10000
|
||||
totals = []
|
||||
methods = [
|
||||
('Misaka', benchmark_misaka),
|
||||
('Misaka (classes)', benchmark_misaka_classes),
|
||||
('Markdown', benchmark_markdown),
|
||||
('Markdown2', benchmark_markdown2),
|
||||
('cMarkdown', benchmark_cMarkdown),
|
||||
('Discount', benchmark_discount)
|
||||
]
|
||||
|
||||
print('Parsing the Markdown Syntax document %d times...' % loops)
|
||||
|
||||
for i, method in enumerate(methods):
|
||||
name = method[1].__name__.split('_', 2)[1]
|
||||
if name not in modules:
|
||||
print('%s is not available' % method[0])
|
||||
continue
|
||||
total = 0
|
||||
for nth in range(0, loops):
|
||||
total += method[1](text)
|
||||
print('%s: %gs' % (method[0], total))
|
||||
benchmark.bench(text)
|
||||
|
|
22
build_ffi.py
22
build_ffi.py
|
@ -57,18 +57,18 @@ ffi.set_source(
|
|||
#include "hoedown/html.h"
|
||||
""",
|
||||
sources=(
|
||||
'src/extra.c',
|
||||
'src/hoedown/version.c',
|
||||
'src/hoedown/stack.c',
|
||||
'src/hoedown/html_smartypants.c',
|
||||
'src/hoedown/html_blocks.c',
|
||||
'src/hoedown/html.c',
|
||||
'src/hoedown/escape.c',
|
||||
'src/hoedown/document.c',
|
||||
'src/hoedown/buffer.c',
|
||||
'src/hoedown/autolink.c',
|
||||
'misaka/extra.c',
|
||||
'misaka/hoedown/version.c',
|
||||
'misaka/hoedown/stack.c',
|
||||
'misaka/hoedown/html_smartypants.c',
|
||||
'misaka/hoedown/html_blocks.c',
|
||||
'misaka/hoedown/html.c',
|
||||
'misaka/hoedown/escape.c',
|
||||
'misaka/hoedown/document.c',
|
||||
'misaka/hoedown/buffer.c',
|
||||
'misaka/hoedown/autolink.c',
|
||||
),
|
||||
include_dirs=('src',))
|
||||
include_dirs=('misaka',))
|
||||
|
||||
|
||||
# NOTE: The constants are refined here, because CFFI
|
||||
|
|
|
@ -19,8 +19,8 @@ MAX_NESTING = 16
|
|||
def html(text, extensions=0, render_flags=0):
|
||||
ib = lib.hoedown_buffer_new(IUNIT)
|
||||
ob = lib.hoedown_buffer_new(OUNIT)
|
||||
renderer = lib.hoedown_html_renderer_new(0, 0)
|
||||
document = lib.hoedown_document_new(renderer, 0, 16);
|
||||
renderer = lib.hoedown_html_renderer_new(render_flags, 0)
|
||||
document = lib.hoedown_document_new(renderer, extensions, 16);
|
||||
|
||||
lib.hoedown_buffer_puts(ib, text.encode('utf-8'))
|
||||
lib.hoedown_document_render(document, ob, ib.data, ib.size);
|
||||
|
|
Loading…
Reference in New Issue