def analyze(trace_path, index: VarIndex, scopes_ends):
    df, size_mb = read_as_np_array(trace_path)
    defs, uses = index.get_object_vars(df)
    reach_in = []
    intermethod_pairs = []
    intraclass_pairs = []
    for this_row, this_ds, this_us, in zip(df, defs, uses):
        # join reachins to pairs
        for def_idx, def_var_name in reach_in:
            that_row = df[def_idx]
            that_scope = that_row[SCOPE_INDEX]
            if that_scope != this_row[SCOPE_INDEX]:
                if def_var_name in this_us:
                    this_row_idx = this_row[IDX_INDEX]
                    that_scope_end = scopes_ends[that_scope]
                    du_pair = (that_row[LINE_INDEX], this_row[LINE_INDEX])
                    if that_scope_end > this_row_idx:
                        intermethod_pairs.append(du_pair)
                    else:
                        intraclass_pairs.append(du_pair)

        # filter reach ins
        reach_in = [(def_idx, var_name) for def_idx, var_name in reach_in
                    if var_name not in this_ds]

        # add this row definitions
        for this_definition in this_ds:
            reach_in.append((this_row[0], this_definition))
    return intermethod_pairs, intraclass_pairs
        def get_pairs(trace_file_path):
            np_array, _ = read_as_np_array(trace_file_path)
            idx_pairs = analyze_trace_w_index(trace_file_path, cppvi)

            def rename_vars(s):
                return {(el[0], el[1]) for el in s}

            idx_pairs = rename_vars(idx_pairs)
            return idx_pairs
    def covered_items_of(self,
                         module_path,
                         of_type=CoverageMetric.ALL_PAIRS,
                         selected_node_ids=None) -> dict:
        data = {}
        total_items = self.total_items_of(module_path, of_type=of_type)
        node_ids, traces_paths = self.trace_reader.get_traces_for(
            module_path, selected_node_ids=selected_node_ids)
        module_name = os.path.basename(module_path)
        desc = "Finding def-use pairs of type {} for module {}".format(
            of_type.name, module_name)
        for test_case, trace_file_path in tqdm(zip(node_ids, traces_paths),
                                               total=len(node_ids),
                                               desc=desc,
                                               unit="test_case"):
            np_array, fsize = read_as_np_array(trace_file_path,
                                               max_size_mb=self.max_trace_size)
            if np_array is None:
                continue

            scopes = read_scopes_for_trace_file(trace_file_path)
            if not scopes:
                continue

            if of_type == CoverageMetric.M_ONLY:
                mp = analyze_trace_w_index(trace_file_path, self.cppvi)
                data[test_case] = rename_vars(mp)
            elif of_type == CoverageMetric.IM_ONLY:
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(imp)
            elif of_type == CoverageMetric.IC_ONLY:
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(icp)
            elif of_type == CoverageMetric.M_AND_IM:
                mp = analyze_trace_w_index(trace_file_path, self.cppvi)
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(mp) | rename_vars(imp)
            elif of_type == CoverageMetric.M_AND_IC:
                mp = analyze_trace_w_index(trace_file_path, self.cppvi)
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(mp) | rename_vars(icp)
            elif of_type == CoverageMetric.IM_AND_IC:
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(imp) | rename_vars(icp)
            elif of_type == CoverageMetric.ALL_PAIRS:
                mp = analyze_trace_w_index(trace_file_path, self.cppvi)
                imp, icp = analyze(trace_file_path, self.vi, scopes)
                data[test_case] = rename_vars(mp) | rename_vars(
                    imp) | rename_vars(icp)
            else:
                raise ValueError(
                    "Unknown coverage metric {} in parameter 'of_type'".format(
                        of_type))
            data[test_case] = data[test_case].intersection(total_items)
        return data
        def get_pairs(trace_file_path):
            np_array, _ = read_as_np_array(trace_file_path)
            scopes = read_scopes_for_trace_file(trace_file_path)
            im_pairs, ic_pairs = analyze(trace_file_path, cppvi, scopes)

            def rename_vars(s):
                return {(el[0], el[1]) for el in s}

            im_pairs = rename_vars(im_pairs)
            ic_pairs = rename_vars(ic_pairs)
            return im_pairs, ic_pairs
示例#5
0
 def covered_items_of(self,
                      module_path,
                      of_type=None,
                      selected_node_ids=None) -> dict:
     covered_items = {}
     node_ids, paths = self.trace_reader.get_traces_for(
         module_path=module_path, selected_node_ids=selected_node_ids)
     total_items = self.total_items_of(module_path, of_type=of_type)
     for node_id, trace_file_path in zip(node_ids, paths):
         df, size = read_as_np_array(trace_file_path,
                                     max_size_mb=self.max_trace_size)
         if df is not None:
             lines = df.T[LINE_INDEX]
             covered_items[node_id] = set(lines).intersection(total_items)
         else:
             covered_items[node_id] = set()
     return covered_items
def analyze_trace(trace_file, py_var_index):
    np_array, file_size = read_as_np_array(trace_file, cut=-1)

    st = time()
    defs, uses = py_var_index.get_vars(np_array)
    pairs_cpp_new = cpp_pairs_new(np_array, defs, uses)
    total_new = time() - st

    pairs_count_new = _count_pairs(pairs_cpp_new)

    print("total cpp new", total_new)
    print("Speed: {}Mb/s".format(file_size // total_new))
    print("New found {} pairs".format(pairs_count_new))
    unique_pairs = _unique_pairs(pairs_cpp_new)
    print("Unique pairs {}".format(len(unique_pairs)))

    del pairs_cpp_new
    del np_array
    del defs
    del uses
    gc.collect()

    return unique_pairs
def get_trace(function, *args, **kwargs):
    trace_file_path = trace_this(function, *args, **kwargs)
    trace, _ = read_as_np_array(trace_file_path)
    return trace
def analyze_trace_w_index(trace_file, cpp_var_index, cut=-1):
    np_array, file_size = read_as_np_array(trace_file, cut=cut)
    pairs_cpp_new = cpp_pairs_w_index(np_array, cpp_var_index)
    unique_pairs = _unique_pairs(pairs_cpp_new)
    return unique_pairs