Beispiel #1
0
 def generate_sequences(
     self,
 ) -> Tuple[tsc.TestSuiteChromosome, tsc.TestSuiteChromosome]:
     stopping_condition = self.get_stopping_condition()
     stopping_condition.reset()
     self._population = self._get_random_population()
     self._sort_population()
     StatisticsTracker().current_individual(self._get_best_individual())
     generation = 0
     while (
         not self.is_fulfilled(stopping_condition)
         and self._get_best_individual().get_fitness() != 0.0
     ):
         self.evolve()
         StatisticsTracker().current_individual(self._get_best_individual())
         self._logger.info(
             "Generation: %5i. Best fitness: %5f, Best coverage %5f",
             generation,
             self._get_best_individual().get_fitness(),
             self._get_best_individual().get_coverage(),
         )
         generation += 1
     StatisticsTracker().track_output_variable(
         RuntimeVariable.AlgorithmIterations, generation
     )
     return self.split_chromosomes()
Beispiel #2
0
def test_variables_generator():
    tracker = StatisticsTracker()
    value_1 = MagicMock(Timer)
    value_2 = MagicMock(Timer)
    tracker.track_output_variable(RuntimeVariable.TotalTime, value_1)
    tracker.track_output_variable(RuntimeVariable.TotalTime, value_2)
    result = [v for _, v in tracker.variables_generator]
    assert result == [value_1, value_2]
Beispiel #3
0
 def _collect_statistics() -> None:
     tracker = StatisticsTracker()
     tracker.track_output_variable(RuntimeVariable.TargetModule,
                                   config.INSTANCE.module_name)
     tracker.track_output_variable(RuntimeVariable.RandomSeed,
                                   randomness.RNG.get_seed())
     tracker.track_output_variable(RuntimeVariable.ConfigurationId,
                                   config.INSTANCE.configuration_id)
     for runtime_variable, value in tracker.variables_generator:
         tracker.set_output_variable_for_runtime_variable(
             runtime_variable, value)
Beispiel #4
0
    def generate_sequences(
        self, ) -> Tuple[tsc.TestSuiteChromosome, tsc.TestSuiteChromosome]:
        stopping_condition = self.get_stopping_condition()
        stopping_condition.reset()
        test_chromosome: tsc.TestSuiteChromosome = tsc.TestSuiteChromosome()
        failing_test_chromosome: tsc.TestSuiteChromosome = tsc.TestSuiteChromosome(
        )
        generation: int = 0
        fitness_functions = self.get_fitness_functions()
        for fitness_function in fitness_functions:
            test_chromosome.add_fitness_function(fitness_function)
            failing_test_chromosome.add_fitness_function(fitness_function)

        combined_chromosome = self._combine_current_individual(
            test_chromosome, failing_test_chromosome)

        while (not self.is_fulfilled(stopping_condition)
               and combined_chromosome.get_fitness() != 0.0):
            try:
                generation += 1
                stopping_condition.iterate()
                self.generate_sequence(
                    test_chromosome,
                    failing_test_chromosome,
                    generation,
                )
                combined_chromosome = self._combine_current_individual(
                    test_chromosome, failing_test_chromosome)
                StatisticsTracker().current_individual(combined_chromosome)
                self._logger.info(
                    "Generation: %5i. Best fitness: %5f, Best coverage %5f",
                    generation,
                    combined_chromosome.get_fitness(),
                    combined_chromosome.get_coverage(),
                )
            except (ConstructionFailedException,
                    GenerationException) as exception:
                self._logger.debug(
                    "Generate test case failed with exception %s", exception)

        self._logger.debug("Number of algorithm iterations: %d", generation)
        StatisticsTracker().track_output_variable(
            RuntimeVariable.AlgorithmIterations, generation)

        return test_chromosome, failing_test_chromosome
Beispiel #5
0
def test_send_statistics(strategy):
    strategy.send_statistics()
    tracker = StatisticsTracker()
    statistics = [
        v
        for k, v in tracker.variables_generator
        if k == RuntimeVariable.MonkeyTypeExecutions
    ]
    assert len(statistics) == 1
    assert statistics[0] == 0
 def _track_sut_data(tracer: ExecutionTracer, test_cluster: TestCluster) -> None:
     """Track data from the SUT."""
     tracker = StatisticsTracker()
     tracker.track_output_variable(
         RuntimeVariable.CodeObjects,
         len(tracer.get_known_data().existing_code_objects),
     )
     tracker.track_output_variable(
         RuntimeVariable.Predicates, len(tracer.get_known_data().existing_predicates)
     )
     tracker.track_output_variable(
         RuntimeVariable.AccessibleObjectsUnderTest,
         test_cluster.num_accessible_objects_under_test(),
     )
