Esempio n. 1
0
def test_greenlet_timer():
    gevent = pytest.importorskip('gevent', '1')
    from profiling.timers.greenlet import GreenletTimer

    def light():
        factorial(10)
        gevent.sleep(0.1)
        factorial(10)

    def heavy():
        factorial(10000)

    def profile(profiler):
        with profiling(profiler):
            gevent.spawn(light).join(0)
            gevent.spawn(heavy)
            gevent.wait()
        stat1 = find_stat(profiler.stats, 'light')
        stat2 = find_stat(profiler.stats, 'heavy')
        return (stat1, stat2)

    # using default timer.
    # light() ends later than heavy().  its total time includes heavy's also.
    normal_profiler = Profiler(top_frame=sys._getframe())
    stat1, stat2 = profile(normal_profiler)
    assert stat1.total_time >= stat2.total_time
    # using greenlet timer.
    # light() ends later than heavy() like the above case.  but the total time
    # doesn't include heavy's.  each greenlets have isolated cpu time.
    greenlet_profiler = Profiler(GreenletTimer(), top_frame=sys._getframe())
    stat1, stat2 = profile(greenlet_profiler)
    assert stat1.total_time < stat2.total_time
Esempio n. 2
0
def _test_contextual_timer(timer, sleep, spawn, join=lambda x: x.join()):
    def light():
        factorial(10)
        sleep(0.1)
        factorial(10)

    def heavy():
        factorial(10000)

    def profile(profiler):
        with profiling(profiler):
            c1 = spawn(light)
            c2 = spawn(heavy)
            for c in [c1, c2]:
                join(c)
        stat1 = find_stat(profiler.stats, 'light')
        stat2 = find_stat(profiler.stats, 'heavy')
        return (stat1, stat2)

    # using the default timer.
    # light() ends later than heavy().  its total time includes heavy's also.
    normal_profiler = Profiler(top_frame=sys._getframe())
    stat1, stat2 = profile(normal_profiler)
    assert stat1.total_time >= stat2.total_time
    # using the given timer.
    # light() ends later than heavy() like the above case.  but the total time
    # doesn't include heavy's.  each contexts should have isolated cpu time.
    contextual_profiler = Profiler(timer, top_frame=sys._getframe())
    stat1, stat2 = profile(contextual_profiler)
    assert stat1.total_time < stat2.total_time
Esempio n. 3
0
class FaceRecognizer(AbstractRecognizer):
    def __init__(self):
        AbstractRecognizer.__init__(self)

        self._name = "[FaceRecognizer]"
        self._profiler = Profiler("[Profiler:Main]")

    def recognize(self, cb, args=None):
        recognized_name = None

        if args is None:
            EventLogger.warning(self._name + " did not get any args!")
            cb("NO NAME")

        if len(args) != 2:
            EventLogger.error("Please start this Program with the parameter 1 or 0")
            cb("NO NAME")

        if args[1] == 1:  # ------------------------------------------------| Create_Database_Mode
            EventLogger.info("Start Mode 1: Create Face Database")
            fdb = gfd.GenerateFaceDatabase("Roland")
            fdb.generate_face_database()
            cb("NO NAME")

        elif args[1] == 2:  # ------------------------------------------------| Face_Recognition_Mode
            EventLogger.info("Start Mode 2: Face Recognition")
            fr_instance = fr.faceRecognition(cb)
            fr_instance.start_process()

        else:  # -----------------------------------------------------------------| Face_Detection_Mode
            EventLogger.info("Start Mode default: Detecting Faces")
            start_new_thread(face_detection_webcam, (self.__found_face,))
            cb("NO NAME")


    def __found_face(self, frame, faces):
        '''
        This function is just for debugging purpose

        :param frame:
        :param faces:
        :return:
        '''
        if frame is None or faces is None:
            print "found_face: one param is None"

        max = len(faces)
        print "Found " + str(len(faces)) + " images"

        # process the faces
        for i, val in enumerate(faces):
            rnd = str(randint(0, 1000000))
            util.save_image("./image_" + str(i) + "_" + str(rnd) + ".pgm", val)

        #start_new_thread(self.__profile_process, (self.__callback_profile_process, "Marv", ))

    def cb_recognize(self, recognized_name):
        print "callbacked: " + str(recognized_name)
        self._profiler.start_profile_routine(recognized_name, False)
Esempio n. 4
0
def test_profile():
    profiler = Profiler()
    frame = foo()
    profiler._profile(frame, 'call', None)
    profiler._profile(frame, 'return', None)
    assert len(profiler.stats) == 1
    stat1 = find_stat(profiler.stats, 'foo')
    stat2 = find_stat(profiler.stats, 'bar')
    stat3 = find_stat(profiler.stats, 'baz')
    assert stat1.calls == 0
    assert stat2.calls == 0
    assert stat3.calls == 1
