Skip to content

Instantly share code, notes, and snippets.

@zeapo
Created February 12, 2015 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeapo/162f616bdfa1ac67f157 to your computer and use it in GitHub Desktop.
Save zeapo/162f616bdfa1ac67f157 to your computer and use it in GitHub Desktop.
unique id test
import time
import random
import base64
import threading
from matplotlib import pyplot
from sets import Set
def run_job(nb_rand):
ids = Set()
nb_threads = 20
nb_ids_by_thread = 30
def gen_unique_id():
timestamp = int(round(time.time()) * 1000)
rand = int(random.SystemRandom().random() * nb_rand)
basis = timestamp + rand
return base64.b64encode("%012d"%(basis))
def worker():
for i in range(nb_ids_by_thread):
ids.add(gen_unique_id().replace('==', ''))
for i in range(nb_threads):
t = threading.Thread(None, worker, 'T%d'%i, [], {})
t.start()
t.join()
print "[%e] Len(ids) = %d and should be %d"%(nb_rand, len(ids), nb_threads * nb_ids_by_thread)
return (len(ids), nb_threads * nb_ids_by_thread)
st = 1e1
while st < 10e12:
run_job(st)
st *= 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment