Skip to content

Instantly share code, notes, and snippets.

@Qolt
Created May 19, 2013 13:30
Show Gist options
  • Save Qolt/5607628 to your computer and use it in GitHub Desktop.
Save Qolt/5607628 to your computer and use it in GitHub Desktop.
#!/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.0001
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
#print self.number, ": ", self.appear_time
class truck():
def __init__(self):
self.status = "free"
self.work_end_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
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()))
truck_number = random.randint(0 ,1)
if len(requests) != 0 and link_time_end < current_time.get_current_time():
if trucks[truck_number].status == "free":
trucks[truck_number].status = "busy"
trucks[truck_number].work_end_time = current_time.get_current_time() + 12 * minute + random.randint(-8 * minute, 8 * minute)
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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment