コード例 #1
0
ファイル: grader_multitask.py プロジェクト: MeawHB/QiwuGrader
class GraderThread(GraderSkeleton, threading.Thread):
    def __init__(self,
                 shared_counter,
                 test_config,
                 time_counter=SharedCounter(val_type='d'),
                 **kwargs):
        super(GraderThread, self).__init__(shared_counter, test_config,
                                           time_counter, kwargs)
        threading.Thread.__init__(self)

        # initialize grader
        self.grader = Grader()

    def init(self):
        self.grader.init(self.test_config)

    def grade(self):
        # calculate result
        success_count, success_time = self.grader.test()
        if success_count > 0:
            self.shared_counter.increment()
            self.time_counter.increment(success_time)

    def run(self):
        while self.loop > 0:
            self.grade()
            self.loop -= 1

            # only sleep when we need to do next grade
            if self.loop > 0:
                sleep(self.spawn_interval)
コード例 #2
0
    def run(test_config_file_name):
        test_config = YamlConfigFileHandler(test_config_file_name)

        if test_session == 1:  # single session grade
            # init_log_file()

            grader = Grader()
            grader.init(test_config)
            grader.test()
        elif test_session > 1:  # multi session grade
            # calculate thread spawn interval
            spawn_interval = test_length / (test_session * 1.0)

            # determine grader class
            use_process = False
            handler_count = test_session
            session_per_handler = 1
            Handler_Class = GraderThread

            # use process to speed up grade
            if test_session > 512:
                use_process = True
                handler_count = multiprocessing.cpu_count()
                session_per_handler = test_session / handler_count
                Handler_Class = GraderProcess

            # count the number of spawned sessions
            session_count = 0

            # thread safe success counter
            success_count = SharedCounter()
            success_time_count = SharedCounter(val_type='d')

            # process time counter
            process_time = time.time()

            # thread group
            threads = []

            # if not use_process and test_session <= 100:
            #     init_log_file()

            report_logger.info(
                "Testing {0} sessions in {1} seconds, interval: {2}, using class {3}"
                .format(test_session, test_length, spawn_interval,
                        Handler_Class.__name__))
            report_logger.info("Warming up ...")

            warm_up_time = time.time()
            # Spawn threads
            while session_count < handler_count:
                grader_handler = Handler_Class(success_count,
                                               test_config,
                                               success_time_count,
                                               loop=session_per_handler,
                                               spawn_interval=spawn_interval *
                                               handler_count)
                grader_handler.init()

                threads.append(grader_handler)
                session_count += 1

            report_logger.info(
                "Warm up process finished in {0} seconds".format(time.time() -
                                                                 warm_up_time))

            launch_time = time.time()
            # Start threads
            for grader_handler in threads:
                grader_handler.start()

                # Wait for spawn interval
                sleep(spawn_interval)

            report_logger.info("{0} sessions started in {1}".format(
                int(session_count * session_per_handler),
                time.time() - launch_time))

            # Wait for all threads to finish
            for grader_handler in threads:
                grader_handler.join()

            questions_count = success_count.value() * len(
                test_config.get_config("questions"))
            report_logger.info(
                "Result: {0} / {1} passed. Total time: {2}\nSuccess time: {3} Passed: {4} Success avg: {5}"
                .format(success_count.value(),
                        int(session_count * session_per_handler),
                        time.time() - process_time, success_time_count.value(),
                        questions_count,
                        success_time_count.value() / questions_count))