def main(): t1 = time.time() Threads = [] # 创建线程 for i in range(THREAD_NUM): t = threading.Thread(target=working, name="T"+str(i)) t.setDaemon(True) Threads.append(t) for t in Threads: t.start() for t in Threads: t.join() logger.info("main thread end") t2 = time.time() print "========================================" print "URL:", PERF_TEST_URL print "任务数量:", THREAD_NUM, "*", ONE_WORKER_NUM, "=", THREAD_NUM*ONE_WORKER_NUM print "总耗时(秒):", t2-t1 print "每次请求耗时(秒):", (t2-t1) / (THREAD_NUM*ONE_WORKER_NUM) print "每秒承载请求数:", 1 / ((t2-t1) / (THREAD_NUM*ONE_WORKER_NUM)) print "错误数量:", ERROR_NUM
def fetch_sql(sql=None, res=False, *args): # 这个方法写的真是难用 """ 这个方法感觉真的很难用 :param res: 返回结果形态, True 返回内容,False 返回受影响行 :param sql: :param args: :return: """ if not sql: logger.error("sql is nil") return [] try: con = DB.get_connect() cur = con.cursor() sql_log = sql % args row = cur.execute(sql, args) # 预编译防止sql 注入 con.commit() logger.info('sql: '+sql_log) con.close() if not res: # cur.lastrowid return row else: return list(cur.fetchall()) except Exception, e: sql_log = sql % args logger.error('sql: '+sql_log+', error: '+str(e)) return []
def working(): t = threading.currentThread() logger.info("[{}] Sub Thread Begin".format(t.name)) i = 0 while i < ONE_WORKER_NUM: i += 1 doWork(i) sleep(LOOP_SLEEP) logger.info("[{}] Sub Thread End".format(t.name))
con.close() if not res: # cur.lastrowid return row else: return list(cur.fetchall()) except Exception, e: sql_log = sql % args logger.error('sql: '+sql_log+', error: '+str(e)) return [] def return_dict(key, value): """ two list to dict :param key: :param value: :return: list(dict, dict) """ try: if len(key) == len(value) and False in [isinstance(i, tuple) for i in value] and False in [isinstance(i, list) for i in value]: return dict(zip(key, value)) else: return [dict(zip(key, r)) for r in value] except Exception, e: logger.error("error,"+str(e)+" Dao input key value is not equal, please check Dao : "+str(key)+str(value)) return {} if __name__ == "__main__": logger.info(return_dict(["name", "age"], [["whnzy", 16], ["haining", 22]])) logger.info(return_dict(["brand_name", "id"], [("whnzy", 16), ("haining", 22)]))