Source

Socrates / test / euclid / funtest.py

#!/usr/local/bin/spython
# -*- coding:utf-8 -*-

import timeit
import stackless
import pickle
import socrates.euclid.stacklesseuclid as engine
import random
import hashlib
import uuid
import logging
import datetime

"""
欧几里得引擎功能测试
目前版本仅支持stackless版本。
"""

filemd5s = []
lession_channel = stackless.channel()
__count__=0

euclid = engine.Euclid("filemd5")
#inc = lambda x:x+1

def inc(obj):
    if obj == None:
        return 1
    else:
        return obj+1

def testinc(md5, pool):
    pool.asyDoFunc("width", {"filemd5":md5}, inc, channel=lession_channel)

def lession():
    global __count__
    while True:
        lession_channel.receive()
        __count__+=1
        stackless.schedule()

def dotestinc(pool, channel):
    while True:
        md5 = channel.receive()
        doinc = stackless.tasklet(testinc(md5))
        data = pickle.dumps(doinc)
        pool.channel.send(data)
        stackless.schedule()

def init(pool):
    """
  数据初始化
    """
    for simulate_md5 in filemd5s:
        subject = dict(filemd5=simulate_md5,)
        pool.writeSubjects(subject)

def newSubject(pool, filemd5):
    subject = dict(filemd5=filemd5)
    pool.writeSubjectsTaskly(subject)

if __name__=="__main__":
    import sys

    howlong=int(sys.argv[1])
    #stackless.tasklet(init)(engine.datapool).run()
    #init(engine.datapool)

    channel = stackless.channel()
    stackless.tasklet(dotestinc)(euclid, channel).run()
    stackless.tasklet(lession)().run()
    #先初始化数据
    for line in sys.stdin:
        md5=line.strip()
        #newSubject(engine.datapool, md5)
        filemd5s.append(md5)
        euclid.writeSubjects({"filemd5":md5})

    #计时执行,统计时段内的访问次数
    start = datetime.datetime.now()
    print "%s 计时开始……"%start.strftime("%H:%M:%S")
    while (datetime.datetime.now() - start).seconds < howlong:
        testinc(random.choice(filemd5s), euclid)
    end = datetime.datetime.now()
    print "%s 停止发送请求"%end.strftime("%H:%M:%S")
    while lession_channel.balance > 0:
        print "等待 %s 项队列任务完成……"%lession_channel.balance
        stackless.schedule(100)
    #输出结果
    print datetime.datetime.now().strftime("%H:%M:%S")
    print "在 %s 条记录上进行了 %s 秒满负载访问模拟"%(len(filemd5s), howlong)
    print "完成 %s 次统计访问操作并得到了结果"%__count__
    print "约合每秒 %s 次请求"%((__count__)*1.0/howlong)