Skip to content

Instantly share code, notes, and snippets.

@voluntas
Last active April 8, 2019 05:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voluntas/a05f50242e56cad03415 to your computer and use it in GitHub Desktop.
Save voluntas/a05f50242e56cad03415 to your computer and use it in GitHub Desktop.
PyPy コトハジメ

PyPy コトハジメ

更新

2015-03-14

作者

@voluntas

バージョン

0.0.3

URL

http://voluntas.github.io/

depth

3

概要

PyPy の本番での使用を目的とする人向けの資料

Python の高速な実装である PyPy を使用し Django を使ったウェブアプリの高速を行う。

この資料では PyPy への移行と検証を行うための知識をまとめておく。

PyPy の性能

性能に関してはこれを見てみるとよい、一通りの項目において CPython よりも早い。

PyPy's Speed Center

PyPy の Django に関してはこちら。

PyPy's Speed Center: Timeline

最近 2015-02 のベンチだと、 10 倍程度早い。

Programming Rants: C++, Java, HHVM, Ruby, NodeJS/Rhino/JSC/SpiderMonkey, Go, Scala, Python/PyPy String Conversion with CombSort Benchmark

引用:

$ go version
go version go1.4.1 linux/amd64
$ time go build scomb.go
CPU: 0.14s      Real: 0.17s     RAM: 30428KB
$ time ./scomb
CPU: 11.45s     Real: 11.54s    RAM: 251628KB

$ python --version
Python 3.4.2
$ time python scomb.py
CPU: 121.33s    Real: 122.06s   RAM: 641844KB

$ pypy --version
Python 2.7.8 (c6ad44ecf5d8, Nov 18 2014, 18:04:31) [PyPy 2.4.0 with GCC 4.9.2]
$ time pypy scomb.py
CPU: 14.72s     Real: 14.97s    RAM: 522080KB

構成

実際に検証を行う場合の構成であって、オススメの構成とかでは無い

ライブラリ

PyPy は C 拡張だと逆に遅くなる事があるので、Pure Python ライブラリを使用する

MySQL 接続性能

PyMySQL を使う

@methane が取ったベンチマークが参考になる

URL

https://gist.github.com/methane/b5813669e9dbd0c321bd

PyPy 2.5 (PyMySQL) と CPython (mysqlclient) ではほとんど差がなさそうなので、気にしなくて良いと思う。

JSON encode/decode 性能

PyPy 標準の json ライブラリを使う

URL

https://gist.github.com/voluntas/05482e373baad5d769eb

参考

http://morepypy.blogspot.jp/2011/10/speeding-up-json-encoding-in-pypy.html

簡単なベンチマークを取った。PyPy 内蔵 json が圧倒的に早い。

CPython
JSON Benchmark
2.7.9 (default, Dec 11 2014, 02:36:08)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
-----------------------------
ENCODING
simplejson: 0.281742s
cjson: 0.483193s
ujson: 0.21507s
jsonlib: 0.409993s
json: 0.732388s
yajl: 0.395648s

DECODING
simplejson: 0.540883s
cjson: 0.356611s
ujson: 0.215889s
jsonlib: 0.258306s
json: 0.35897s
yajl: 0.394774s

PyPy
JSON Benchmark
2.7.8 (10f1b29a2bd21f837090286174a9ca030b8680b2, Feb 05 2015, 07:09:36)
[PyPy 2.5.0 with GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)]
-----------------------------
ENCODING
json: 0.176226s
simplejson: 1.230522s

DECODING
json: 0.342462s
simplejson: 2.074411s

NumPy

URL

https://bitbucket.org/pypy/numpy.git

PyPy が提供している PyPy 用の NumPy を使う。

redis-py

未確定

URL

https://pypi.python.org/pypi/redis/

parser に hiredis (C 拡張) を使わないようにするのがいいのかどうか。

uWSGI

バージョン

2.1 (master)

uwsgi.ini

uWSGI の設定ファイル。Nginx との通信は Unix Domain Socket を使う。

後でサンプルっぽいのを作る

[uwsgi]
processes = 1
enable-threads = False
single-interpreter = True
projectname = 
projectdomain = 
base = 
uid = 
gid = 
chmod-socket = 666
chown-socket = 
chdir = %(base)
module = wsgi:application
master = True
pidfile = 
vacuum = True
env = 
socket = /tmp/uwsgi.sock
logto = 
pypy-home = 
pypy-wsgi-file = 
stats = /tmp/uwsgi_stats.socket

Nginx

バージョン

1.6.x

conrigure オプション

後で

nginx.conf

後で

メトリクス

NewRelic

AWS CloudWatch

The uWSGI Stats Server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment