Guest User

Untitled

a guest
Jan 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. import redis
  4. import random
  5. import pylibmc
  6. import sys
  7.  
  8. r = redis.Redis(host = 'localhost', port = 6389)
  9. mc = pylibmc.Client(['localhost:11222'])
  10.  
  11. REDIS_SETGET = False
  12. REDIS_HSET = False
  13. MC = False
  14.  
  15. NUM_ENTRIES = 1000000
  16. MAX_VAL = 12000000
  17.  
  18. if len(sys.argv) != 2 or sys.argv[1] not in ('redis-normal', 'redis-hashes', 'memcached'):
  19. print 'Specify a test: redis-normal, redis-hashes, memcached'
  20. print 'NOTE: clear out memcached (restart) or Redis (FLUSHALL) before running'
  21. sys.exit(2)
  22. if sys.argv[1] == 'redis-normal':
  23. REDIS_SETGET = True
  24. elif sys.argv[1] == 'redis-hashes':
  25. REDIS_HSET = True
  26. elif sys.argv[1] == 'memcached':
  27. MC = True
  28.  
  29. p = r.pipeline()
  30. for i in range(0, NUM_ENTRIES):
  31. value = random.randint(0, MAX_VAL)
  32. if MC:
  33. mc.set(str(i), value)
  34. elif REDIS_SETGET:
  35. r.set(str(i), value)
  36. elif REDIS_HSET:
  37. bucket = int(i / 500)
  38. p.hset(bucket, i, value)
  39.  
  40. if i % (NUM_ENTRIES/10) == 0:
  41. if REDIS_SETGET or REDIS_HSET:
  42. p.execute()
  43. p = r.pipeline()
  44. print i
  45.  
  46. # one final clear out
  47. if REDIS_SETGET or REDIS_HSET:
  48. p.execute()
  49.  
  50. # get size
  51. if MC:
  52. size = int(mc.get_stats()[0][1]['bytes'])
  53. elif (REDIS_SETGET or REDIS_HSET):
  54. size = int(r.info()['used_memory'])
  55.  
  56. print '%s bytes, %s MB' % (size, size / 1024 / 1024)
Add Comment
Please, Sign In to add comment