Created
February 12, 2015 07:51
-
-
Save zeapo/162f616bdfa1ac67f157 to your computer and use it in GitHub Desktop.
unique id test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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