Skip to content

Instantly share code, notes, and snippets.

@azat
Forked from Qolt/modeling.py
Last active December 17, 2015 13:09
Show Gist options
  • Save azat/5614822 to your computer and use it in GitHub Desktop.
Save azat/5614822 to your computer and use it in GitHub Desktop.
azat:/tmp$ wget https://gist.github.com/Qolt/5614809/raw/5d2d29292cb3fff8243202973f5db936117a6e1e/modeling.py
--2013-05-20 23:32:56-- https://gist.github.com/Qolt/5614809/raw/5d2d29292cb3fff8243202973f5db936117a6e1e/modeling.py
Resolving gist.github.com (gist.github.com)... 204.232.175.94
Connecting to gist.github.com (gist.github.com)|204.232.175.94|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: ‘modeling.py’
[ <=> ] 3,538 --.-K/s in 0s
2013-05-20 23:32:57 (149 MB/s) - ‘modeling.py’ saved [3538]
azat:/tmp$ chmod +x modeling.py
azat:/tmp$ ./modeling.py
Start time: 1369078383.72
^CTraceback (most recent call last):
File "./modeling.py", line 48, in <module>
if current_time.get_current_time() >= last_request_time + new_request_time:
File "./modeling.py", line 19, in get_current_time
time_delta = (time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f')))- self.start_time
KeyboardInterrupt
azat:/tmp$ ./modeling.py >> out.txt
^CTraceback (most recent call last):
File "./modeling.py", line 48, in <module>
if current_time.get_current_time() >= last_request_time + new_request_time:
File "./modeling.py", line 19, in get_current_time
time_delta = (time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f')))- self.start_time
KeyboardInterrupt
azat:/tmp$ cat out.txt
Start time: 1369078388.76
azat:/tmp$ ./modeling.py >> out.txt^C
azat:/tmp$ for i in ${1..20}; do ./modeling.py >> out.txt; done
bash: ${1..20}: bad substitution
azat:/tmp$ for i in {1..20}; do ./modeling.py >> out.txt; done
^Z
[1]+ Stopped ./modeling.py >> out.txt
azat:/tmp [1]$ bg
[1]+ ./modeling.py >> out.txt &
azat:/tmp [1]$ tail out.txt
Start time: 1369078388.76
Start time: 1369078422.7
Done 89 requests.
Total 108 requests.
Truck # 0 done 41 requests. Spent 29762 sec at work. Load: 82.6722222222
Truck # 1 done 44 requests. Spent 29543 sec at work. Load: 82.0638888889
[1]+ Done ./modeling.py >> out.txt
azat:/tmp$ tail out.txt
Start time: 1369078388.76
Start time: 1369078422.7
Done 89 requests.
Total 108 requests.
Truck # 0 done 41 requests. Spent 29762 sec at work. Load: 82.6722222222
Truck # 1 done 44 requests. Spent 29543 sec at work. Load: 82.0638888889
azat:/tmp$
#!/usr/bin/python
import time
import optparse
from datetime import datetime, timedelta
import string
import random
import sys
minute = 60
hour = 60 * minute
time_scale = 0.0002
class time_emulator():
def __init__(self):
self.start_time = time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f'))
print "Start time: " + str(self.start_time)
def get_current_time(self):
time_delta = (time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f')))- self.start_time
current_time = int(time_delta / time_scale)
return current_time
class request():
def __init__(self, number, appear_time):
self.number = number
self.appear_time = appear_time
class truck():
def __init__(self):
self.status = "free"
self.work_end_time = 0
self.jobs_counter = 0
self.work_time = 0
if __name__ == "__main__":
requests = []
trucks = [truck(), truck()]
requests_counter = 0
total_requests_counter = 0
link_time_end = 0
current_time = time_emulator()
new_request_time = 5 * minute + random.randint(-4 * minute, 4 * minute)
last_request_time = 0
current_request = 0
truck_number = random.randint(0 ,1)
while (current_time.get_current_time() <= 10 * hour):
if current_time.get_current_time() >= last_request_time + new_request_time:
new_request_time = 5 * minute + random.randint(-4 * minute, 4 * minute)
last_request_time = current_time.get_current_time()
total_requests_counter += 1
if len(requests) < 5:
requests_counter +=1
requests.append(request(requests_counter, current_time.get_current_time()))
if requests != []:
if current_request == requests[0].number:
if truck_number == 1:
truck_number = 0
else:
truck_number = 1
else:
truck_number = random.randint(0 ,1)
current_request = requests[0].number
if requests != [] and link_time_end < current_time.get_current_time():
if trucks[truck_number].status == "free":
trucks[truck_number].status = "busy"
new_job_time = 12 * minute + random.randint(-8 * minute, 8 * minute)
trucks[truck_number].work_end_time = current_time.get_current_time() + new_job_time
trucks[truck_number].work_time += new_job_time
trucks[truck_number].jobs_counter += 1
link_time_end = current_time.get_current_time() + minute
del requests[0]
#print "Truck #", truck_number, " Work start at", current_time.get_current_time()," | end at ", trucks[truck_number].work_end_time
if trucks[truck_number].status == "busy":
link_time_end = current_time.get_current_time() + minute
for truck in trucks:
if current_time.get_current_time() > truck.work_end_time:
truck.status = "free"
print "Done ", requests_counter, " requests."
print "Total ", total_requests_counter, " requests."
for i, truck in enumerate(trucks):
print "Truck #",i ,"done ", truck.jobs_counter, " requests. Spent ", truck.work_time, " sec at work.", "Load: ", ((truck.work_time / (10.0 * hour)) * 100.0)
azat:/tmp$ rm out.txt; i=0; while [ "$i" != 3 ]; do python ./modeling.py 1>| out.txt; i=$(( $i + 1 )); done;
azat:/tmp$ wc -l out.txt
5 out.txt
azat:/tmp$ head out.txt
Start time: 1369080169.6
Done 87 requests.
Total 121 requests.
Truck # 0 done 38 requests. Spent 31630 sec at work. Load: 87.8611111111
Truck # 1 done 45 requests. Spent 32024 sec at work. Load: 88.9555555556
azat:/tmp$ rm out.txt
azat:/tmp$ i=0; while [ $i != 20 ]; do ./modeling.py >> out.txt; i=$(( $i + 1 )); done;
^Z
[1]+ Stopped ./modeling.py >> out.txt
azat:/tmp [1]$ bg
[1]+ ./modeling.py >> out.txt &
azat:/tmp [1]$ tail out.txt
azat:/tmp [1]$ fg
./modeling.py >> out.txt
^CTraceback (most recent call last):
File "./modeling.py", line 47, in <module>
while (current_time.get_current_time() <= 10 * hour):
File "./modeling.py", line 19, in get_current_time
time_delta = (time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f')))- self.start_time
KeyboardInterrupt
azat:/tmp$ ^C
azat:/tmp$ i=0; while [ $i != 20 ]; do (./modeling.py >> out.txt); i=$(( $i + 1 )); done;
^Z
[1]+ Stopped ( ./modeling.py >> out.txt )
azat:/tmp [1]$ bg
[1]+ ( ./modeling.py >> out.txt ) &
azat:/tmp [1]$ tail out.txt
Start time: 1369079291.13
azat:/tmp [1]$ tail out.txt
Start time: 1369079291.13
azat:/tmp [1]$ tail out.txt
Start time: 1369079291.13
azat:/tmp [1]$ tail -0f out.txt
tail: inotify resources exhausted
tail: inotify cannot be used, reverting to polling
^C
[1]+ Done ( ./modeling.py >> out.txt )
azat:/tmp$ tail out.txt
Start time: 1369079291.13
Start time: 1369079306.28
Done 58 requests.
Total 75 requests.
Truck # 0 done 26 requests. Spent 19672 sec at work. Load: 54.6444444444
Truck # 1 done 27 requests. Spent 19512 sec at work. Load: 54.2
azat:/tmp$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment