示例#1
0
 def toggleProfiling(self):
     import profiler
     if self.actions.profiling.isChecked():
         profiler.start()
         log.notice('profiling started')
     else:
         profiler.stop()
         log.notice('profiling stopped')
         mh.changeTask('Develop', 'Profile')
示例#2
0
 def toggleProfiling(self):
     import profiler
     if self.actions.profiling.isChecked():
         profiler.start()
         log.notice('profiling started')
     else:
         profiler.stop()
         log.notice('profiling stopped')
         mh.changeTask('Utilities', 'Profile')
示例#3
0
 def testProfiler(self):
     '''makes sure the profiler can be started and stopped and has the right value'''
     
     profiler.start("my name")
     time.sleep(1)
     d = profiler.stop()
     
     self.assertGreaterEqual(d.elapsed,1.00)
     self.assertEqual(d.name,"my name")
     self.assertEqual(d.name,"my name")
示例#4
0
    def flush(self, modeltype, donotcache=False):
        donotcache = donotcache or modeltype in self._donotcache
        self.assertismodelclass(modeltype)

        if modeltype not in self.queues:
            self.logger.debug(
                "Trying to flush a queue of %s that has never been filled before."
                % modeltype.__name__)
            return

        profiler.start('flush_' + modeltype.__name__)
        requireCloseSpider = False
        msg = ''
        success = True

        self.flush_active[modeltype] = True
        try:
            for deps in self.get_dependencies(
                    modeltype
            ):  #If we try to flush a model that is dependent on another, flush the dependenciy first.
                self.flush(deps)

            queue = self.queues[modeltype]
            if len(queue) > 0:
                chunksize = 100
                queue = self.exec_callbacks('before_flush', modeltype, queue)

                with db.proxy.atomic():
                    for idx in range(0, len(queue), chunksize):
                        queue_chunked = queue[idx:idx + chunksize]
                        data = list(
                            map(lambda x: (x._data), queue_chunked)
                        )  # Extract a list of dict from our Model queue
                        q = modeltype.insert_many(data)
                        updateablefields = {}
                        for fieldname in modeltype._meta.fields:
                            field = modeltype._meta.fields[fieldname]
                            if not isinstance(field, PrimaryKeyField):
                                updateablefields[fieldname] = field

                        try:
                            sql = self.add_onduplicate_key(
                                q, updateablefields
                            )  # Manually add "On duplicate key update"
                            db.proxy.execute_sql(sql[0], sql[1])

                        except Exception as e:  #We have a nasty error. Dumps useful data to a file.
                            filename = "%s_queuedump.txt" % (
                                modeltype.__name__)
                            msg = "%s : Flushing %s data failed. Dumping queue data to %s.\nError is %s." % (
                                self.__class__.__name__, modeltype.__name__,
                                filename, str(e))
                            self.logger.error("%s\n %s" %
                                              (msg, traceback.format_exc()))
                            self.dumpqueue(filename, queue)
                            success = False
                            requireCloseSpider = True

                if success:
                    #Hooks
                    self.exec_callbacks('after_flush', modeltype, queue)

                    #Stats
                    queueindex = modeltype
                    if queueindex in self.queuestats:
                        for spider in self.queuestats[queueindex]:

                            if spider not in self.stats:
                                self.stats[spider] = {}

                            if modeltype not in self.stats[spider]:
                                self.stats[spider][modeltype] = 0

                            self.stats[spider][modeltype] += self.queuestats[
                                queueindex][spider]  # consume stats for spider
                            self.queuestats[queueindex][
                                spider] = 0  # reset to 0

                    #cache
                    reloadeddata = None
                    if not donotcache or issubclass(modeltype,
                                                    BasePropertyOwnerModel):
                        self.cache.bulkwrite(queue)
                        reloadeddata = self.cache.reloadmodels(
                            queue, queue[0]._meta.primary_key
                        )  # Retrieve primary key (autoincrement id)
                    #Propkey/propval
                    if issubclass(
                            modeltype, BasePropertyOwnerModel
                    ):  # Our class has a property table defined (propkey/propval)
                        if reloadeddata and len(reloadeddata) > 0:
                            for obj in reloadeddata:
                                obj_spider = obj._extra_data['spider']
                                props = obj.getproperties()
                                for prop in props:
                                    self.enqueue(prop, obj_spider)

                            if not self.flush_active[modeltype._meta.valmodel]:
                                self.flush(modeltype._meta.valmodel,
                                           donotcache)  # Flush db properties

                        #Remove data from cache if explicitly asked not to cache. That'll save some memory
                        # We delete after inserting instead of simply preventing because we want BasePropertyOwnerModel
                        # object to successfully respect foreign key constraints with Auto Increment fields.
                        if donotcache:
                            profiler.start('dao_deleteobj')
                            self.cache.bulkdeleteobj(
                                queue
                            )  # Delete BasePropertyOwnerModel after provals are flushed
                            profiler.stop('dao_deleteobj')

            self.queues[modeltype] = []
            self.flush_active[modeltype] = False
            profiler.stop('flush_' + modeltype.__name__)
        except:
            self.flush_active[modeltype] = False
            raise

        if requireCloseSpider:
            raise CloseSpider(msg)
示例#5
0
 def testGeneratedName(self):
     '''makes sure a suitable name is generated if no specific name is passed into start()'''
     
     profiler.start()
     d = profiler.stop()
     self.assertEqual(d.name,self.id().rpartition('.')[-1])
