Commits

Andriy Kornatskyy  committed b135e70

Added caching benchmark for flask.

  • Participants
  • Parent commits 597c6c1

Comments (0)

Files changed (4)

File 07-caching/Makefile

-.SILENT: clean env pypy django flask pyramid web.py bottle wheezy.web tornado web2py bobo cherrypy wsgi circuits
-.PHONY: clean env pypy django flask pyramid web.py bottle wheezy.web tornado web2py bobo cherrypy wsgi circuits
+.SILENT: clean env pypy django flask wheezy.web
+.PHONY: clean env pypy django flask wheezy.web
 
 VERSION=2.7
 PYPI=http://pypi.python.org/simple
 		cd $(ENV)/bin && ./easy_install-$(VERSION) -i $(PYPI) -O2 \
 			"uwsgi>=1.2.6" "gunicorn>=0.14.6" \
 			"python-memcached>=1.48" "pylibmc>=1.2.3" \
-			"django>=1.4.2" "wheezy.web>=0.1.307" ; \
+			"django>=1.4.2" "wheezy.web>=0.1.307" "Flask-Cache>=0.9.0"; \
 	else \
 		cd $(ENV)/bin && ./easy_install-$(VERSION) -i $(PYPI) \
 				"distribute>=0.6.28" \
 	$(PYPY)/bin/pypy distribute_setup.py ; \
 	rm distribute* ; \
 	cd $(PYPY)/bin && ./easy_install -i $(PYPI) -O2 \
-		"gunicorn>=0.14.6" "python-memcached>=1.48" "wheezy.web>=0.1.307"
+		"gunicorn>=0.14.6" "python-memcached>=1.48" "wheezy.web>=0.1.307" \
+		"Flask-Cache>=0.9.0"
 
 clean:
 	find ./ -type d -name __pycache__ | xargs rm -rf
 	export PYTHONPATH=$$PYTHONPATH:wheezy.web ; \
 	$(ENV)/bin/gunicorn -b 0.0.0.0:8080 -w 4 app:main
 endif
+
+flask:
+ifeq ($(SERVER),uwsgi)
+	$(ENV)/bin/uwsgi --ini flask/uwsgi.ini
+else
+	export PYTHONPATH=$$PYTHONPATH:flask ; \
+	$(ENV)/bin/gunicorn -b 0.0.0.0:8080 -w 4 app:main
+endif

File 07-caching/benchmark.py

 path = os.getcwd()
 
 frameworks = ['wheezy.web']
-frameworks += ['django']
+frameworks += ['django', 'flask']
 frameworks = sorted(frameworks)
 
 

File 07-caching/flask/app.py

+from memcache import Client as MemcacheClient
+from pylibmc import Client as PylibmcClient
+from werkzeug.contrib.cache import MemcachedCache as MemcachedBase
+
+from flask import Flask
+from flask.ext.cache import Cache
+
+
+# cache backends
+
+class MemcacheCache(MemcachedBase):
+
+    def import_preferred_memcache_lib(self, servers):
+        return MemcacheClient(servers)
+
+
+class PylibmcCache(MemcachedBase):
+
+    def import_preferred_memcache_lib(self, servers):
+        return PylibmcClient(servers)
+
+
+def memcache(app, config, args, kwargs):
+    args.append(config['CACHE_MEMCACHED_SERVERS'])
+    return MemcacheCache(*args, **kwargs)
+
+
+def pylibmc(app, config, args, kwargs):
+    args.append(config['CACHE_MEMCACHED_SERVERS'])
+    return PylibmcCache(*args, **kwargs)
+
+
+# config
+
+main = Flask(__name__)
+
+memory_cache = Cache(main, with_jinja2_ext=False, config={
+    'CACHE_TYPE': 'simple'
+})
+
+
+pylibmc_cache = Cache(main, with_jinja2_ext=False, config={
+    'CACHE_TYPE': 'app.pylibmc',
+    'CACHE_MEMCACHED_SERVERS': ('/tmp/memcached.sock',)
+})
+
+
+memcache_cache = Cache(main, with_jinja2_ext=False, config={
+    'CACHE_TYPE': 'app.memcache',
+    'CACHE_MEMCACHED_SERVERS': ('unix:/tmp/memcached.sock',)
+})
+
+
+hello = ''.join(['%s. Hello World! ' % i for i in xrange(500)])
+
+
+# views
+
+@main.route('/welcome')
+def welcome():
+    return hello
+
+
+@main.route('/memory')
+@memory_cache.cached(timeout=60 * 15)
+def memory():
+    return hello
+
+
+@main.route('/pylibmc')
+@pylibmc_cache.cached(timeout=60 * 15)
+def pylibmc_view():
+    return hello
+
+
+@main.route('/memcache')
+@memcache_cache.cached(timeout=60 * 15)
+def memcache_view():
+    return hello

File 07-caching/flask/uwsgi.ini

+
+[uwsgi]
+chdir = flask
+virtualenv = ../env
+pythonpath = .
+wsgi = app:main
+procname-prefix = MyFlask-
+
+http-socket = 0.0.0.0:8080
+listen = 1024
+uid = www-data
+gid = www-data
+harakiri = 10
+optimize = 2
+master = True
+processes = 4
+disable-logging = True
+#logto = /dev/null
+no-default-app = False
+auto-procname = True
+limit-as = 90
+buffer-size = 2048
+post-buffering = 1024
+limit-post = 1024
+thread-stacksize = 64