friendlessstar 发表于 2017-5-6 09:09:08

python web开发几个模板系统的性能对比

  对比目标,jinja2,cheetah,mako,webpy,bottle,tornado,django的性能。
  方法,随机生成一个二维数组,第一列是自增数据,第二列是长度为100的随机字符串,然后生成html,比较一次生成的时间。
  说明,如果模板有编译缓存,打开。有其他方法加速,打开。生成缓存,关闭。不计算随机数据生成时间,一次生成后一直使用。
  以下是文件有效内容,没用的都略去了。最后的顺序是因为我根据结果整理了一下调用次序。
  —–testcheetah.tmpl—–
  #for $i in $l
  #end for
  $i
  $i
  —–testdjango.html—–
  {% for i in l %}
  {% endfor %}
  {{ i.0 }}
  {{ i.1 }}
  —–testjinja2.html—–
  {% for i in l %}
  {% endfor %}
  {{ i }}
  {{ i }}
  —–testmako.html—–
  % for i in l:
  % endfor
  ${i}
  ${i}
  —–testwebpy.html—–
  $def with(l)
  $for i in l:
  $i
  $i
  —–tmpl.py—–
  #!/usr/bin/python
  # -﹡- coding: utf-8 -﹡-
  ”’
  @date: 2011-11-03
  @author: shell.xu
  ”’
  import os, random, string, timeit
  testdata = []
  def init_testdata():
  for i in xrange(1000):
  s = ”.join()
  testdata.append((i, s))
  init_testdata()
  # ——–webpy——–
  import web
  render = web.template.render(‘./’)
  def render_webpy():
  return render.testwebpy(testdata)
  # ——–jinja2——–
  from jinja2 import Environment, FileSystemLoader, FileSystemBytecodeCache
  env = Environment(loader = FileSystemLoader(‘./’),
  bytecode_cache = FileSystemBytecodeCache(‘./’, ‘%s.cache’))
  tmpl_jinja = env.get_template(‘testjinja2.html’)
  def render_jinja2():
  return tmpl_jinja.render(l = testdata)
  # ——–cheetah——–
  from testcheetah import testcheetah
  def render_cheetah():
  return testcheetah(searchList = [{'l': testdata},])
  # ——–mako——–
  from mako.template import Template as makotmpl
  tmpl_mako = makotmpl(filename = ‘./testmako.html’)
  def render_mako():
  return tmpl_mako.render(l = testdata)
  # ——–django——–
  from django.template import Template as djangotmpl
  from django.template import Context
  from django.conf import settings
  settings.configure()
  with open(‘testdjango.html’, ‘r’) as fi: tmpl_django = djangotmpl(fi.read())
  def render_django():
  return tmpl_django.render(Context({‘l': testdata}))
  # ——–bottle——–
  from bottle import SimpleTemplate
  with open(‘testbottle.html’, ‘r’) as fi: tmpl_bottle = SimpleTemplate(fi.read())
  def render_bottle():
  return tmpl_bottle.render(l = testdata)
  # ——–tornado——–
  from tornado import template as tornado_tmpl
  with open(‘testtornado.html’, ‘r’) as fi: tmpl_tornado = tornado_tmpl.Template(fi.read())
  def render_tornado():
  return tmpl_tornado.generate(l = testdata)
  def testfunc(funcname, times = 10000):
  from timeit import Timer
  t = Timer(“%s()” % funcname, “from __main__ import ﹡”)
  print ‘funcname: %s used %f’ % (funcname, t.timeit(times) / times)
  if __name__ == ‘__main__':
  testfunc(‘render_django’, times = 1000)
  testfunc(‘render_webpy’, times = 1000)
  testfunc(‘render_bottle’, times = 10000)
  testfunc(‘render_tornado’, times = 10000)
  testfunc(‘render_jinja2′, times = 10000)
  testfunc(‘render_mako’, times = 10000)
  testfunc(‘render_cheetah’, times = 100000)
  以下是运行结果。
  funcname: render_django used 0.071762
  funcname: render_webpy used 0.015729
  funcname: render_bottle used 0.008752
  funcname: render_tornado used 0.005675
  funcname: render_jinja2 used 0.002073
  funcname: render_mako used 0.001627
  funcname: render_cheetah used 0.000014
  点评一下吧。django就是个渣,不多废话了。webpy的代码很简洁,可惜速度太慢了。bottle看起来快一点,不过也没有多出彩。 tornado本身速度很快,不过模板——也就是如此吧。真的值得一用的,只有jinja2,mako,cheetah三个。速度都小于了5ms,单核每 秒可以生成200个页面,16核机器上大概就能跑到3000req/s,性能比较高。jinja2的速度比较折衷,配置灵活,语法类似django是他的 优点。而且不得不说,jinja2的文档真的很不错。mako的速度比jinja2略快,模板写起来也很舒服。文档略凌乱,可以接受。cheetah的速 度——已经不像是模板了好吧。
  这个东西是使用编译器将模板编译为py文件,然后再通过python编译为pyc,从而获得如此高的性能的。如果python可以执行加速(例如 psyco, pypy什么的),相信速度还要快。但是不得不说,语法实在是太严格了一点。我在for前面多了一个空格,居然直接报错,而且还是一个无关错误。找起问题 来相当困难。不过,对于习惯了python格式的格式控来说,cheetah还是有相当价值的。cheetah加速后的速度,单核上每秒可以生成7W多个 页面,16核的普通服务器,每秒可以承载100W req/s。看在效率的份上,我可以原谅他大多数的问题。
页: [1]
查看完整版本: python web开发几个模板系统的性能对比