Skip to content

Instantly share code, notes, and snippets.

@adewes
Created July 29, 2013 15:07
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 adewes/6104997 to your computer and use it in GitHub Desktop.
Save adewes/6104997 to your computer and use it in GitHub Desktop.
Simple functions to calculate expected number of failures and workload in systems that can process a given number of subscribers in parallel, using a binomial distribution model to simulate subscriber behavior. Applicable e.g. to web servers & cellphone base stations.
from scipy.stats import binom
def workload(n_subscribers,n_capacity,p_call):
"""
Calculates the estimated workload (in percent) of a system to which n_subscribers connect at any given time with probability *p_call* and that can process *n_capacity* clients in parallel.
Arguments:
n_subscribers : The number of subscribers that might use the system at the given time.
n_capacity : The maximum number of subscribers that the system can process in parallel.
p_call : The probability that a subscriber will want to use the system at the given time.
Return value:
The expectation value of the workload of the system.
"""
workload = 0
n = 1.0
while n < n_subscribers:
if n < n_capacity:
weight = n/float(n_capacity)
else:
weight = 1.0
increment = weight*binom.pmf(n,n_subscribers,p_call)
workload+=increment
n+=1
return workload
def failures(n_subscribers,n_capacity,p_call):
"""
Calculates the expected number of processing failures in a system that can process *n_capacity* subscribers in parallel when *n_subscribers* will use it at a given moment with a probability *p_call*.
n_subscribers : The number of subscribers that might use the system at the given time.
n_capacity : The maximum number of subscribers that the system can process in parallel.
p_call : The probability that a subscriber will want to use the system at the given time.
Return value:
The expected number of processing failures (i.e. a subscriber wants to use the system but all slots are already filled).
"""
failures = 0
n = n_capacity+1.0
while n < n_subscribers:
increment = binom.pmf(n,n_subscribers,p_call)*(n-n_capacity)
failures+=increment
n+=1
return failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment