显示标签为“CGI”的博文。显示所有博文
显示标签为“CGI”的博文。显示所有博文

星期六, 十二月 16, 2006

memcached

memcached homepage http://www.danga.com/memcached/
memcached for Win32 http://jehiah.cz/projects/memcached-win32/

Client APIS http://www.danga.com/memcached/apis.bml

Protocol
--------

Clients of memcached communicate with server through TCP connections.
(A UDP interface is also available; details are below under "UDP
protocol.") A given running memcached server listens on some
(configurable) port; clients connect to that port, send commands to
the server, read responses, and eventually close the connection.

There is no need to send any command to end the session. A client may
just close the connection at any moment it no longer needs it. Note,
however, that clients are encouraged to cache their connections rather
than reopen them every time they need to store or retrieve data. This
is because memcached is especially designed to work very efficiently
with a very large number (many hundreds, more than a thousand if
necessary) of open connections. Caching connections will eliminate the
overhead associated with establishing a TCP connection (the overhead
of preparing for a new connection on the server side is insignificant
compared to this).

There are two kinds of data sent in the memcache protocol: text lines
and unstructured data. Text lines are used for commands from clients
and responses from servers. Unstructured data is sent when a client
wants to store or retrieve data. The server will transmit back
unstructured data in exactly the same way it received it, as a byte
stream. The server doesn't care about byte order issues in
unstructured data and isn't aware of them. There are no limitations on
characters that may appear in unstructured data; however, the reader
of such data (either a client or a server) will always know, from a
preceding text line, the exact length of the data block being
transmitted.

Text lines are always terminated by \r\n. Unstructured data is _also_
terminated by \r\n, even though \r, \n or any other 8-bit characters
may also appear inside the data. Therefore, when a client retrieves
data from a server, it must use the length of the data block (which it
will be provided with) to determine where the data block ends, and not
the fact that \r\n follows the end of the data block, even though it
does.

Keys
----

Data stored by memcached is identified with the help of a key. A key
is a text string which should uniquely identify the data for clients
that are interested in storing and retrieving it. Currently the
length limit of a key is set at 250 characters (of course, normally
clients wouldn't need to use such long keys); the key must not include
control characters or whitespace.

Commands
--------

There are three types of commands.

Storage commands (there are three: "set", "add" and "replace") ask the
server to store some data identified by a key. The client sends a
command line, and then a data block; after that the client expects one
line of response, which will indicate success or faulure.

Retrieval commands (there is only one: "get") ask the server to
retrieve data corresponding to a set of keys (one or more keys in one
request). The client sends a command line, which includes all the
requested keys; after that for each item the server finds it sends to
the client one response line with information about the item, and one
data block with the item's data; this continues until the server
finished with the "END" response line.

All other commands don't involve unstructured data. In all of them,
the client sends one command line, and expects (depending on the
command) either one line of response, or several lines of response
ending with "END" on the last line.

A command line always starts with the name of the command, followed by
parameters (if any) delimited by whitespace. Command names are
lower-case and are case-sensitive.

Expiration times
----------------

Some commands involve a client sending some kind of expiration time
(relative to an item or to an operation requested by the client) to
the server. In all such cases, the actual value sent may either be
Unix time (number of seconds since January 1, 1970, as a 32-bit
value), or a number of seconds starting from current time. In the
latter case, this number of seconds may not exceed 60*60*24*30 (number
of seconds in 30 days); if the number sent by a client is larger than
that, the server will consider it to be real Unix time value rather
than an offset from current time.


Error strings
-------------

Each command sent by a client may be answered with an error string
from the server. These error strings come in three types:

- "ERROR\r\n"

means the client sent a nonexistent command name.

- "CLIENT_ERROR \r\n"

means some sort of client error in the input line, i.e. the input
doesn't conform to the protocol in some way. is a
human-readable error string.

- "SERVER_ERROR \r\n"

means some sort of server error prevents the server from carrying
out the command. is a human-readable error string. In cases
of severe server errors, which make it impossible to continue
serving the client (this shouldn't normally happen), the server will
close the connection after sending the error line. This is the only
case in which the server closes a connection to a client.


In the descriptions of individual commands below, these error lines
are not again specifically mentioned, but clients must allow for their
possibility.


Storage commands
----------------

First, the client sends a command line which looks like this:

\r\n

- is "set", "add" or "replace"

"set" means "store this data".

"add" means "store this data, but only if the server *doesn't* already
hold data for this key".

"replace" means "store this data, but only if the server *does*
already hold data for this key".

- is the key under which the client asks to store the data

- is an arbitrary 16-bit unsigned integer (written out in
decimal) that the server stores along with the data and sends back
when the item is retrieved. Clients may use this as a bit field to
store data-specific information; this field is opaque to the server.

- is expiration time. If it's 0, the item never expires
(although it may be deleted from the cache to make place for other
items). If it's non-zero (either Unix time or offset in seconds from
current time), it is guaranteed that clients will not be able to
retrieve this item after the expiration time arrives (measured by
server time).

- is the number of bytes in the data block to follow, *not*
including the delimiting \r\n. may be zero (in which case
it's followed by an empty data block).

After this line, the client sends the data block:

\r\n

- is a chunk of arbitrary 8-bit data of length
from the previous line.

After sending the command line and the data blockm the client awaits
the reply, which may be:

- "STORED\r\n", to indicate success.

- "NOT_STORED\r\n" to indicate the data was not stored, but not
because of an error. This normally means that either that the
condition for an "add" or a "replace" command wasn't met, or that the
item is in a delete queue (see the "delete" command below).


Retrieval command:
------------------

The retrieval command looks like this:

get *\r\n

- * means one or more key strings separated by whitespace.

After this command, the client expects zero or more items, each of
which is received as a text line followed by a data block. After all
the items have been transmitted, the server sends the string

"END\r\n"

to indicate the end of response.

Each item sent by the server looks like this:

VALUE \r\n
\r\n

- is the key for the item being sent

- is the flags value set by the storage command

- is the length of the data block to follow, *not* including
its delimiting \r\n

- is the data for this item.

If some of the keys appearing in a retrieval request are not sent back
by the server in the item list this means that the server does not
hold items with such keys (because they were never stored, or stored
but deleted to make space for more items, or expired, or explicitly
deleted by a client).



Deletion
--------

The command "delete" allows for explicit deletion of items:

delete

星期五, 十月 06, 2006

几种Apache CGI模块性能简单分析比较

最近对  mod_python, mod_php, mod_ruby, mod_lua做一个简单的性能比较,目的是想知道mod_lua在性能方面该如何改进!

工作在Windows 2003, VS 2005下完成,全部采用动态编译
-
mod_python已被捐赈给apache由apache关方维护,轻松搞定(支持python 2.4,不支持python2.5)
mod_php 一直被作为CGI开发的热点,也非常容易搞定
mod_lua 是本人的开源项目,自然不用碰到问题(支持最新lua 5.1.1)
mod_ruby 这个小日本的东西,在他上面花的时间最多, 首先与yarv(ruby 2.0)还不兼容,另外在ruby 1.8.5的编译时候需要在程序内部切换一个宏,另外还有个内存问题! 文档资料也很少.(mod_lua根本还没有文档,我以后会陆续给出)!需要一定的经验,不能自动完成.

( 就我对apache与ruby的了解程度自信可以搞定, 对于小日本开源软件的态度还真不敢恭维,ROR不知是被那位炒起来的, 反正mod_ruby的性能不敢恭维.)

---PHP的测试
页面
echo "Hello World!";
命令行
ab -n 100 http://localhost/tmp/hello.php

输出结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: ; &nbsp; 80

Document Path: &nbsp; ; /tmp/hello.php
Document Length: 12 bytes

Concurrency Level: &nbsp; &nbsp; 1

Time taken for tests: 0.609375 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 28700 bytes
HTML transferred: 1200 bytes
Requests per second: 164.10 [#/sec] (mean)
Time per request: 6.094 [ms] (mean)
Time per request: 6.094 [ms] (mean, across all concurrent requests)
Transfer rate: 45.95 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 4 7.2 0 15
Waiting: 0 4 7.1 0 15
Total: 0 5 7.2 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 15
75% 15
80% 15
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)

------mod_pythonde的测试
页面
from mod_python import apache

def handler(req):
req.content_type = 'text/plain'
req.write("Hello World!")
return apache.OK
命令行
ab -n 100 http://localhost/tmp/hello.py
输出结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.nutstech.com (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: www.nutstech.com
Server Port: 80

Document Path: /tmp/hello.py
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 0.359375 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 20500 bytes
HTML transferred: 1200 bytes
Requests per second: 278.26 [#/sec] (mean)
Time per request: 3.594 [ms] (mean)
Time per request: 3.594 [ms] (mean, across all concurrent requests)
Transfer rate: 55.65 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.6 0 15
Processing: 0 2 5.7 0 15
Waiting: 0 2 5.2 0 15
Total: 0 3 6.0 0 15

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 15
90% 15
95% 15
98% 15
99% 15
100% 15 (longest request)

-----mod_lua的测试
页面程序
http.print("Hello World!")
命令行
ab -n 100 http://localhost/tmp/hello.lua
测试结果
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.lua
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 0.468750 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24200 bytes
HTML transferred: 1200 bytes
Requests per second: 213.33 [#/sec] (mean)
Time per request: 4.688 [ms] (mean)
Time per request: 4.688 [ms] (mean, across all concurrent requests)
Transfer rate: 49.07 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.6 0 15
Processing: 0 3 6.6 0 31
Waiting: 0 2 6.0 0 15
Total: 0 3 6.9 0 31

Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 15
90% 15
95% 15
98% 15
99% 31
100% 31 (longest request)

----mod_ruby的测试
页面
#!/usr/bin/ruby

require "cgi"

cgi = CGI.new
print cgi.header("type"=>"text/plain")
print "Hello World!"


命令
ab -n 100 http://localhost/tmp/hello.rbx
测试结果
 This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.rbx
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 1.187500 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24300 bytes
HTML transferred: 1200 bytes
Requests per second: 84.21 [#/sec] (mean)
Time per request: 11.875 [ms] (mean)
Time per request: 11.875 [ms] (mean, across all concurrent requests)
Transfer rate: 19.37 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 10 9.1 15 46
Waiting: 0 9 9.1 15 46
Total: 0 10 9.1 15 46

Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 15
80% 15
90% 15
95% 31
98% 31
99% 46
100% 46 (longest request)

This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software: Apache/2.0.59
Server Hostname: localhost
Server Port: 80

Document Path: /tmp/hello.rbx
Document Length: 12 bytes

Concurrency Level: 1
Time taken for tests: 1.187500 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 24300 bytes
HTML transferred: 1200 bytes
Requests per second: 84.21 [#/sec] (mean)
Time per request: 11.875 [ms] (mean)
Time per request: 11.875 [ms] (mean, across all concurrent requests)
Transfer rate: 19.37 [Kbytes/sec] received


Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 2.1 0 15
Processing: 0 10 9.1 15 46
Waiting: 0 9 9.1 15 46
Total: 0 10 9.1 15 46

Percentage of the requests served within a certain time (ms)
50% 15
66% 15
75% 15
80% 15
90% 15
95% 31
98% 31
99% 46
100% 46 (longest request)



结果比较:


  • 模块名称 结果(次/秒)
  • mod_python 278.26
  • mod_lua 213.33
  • mod_php 164.10
  • mod_ruby 84.21
mod_python的处理速度果然快, 难怪google实用了很多的python, 不要犹豫, 用python吧!
当然php在WEB开发中的地位已经巩固, 但我相信很快就会被python赶上并超越
我开发的mod_lua,是为嵌入式应用准备的,在执行速度与模块大小方面都不错!且可直接用共享内存来通信 具体大家可以到 http://sf.net/projects/modlua 去了解!
mod_ruby是在是不敢恭维,如果采用eruby估计会更慢,将用ruby开发web的人,需要仔细考虑了