Beispiel #7
0
    def evolve(self) -> None:
        """Evolve the current population and replace it with a new one."""
        new_generation = []
        new_generation.extend(self.elitism())
        while not self.is_next_population_full(new_generation):
            parent1 = self._selection_function.select(self._population, 1)[0]
            parent2 = self._selection_function.select(self._population, 1)[0]

            offspring1 = parent1.clone()
            offspring2 = parent2.clone()

            try:
                if randomness.next_float() <= config.INSTANCE.crossover_rate:
                    self._crossover_function.cross_over(offspring1, offspring2)

                offspring1.mutate()
                offspring2.mutate()
            except ConstructionFailedException as ex:
                self._logger.info("Crossover/Mutation failed: %s", ex)
                continue

            fitness_parents = min(parent1.get_fitness(), parent2.get_fitness())
            fitness_offspring = min(offspring1.get_fitness(), offspring2.get_fitness())
            length_parents = (
                parent1.total_length_of_test_cases + parent2.total_length_of_test_cases
            )
            length_offspring = (
                offspring1.total_length_of_test_cases
                + offspring2.total_length_of_test_cases
            )
            best_individual = self._get_best_individual()

            if (fitness_offspring < fitness_parents) or (
                fitness_offspring == fitness_parents
                and length_offspring <= length_parents
            ):
                for offspring in [offspring1, offspring2]:
                    if (
                        offspring.total_length_of_test_cases
                        <= 2 * best_individual.total_length_of_test_cases
                    ):
                        new_generation.append(offspring)
                    else:
                        new_generation.append(randomness.choice([parent1, parent2]))
            else:
                new_generation.append(parent1)
                new_generation.append(parent2)

        self._population = new_generation
        self._sort_population()
        StatisticsTracker().current_individual(self._get_best_individual())
Beispiel #8
0
 def _track_statistics(
     non_failing: tsc.TestSuiteChromosome,
     failing: tsc.TestSuiteChromosome,
     combined: tsc.TestSuiteChromosome,
 ) -> None:
     tracker = StatisticsTracker()
     tracker.current_individual(combined)
     tracker.track_output_variable(RuntimeVariable.Size, combined.size())
     tracker.track_output_variable(
         RuntimeVariable.Length, combined.total_length_of_test_cases
     )
     tracker.track_output_variable(RuntimeVariable.FailingSize, failing.size())
     tracker.track_output_variable(
         RuntimeVariable.FailingLength, failing.total_length_of_test_cases,
     )
     tracker.track_output_variable(RuntimeVariable.PassingSize, non_failing.size())
     tracker.track_output_variable(
         RuntimeVariable.PassingLength, non_failing.total_length_of_test_cases
     )
Beispiel #9
0
            len(test_cluster.get_all_generatable_types()),
        )

    def _run(self) -> ReturnCode:
        if (setup_result := self._setup_and_check()) is None:
            return ReturnCode.SETUP_FAILED
        executor, test_cluster = setup_result

        with Timer(name="Test generation time", logger=None):
            algorithm: TestGenerationStrategy = self._instantiate_test_generation_strategy(
                executor, test_cluster
            )
            self._logger.info(
                "Start generating sequences using %s", config.INSTANCE.algorithm
            )
            StatisticsTracker().set_sequence_start_time(time.time_ns())
            non_failing, failing = algorithm.generate_sequences()
            self._logger.info(
                "Stop generating sequences using %s", config.INSTANCE.algorithm
            )
            algorithm.send_statistics()

            with Timer(name="Re-execution time", logger=None):
                combined = tsc.TestSuiteChromosome()
                for fitness_func in non_failing.get_fitness_functions():
                    combined.add_fitness_function(fitness_func)
                combined.add_tests(non_failing.test_chromosomes)
                combined.add_tests(failing.test_chromosomes)
                StatisticsTracker().track_output_variable(
                    RuntimeVariable.Coverage, combined.get_coverage()
                )
 def send_statistics(self):
     super().send_statistics()
     tracker = StatisticsTracker()
     tracker.track_output_variable(RuntimeVariable.MonkeyTypeExecutions,
                                   self._monkey_type_executions)
     tracker.track_output_variable(
         RuntimeVariable.ParameterTypeUpdates,
         [self._special_chars(str(p)) for p in self._parameter_updates],
     )
     tracker.track_output_variable(RuntimeVariable.ParameterTypeUpdatesSize,
                                   len(self._parameter_updates))
     tracker.track_output_variable(
         RuntimeVariable.ReturnTypeUpdates,
         [self._special_chars(str(r)) for r in self._return_type_updates],
     )
     tracker.track_output_variable(RuntimeVariable.ReturnTypeUpdatesSize,
                                   len(self._return_type_updates))
Beispiel #11
0
 def send_statistics(self) -> None:
     super().send_statistics()
     tracker = StatisticsTracker()
     tracker.track_output_variable(RuntimeVariable.ExecutionResults,
                                   self._execution_results)
Beispiel #12
0
def test_tracker():
    tracker = StatisticsTracker()
    value = MagicMock(Timer)
    tracker.track_output_variable(RuntimeVariable.TotalTime, value)
    assert tracker.variables.get() == (RuntimeVariable.TotalTime, value)
Beispiel #13
0
def test_singleton():
    tracker_1 = StatisticsTracker()
    tracker_2 = StatisticsTracker()
    assert tracker_1 is tracker_2