Setting up soldat and gefr

本文介绍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,内容为:

[repos:oss]
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

from gefr import Gefr

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!

soldat & gefr

我的这一套stack正在走向完整。上次贴了一张soldat-http的图,现在基于soldat的wsgi服务器也已经有了一个基本可以运行的实现,名字叫做gefr(我的命名出处参考这里)。现在gefr上已经可以跑基于bottle框架的wsgi程序了,也就是说一些基于python的web应用可能可以通过jython来运行在soldat上。为了搞定jython的环境,这几天我还花了不少时间做了jip帮我从maven仓库里自动下载依赖的jar包。

soldat和gefr的代码都放在我的bitbucket上:

此外,这两个项目也分别发布到了sonatype oss仓库python cheese shop

现在还有几个问题:

  • soldat在读buffer的时候先获得buffer的limit,再去读相应长度的buffer有时会出现BufferUnderflowException。这个可能存在线程安全问题,现在还没发现。
  • gefr启动之后可以通过在jvisualvm里找到这个进程,但是绑定profiler之后很诡异的是gefr就不再处理请求了。
  • 直接用soldat的处理http请求,吞吐量可以上万;但是在上面加上jython的gefr,再加上bottle框架,同样的功能吞吐量就剩下原来的十分之一了。就是因为没法做profile,所以还不知道时间花到哪里去了。

简单地 announce 一下,这样我有更多的动力来继续把这两个小东西做好。