Skip to content

Instantly share code, notes, and snippets.

@odeblic
Created July 29, 2017 00:40
Show Gist options
  • Save odeblic/c34f44d3d25a75a73bacb06f36a65369 to your computer and use it in GitHub Desktop.
Save odeblic/c34f44d3d25a75a73bacb06f36a65369 to your computer and use it in GitHub Desktop.
Data sampling for latency measurement
import random
class Sampling(object):
def __init__(self, size_max):
self.__count = 0
self.__samples = list()
self.__size_max = size_max
def sample(self, value):
self.__count += 1
if self.__count < len(self.__samples) or len(self.__samples) < self.__size_max:
self.__samples.append(value)
else:
index = random.randint(0, self.__count - 1)
if index < len(self.__samples):
self.__samples[index] = value
def get_samples(self):
return self.__samples
def test(count):
sampling = Sampling(10)
values = list()
for val in range(0, count):
values.append(val)
sampling.sample(val)
samples = sampling.get_samples()
print('values={}'.format(values))
print('samples={}\n'.format(samples))
if __name__ == "__main__":
test(5)
test(10)
test(20)
test(100)
test(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment