redis_test_with_lvs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # coding: utf-8
  2. import redis
  3. import time
  4. import threading
  5. redis_master = '192.168.1.82'
  6. redis_slave = '192.168.5.150'
  7. redis_real_ip_list = ['192.168.5.22','192.168.5.23','192.168.5.24']
  8. def tester(count_num):
  9. d = {}
  10. def rbserver(count_num):
  11. for x in range(count_num):
  12. slave_conn = redis.StrictRedis(host=redis_slave, port=6379, db=0)
  13. slave_id = slave_conn.info()['run_id']
  14. if not d.get(slave_id):
  15. d[slave_id] = 1
  16. else:
  17. d[slave_id] += 1
  18. def theadmanager(func,*host):
  19. thread_list = []
  20. start = time.clock()
  21. for x in range(5):
  22. if host:
  23. thread = threading.Thread(target=func,args = (count_num / 5, host[0]))
  24. else:
  25. thread = threading.Thread(target=func,args = (count_num / 5,))
  26. thread_list.append(thread)
  27. for thread in thread_list:
  28. thread.start()
  29. for thread in thread_list:
  30. thread.join()
  31. return time.clock() - start
  32. def singeserver(count_num,host):
  33. for x in range(count_num):
  34. conn = redis.StrictRedis(host=host, port=6379, db=0)
  35. slave_id = conn.info()['run_id']
  36. print 'load banlance:',theadmanager(rbserver)
  37. time.sleep(1)
  38. print 'single server:', theadmanager(singeserver,'192.168.5.22')
  39. tester(2000)