def run_simulation(self):
        start_time = time.time()
        self.start_nodes()
        start_time = time.time() - start_time
        print 'Starting network with {0} nodes took: {1} secods'\
              .format(len(self.nodes),start_time)
  
        watcher_monitor = WatcherMonitor(self.limit)
        #Launch a thread which watches the output of the engines
        watcher_thread = Watcher(self.process_dict,self.config,watcher_monitor,self.persistent_table_dict)
        watcher_thread.start()
        
        #Do pre inputs in parallel
        self.input_pre()
       
        #Set the topology
        self.set_topology()

        self.start_evaluations()

        #Do post inputs
        self.input_post()

        time.sleep(2) #MAY NOT be necessary
        while not watcher_monitor.converged() and\
              not watcher_monitor.hit_limit():
            pass

        if watcher_monitor.hit_limit():
            print "HIT LIMIT"
        else:
            print "CONVERGENCE REACHED"
               
        self.node_states = watcher_thread.stop()
        self.lost_list, self.nr_received, self.nr_sent = lamport_transformation(self.node_states)

        f = open('output','w')
        for node in self.node_states:
            f.write("Instance {0} states".format(node))
            f.write("================================")
            for state in self.node_states[node]:
                f.write(state.__str__())
        f.close()

        self.make_xml()
        f = open(self.output,'w')
        f.write(self.result)
        f.close()
Пример #2
0
    def run_simulation(self):
        start_time = time.time()
        self.start_nodes()
        node_start_time = time.time() - start_time
        print 'Starting network with {0} nodes took: {1} secods'\
              .format(len(self.nodes),node_start_time)
  
        watcher_monitor = WatcherMonitor(self.limit)
        #Launch a thread which watches the output of the engines
        watcher_thread = Watcher(self.process_dict,self.config,watcher_monitor,self.pers_table_dict,self.tran_table_dict)
        watcher_thread.start()
        
        #Do pre inputs in parallel
        self.input_pre()
       
        #Set the topology
        if not self.hardcoded_topology:
            self.set_topology()

            self.start_evaluations()

        #Do post inputs
        self.input_post()

        time.sleep(2) #MAY NOT be necessary
        while not watcher_monitor.converged() and\
              not watcher_monitor.hit_limit():
            pass

        if watcher_monitor.hit_limit():
            print "HIT LIMIT"
            self.hit_limit = True
        else:
            print "CONVERGENCE REACHED"
            self.hit_limit = False

        self.evaluations = watcher_monitor.evaluations
        self.node_states = watcher_thread.stop()
        lamport_start = time.time()
        self.lost_list, self.nr_received, self.nr_sent = lamport_transformation(self.node_states)
        lamport_duration = time.time() - lamport_start
        print "Lamport took {0} seconds".format(lamport_duration)
        try:
            percentage_lost = float((self.nr_sent - self.nr_received))/self.nr_sent
        except ZeroDivisionError:
            percentage_lost = 0

        f = open('output','w')
        for node in self.node_states:
            f.write("Instance {0} states".format(node))
            f.write("================================")
            for state in self.node_states[node]:
                f.write(state.__str__())
        f.close()

        write_to_file_start = time.time()
        self.simulation_time = time.time() - start_time
        self.make_xml()
        f = open(self.output,'w')
        f.write(self.result)
        f.close()
        write_duration = time.time() - write_to_file_start
        print "Write to file: {0}s".format(write_duration)

        size_of_file = os.path.getsize(self.output)

        time_taken = time.time() - start_time
        print "Took {0} seconds to run the whole simulation".format(time_taken)
        print start_time
        print time.time()
        

        time_taken = int(time_taken)
        percentage_lost = round(percentage_lost * 100,2)
        f = open('experiment_data', 'a')
        to_write = '{0} & {1} & {2} & {3}\n'.format(self.evaluations, time_taken, self.nr_sent, percentage_lost)
#to_write = 'STATE TRANSITIONS: {0},TOTAL TIME: {6}, SENT MESSAGES: {1}, PERCENTAGE LOST: {2}, LAMPORT TIME: {3}, WRITE TIME: {4}, WRITE SIZE: {5}\n'.format(self.evaluations, self.nr_sent, percentage_lost, lamport_duration, write_duration, size_of_file, time_taken)
        f.write(to_write)
        f.close()