示例#6
0
def mainGraphicsLoop():
    global frames, totalOrgs

    # Initialize the game engine
    pygame.init()

    orgsani = chaosapi.setup()
    organ = orgsani[0]
    for org in organ['organisms']:
        organismList.append(organism.Organism(org))
    totalOrgs = organ['stats']['orgsSpawnedSoFar']

    #set window peramiters and open the window
    size = (screenWidth, screenWidth)
    screen = pygame.display.set_mode(size)

    # Loop until the user clicks the close button.
    done = False

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    font = pygame.font.SysFont('Comic Sans MS', 15)

    # -------- Main Program Loop -----------
    while not done:

        frames += 1

        # --- Main event loop
        for event in pygame.event.get():  # User did something
            if event.type == pygame.QUIT:  # If user clicked close
                done = True  # Flag that we are done so we exit this loop
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    for org in organismList:
                        scoredOrgs.append(org)
                    organismList.clear()

        profiler.start('everything')

        screen.fill((255, 255, 255))

        for region in wall_regions:
            pygame.draw.rect(screen, region[5],
                             (region[0], region[1], region[2], region[3]))

        for scoreOrbs in scoreOrb.scoreOrbList:
            pygame.draw.circle(screen, scoreOrbs.color,
                               (scoreOrbs.x, scoreOrbs.y), scoreOrbs.radius)
            textsurface = font.render(str(scoreOrbs.score), False, (0, 0, 0))
            screen.blit(textsurface, (scoreOrbs.x, scoreOrbs.y))

        encoded = []
        if len(organismList) <= 0:
            for score in scoredOrgs:
                encoded.append(score.to_json())

            scoredOrgs.clear()
            organismList.clear()
            jsonMes = json.dumps({"report": encoded})
            newOrgs = chaosapi.reportOrgs('mechanist', 'finalRoom', orgsani[1],
                                          orgsani[2], jsonMes)
            totalOrgs = newOrgs['stats']['orgsSpawnedSoFar']
            for orgsi in newOrgs['organisms']:
                organismList.append(organism.Organism(orgsi))

        profiler.start('organisms')
        for org in organismList[:]:
            org.neuralNetwork.evaluate()

            color = (255, 0, 0)
            textsurface = font.render(str(org.trainingRoomNamespace), False,
                                      (255, 255, 255))
            orgAmount = font.render(
                str(totalOrgs) + " / 2000", False, (255, 255, 255))
            screen.blit(textsurface, (0, 0))
            screen.blit(orgAmount, (70, 0))

            if (frames - org.timeSinceLastScore) >= org.maxFrames:
                scoredOrgs.append(org)
                organismList.remove(org)
                continue

            for scoreOrbs in scoreOrb.scoreOrbList:
                scoreOrbs.intersects(org)

            # Read & handle outputs
            for output in org.neuralNetwork.outputs:

                if output.type == 'TurnOutput':
                    org.turnOutput(output._lastValue)
                if output.type == 'MoveOutput':
                    org.moveOutput(output._lastValue)
                if output.type == 'MoveSidewaysOutput':
                    org.sidwaysMoveOutput(output._lastValue)

            # Show the player's location before drawing eyes
            pygame.draw.circle(screen, org.color, (int(org.x), int(org.y)),
                               org.radius)

            view_x = org.x + math.cos(
                org.rotation * math.pi / 180) * org.radius * 2
            view_y = org.y - math.sin(
                org.rotation * math.pi / 180) * org.radius * 2
            pygame.draw.line(screen, (0, 0, 0), (org.x, org.y),
                             (view_x, view_y), 2)

            for color in org.neuralNetwork.colors:
                org.color = color.color
            # Render eyes
            for i in org.neuralNetwork.inputs:
                eyeValue = i.attributeValue
                eye = org.neuralNetwork.eyesDict[i.eyeId]

                eyeDirection = eye.direction + -org.rotation
                distance = 100 + org.radius
                distance_x = math.cos(eyeDirection * math.pi / 180) * distance
                distance_y = math.sin(eyeDirection * math.pi / 180) * distance
                x = org.x + distance_x
                y = org.y + distance_y

                trace_x = org.x + math.cos(
                    eyeDirection * math.pi / 180) * org.radius
                trace_y = org.y + math.sin(
                    eyeDirection * math.pi / 180) * org.radius
                trace_distance = distance - org.radius
                eye_to_wall = distance_to_wall(trace_x, trace_y, eyeDirection,
                                               trace_distance)
                eye_to_orb = distance_to_orb(trace_x, trace_y, eyeDirection,
                                             trace_distance)
                #While looking at nothing turn a weird purplish color
                if eye_to_wall[0] == -1 and eye_to_orb[0] == -1:
                    value = 0
                    color = (123, 57, 199)

                #While looking at a wall that is the same as the eyeValue turn green and change the value
                elif eyeValue == eye_to_wall[1]:
                    value = (trace_distance - eye_to_wall[0]) / trace_distance
                    color = (0, 255, 0)

                #While looking at an orb that is the same as the eyeValue turn cyan and change the value
                elif eyeValue == eye_to_orb[1]:
                    value = (trace_distance - eye_to_orb[0]) / trace_distance
                    color = (0, 255, 255)

                #While looking at something else turn yellow
                else:
                    value = 0
                    color = (123, 57, 199)
                eye.value = value

                pygame.draw.line(screen, color, (trace_x, trace_y), (x, y), 2)

                score = font.render(str(org.score), False, (0, 0, 0))
                screen.blit(score, (org.x, org.y))
        profiler.end()
        profiler.end()
        #update the screen
        pygame.display.flip()
 def process_spider_output(self, response, result, spider):
     profiler.start('shared_queue_process')
     for x in self.process_result(result, spider):
         yield x
     profiler.stop('shared_queue_process')