Esempio n. 5
0
def test_frame_stack():
    def to_code_names(frames):
        code_names = deque()
        for frame in reversed(frames):
            code_name = frame.f_code.co_name
            if code_name not in mock_code_names:
                break
            code_names.appendleft(code_name)
        return list(code_names)
    profiler = Profiler()
    frame = foo()
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['foo', 'bar', 'baz']
    # top frame
    profiler = Profiler(top_frame=frame.f_back)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
    # top code
    profiler = Profiler(top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
    # both of top frame and top code
    profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
Esempio n. 6
0
def test_profiler():
    profiler = Profiler(top_frame=sys._getframe())
    assert isinstance(profiler.stats, RecordingStatistics)
    assert isinstance(profiler.result(), FrozenStatistics)
    assert len(profiler.stats) == 0
    with profiling(profiler):
        factorial(1000)
        factorial(10000)
    stat1 = find_stat(profiler.stats, 'factorial')
    stat2 = find_stat(profiler.stats, '__enter__')
    stat3 = find_stat(profiler.stats, '__exit__')
    assert stat1.total_time != 0
    assert stat1.total_time == stat1.own_time
    assert stat1.own_time > stat2.own_time
    assert stat1.own_time > stat3.own_time
    assert stat1.calls == 2
    assert stat2.calls == 0  # entering to __enter__() wasn't profiled.
    assert stat3.calls == 1
Esempio n. 7
0
def test_profiler():
    profiler = Profiler(top_frame=sys._getframe())
    assert isinstance(profiler.stats, RecordingStatistics)
    assert isinstance(profiler.result(), FrozenStatistics)
    assert len(profiler.stats) == 0
    with profiling(profiler):
        factorial(1000)
        factorial(10000)
    stat1 = find_stat(profiler.stats, 'factorial')
    stat2 = find_stat(profiler.stats, '__enter__')
    stat3 = find_stat(profiler.stats, '__exit__')
    assert stat1.total_time != 0
    assert stat1.total_time == stat1.own_time
    assert stat1.own_time > stat2.own_time
    assert stat1.own_time > stat3.own_time
    assert stat1.calls == 2
    assert stat2.calls == 0  # entering to __enter__() wasn't profiled.
    assert stat3.calls == 1
Esempio n. 8
0
def test_setprofile():
    profiler = Profiler()
    assert sys.getprofile() is None
    profiler.start()
    assert sys.getprofile() == profiler._profile
    profiler.stop()
    assert sys.getprofile() is None
    sys.setprofile(lambda *x: x)
    with pytest.raises(RuntimeError):
        profiler.start()
    sys.setprofile(None)
Esempio n. 9
0
    def __init__(self):
        AbstractRecognizer.__init__(self)

        self._name = "[NfcRfidRecognizer]"
        self._profiler = Profiler("[Profiler-NfcRfidRecognizer]")
        # self._gui._nfc_init()

        self._counter = 0
        self._exit_timer = None
        self._exit_cb = None
Esempio n. 10
0
def test_setprofile():
    profiler = Profiler()
    assert sys.getprofile() is None
    profiler.start()
    assert sys.getprofile() == profiler._profile
    profiler.stop()
    assert sys.getprofile() is None
    sys.setprofile(lambda *x: x)
    with pytest.raises(RuntimeError):
        profiler.start()
    sys.setprofile(None)
Esempio n. 11
0
    def handle(self, *args, **kwargs):
        p = Profiler()

        print('Creating users')
        users = autofixture.create('auth.User', 20, overwrite_defaults=True)
        user = users[0]
        p.authorise_with(user)

        print('Creating regions')
        autofixture.create('geo.Region',
                           5,
                           field_values={
                               'created_by': user,
                           })

        print('Creating projects')
        Project.objects.all().delete()
        autofixture.create_one('project.Project',
                               field_values={
                                   'created_by': user,
                               })
        project = Project.objects.first()
        if not ProjectMembership.objects.filter(project=project, member=user)\
                .exists():
            ProjectMembership.objects.create(project=project,
                                             member=user,
                                             role='admin')

        print('Creating leads')
        # create_many_leads(1000, user, project)
        autofixture.create('lead.Lead',
                           100,
                           field_values={
                               'created_by': user,
                           })

        print('Starting profiling')
        p.profile_get('/api/v1/leads/?'
                      'status=pending&'
                      'published_on__lt=2016-01-10&'
                      'assignee={0}&'
                      'search=lorem&'
                      'limit=100&'
                      ''.format(users[2].id))

        p.__del__()
Esempio n. 12
0
def test_profile():
    profiler = Profiler()
    frame = mock_stacked_frame(map(mock_code, ['foo', 'bar', 'baz']))
    profiler._profile(frame, 'call', None)
    profiler._profile(frame, 'return', None)
    assert len(profiler.stats) == 1
    stat1 = find_stat(profiler.stats, 'baz')
    stat2 = find_stat(profiler.stats, 'bar')
    stat3 = find_stat(profiler.stats, 'foo')
    assert stat1.calls == 0
    assert stat2.calls == 0
    assert stat3.calls == 1
Esempio n. 13
0
def test_frame_stack():
    profiler = Profiler()
    frame = sys._getframe()
    frame_stack = profiler._frame_stack(frame)
    assert frame_stack[-1] is frame
    assert frame_stack[-2] is frame.f_back
    assert frame_stack[-3] is frame.f_back.f_back
    # top frame
    profiler = Profiler(top_frame=frame.f_back)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
    # top code
    profiler = Profiler(top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
    # both of top frame and top code
    profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
Esempio n. 14
0
def test_profile():
    profiler = Profiler()
    frame = foo()
    profiler._profile(frame, 'call', None)
    profiler._profile(frame, 'return', None)
    assert len(profiler.stats) == 1
    stat1 = find_stat(profiler.stats, 'foo')
    stat2 = find_stat(profiler.stats, 'bar')
    stat3 = find_stat(profiler.stats, 'baz')
    assert stat1.calls == 0
    assert stat2.calls == 0
    assert stat3.calls == 1
Esempio n. 15
0
class NfcRfidRecognizer(AbstractRecognizer):
    def __init__(self):
        AbstractRecognizer.__init__(self)

        self._name = "[NfcRfidRecognizer]"
        self._profiler = Profiler("[Profiler-NfcRfidRecognizer]")
        # self._gui._nfc_init()

        self._counter = 0
        self._exit_timer = None
        self._exit_cb = None

    def recognize(self, cb, args=None):
        if args != None:
            self.__write_mode(args[0])

        EventLogger.info("NFC/RFID Recognizer started...")
        self._counter = 0

        if self._gui._nfc is None:
            EventLogger.warning(self._name + " NFC/RFID Component was initialized!")

        # Rufe request_tag_id() auf

        self._gui._nfc_cb_to_profiler = cb
        self._exit_cb = cb

        self._gui._nfc.request_tag_id(self._gui._nfc.TAG_TYPE_TYPE2)
        self._exit_timer = threading.Timer(2, self.__cb_retry)
        self._exit_timer.start()

    def cb_recognize(self, recognized_name):
        self._exit_timer.cancel()
        self._gui._nfc_write_name = None
        self._gui._nfc_write_mode = False

        EventLogger.debug(self._name + " cd_recognized= " + str(recognized_name))
        EventLogger.info("NFC/RFID Recognizer finished...")

        if recognized_name != "NO NAME":
            self._profiler.start_profile_routine(recognized_name)

    def __cb_retry(self):
        EventLogger.info("Try to read the Tag...")
        # print "Counter: " + str(self._counter)

        if self._counter >= 5:
            EventLogger.info("Tried 5 times to read the Tag!")
            self._exit_cb("NO NAME")
            return

        self._counter += 1
        self._gui._nfc.request_tag_id(self._gui._nfc.TAG_TYPE_TYPE2)
        # TODO thread.restart?
        self._exit_timer = threading.Timer(2, self.__cb_retry)
        self._exit_timer.start()

    def __write_mode(self, name):
        EventLogger.info("NFC/RFID write Mode: Writing " + str(name) + " to the Tag.")
        self._gui._nfc_write_name = name
        self._gui._nfc_write_mode = True
Esempio n. 16
0
def test_frame_stack():
    def to_code_names(frames):
        code_names = deque()
        for frame in reversed(frames):
            code_name = frame.f_code.co_name
            if code_name not in mock_code_names:
                break
            code_names.appendleft(code_name)
        return list(code_names)

    profiler = Profiler()
    frame = foo()
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['foo', 'bar', 'baz']
    # top frame
    profiler = Profiler(top_frame=frame.f_back)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
    # top code
    profiler = Profiler(top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
    # both of top frame and top code
    profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'baz']
Esempio n. 17
0
 def cb_recognize(self, recognized_name):
     print "callbacked: " + str(recognized_name)
     p = Profiler("[Profiler-Main]")
     p.start_profile_routine(recognized_name, True)
Esempio n. 18
0
    def __init__(self):
        AbstractRecognizer.__init__(self)

        self._name = "[FaceRecognizer]"
        self._profiler = Profiler("[Profiler:Main]")
Esempio n. 19
0
def test_frame_stack():
    profiler = Profiler()
    frame = sys._getframe()
    frame_stack = profiler._frame_stack(frame)
    assert frame_stack[-1] is frame
    assert frame_stack[-2] is frame.f_back
    assert frame_stack[-3] is frame.f_back.f_back
    # top frame
    profiler = Profiler(top_frame=frame.f_back)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
    # top code
    profiler = Profiler(top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
    # both of top frame and top code
    profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert list(frame_stack) == [frame.f_back, frame]
Esempio n. 20
0
def test_frame_stack():
    def to_code_names(frames):
        return [f.f_code.co_name for f in frames]
    profiler = Profiler()
    frame = mock_stacked_frame(map(mock_code, ['foo', 'bar', 'baz']))
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['baz', 'bar', 'foo']
    # top frame
    profiler = Profiler(top_frame=frame.f_back)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'foo']
    # top code
    profiler = Profiler(top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'foo']
    # both of top frame and top code
    profiler = Profiler(top_frame=frame.f_back, top_code=frame.f_back.f_code)
    frame_stack = profiler._frame_stack(frame)
    assert to_code_names(frame_stack) == ['bar', 'foo']