本文介绍soldat服务器和gefr WSGI适配器的环境搭建,以及jip的基本使用。
安装python工具
virtualenv和pip是python开发的关键工具
sudo apt-get install python-virtualenv
sudo apt-get install python-pip
jython需要您手动下载安装。推荐安装到/usr/local/下,并建立软连接到/usr/local/bin/中,下文将假设您是这么做的。
创建虚拟jython环境
virtualenv -p /usr/local/bin/jython gefr-test
cd gefr-test
source bin/activate
安装jip
pip install jip
配置jip
在$HOME下创建文件.jip,内容为:
uri=http://oss.sonatype.org/content/repositories/snapshots/
type=remote
[repos:central]
uri=http://repo1.maven.org/maven2/
type=remote
[repos:local]
uri=/home/sun/.m2/repository/
type=local
安装soldat
配置完jip后,可以使用jip来安装soldat
jip install info.sunng.soldat:soldat:1.0-SNAPSHOT
文件将被下载到 javalib 目录中,您可以检查安装的正确性:
$ ls javalib/
log4j-1.2.16.jar slf4j-log4j12-1.6.1.jar
slf4j-api-1.6.1.jar soldat-1.0-SNAPSHOT.jar
安装gefr
pip install gefr==0.1dev2
创建一个简单的Python WSGI程序
创建test.py
def wsgiapp(environ, start_response):
status = '200 OK'
res_body = "<html><head><title>Welcome</title></head><body><h1>It works!</h1></body></html>"
res_headers = [('Content-Type', 'text/html'),
('Content-Length', str(len(res_body)))]
start_response(status, res_headers)
return [res_body]
Gefr(wsgiapp, host='0.0.0.0', port=8000).start()
启动服务
使用jip附带的jython-all
jython-all test.py
打开浏览器,访问 http://localhost:8000/
用ab测试服务性能
$ ab -n 10000 -c 100 http://localhost:8000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: gefr/0.1dev
Server Hostname: localhost
Server Port: 8000
Document Path: /
Document Length: 79 bytes
Concurrency Level: 100
Time taken for tests: 2.539 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1640000 bytes
HTML transferred: 790000 bytes
Requests per second: 3938.11 [#/sec] (mean)
Time per request: 25.393 [ms] (mean)
Time per request: 0.254 [ms] (mean, across all concurrent requests)
Transfer rate: 630.71 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.6 0 8
Processing: 9 25 22.4 21 239
Waiting: 9 25 22.4 21 239
Total: 13 25 22.3 21 239
Percentage of the requests served within a certain time (ms)
50% 21
66% 22
75% 22
80% 23
90% 26
95% 40
98% 65
99% 233
100% 239 (longest request)
Good Luck!