Esempio n. 1
0
 def test_stopwatch_cancel(self):
     """Test that spans can be correctly cancelled and not reported."""
     sw = StopWatch()
     sw.start('root')
     sw.start('child')
     sw.cancel('child')
     sw.end('root')
     agg_values = sw.get_last_aggregated_report().aggregated_values
     assert len(agg_values) == 1
     assert 'root' in agg_values
    # trie = build_cost_trie(data_file_path)
    #
    # stopwatch.mark("Starting to pickle")
    # pickle.dump(trie, open('trie.pickle', 'wb'))
    #
    # ======================================================================

    stopwatch.mark("Unpickling Trie:")
    trie = pickle.load(open('trie.pickle', 'rb'))

    stopwatch.mark("Reading 10,000 phone numbers from file:")
    phone_numbers = open("data/phone-numbers-10000.txt").read().splitlines()

    stopwatch.mark(f"Getting costs for {len(phone_numbers)} phone numbers:")
    find_call_cost(phone_numbers, trie)

    # ===== Uncomment to benchmark writing the costs to results file =======
    #
    # with open('data/results-3.txt', 'w') as out:
    #     numbers = (number for number in open(
    #         'data/phone-numbers-10000.txt').read().splitlines())
    #     for number in numbers:
    #         cost = trie.search(number)
    #         out.write(f'{number},{cost}\n')
    #
    # ======================================================================

    stopwatch.end()  # Can whoever grades this help me figure out why my stopwatch
    # output is delayed until the whole program is finished ??? I programmed it to
    # print during each mark's execution :(
    Params:
    costs_dict: dictionary containing number prefix, cost data

    input_file_path: path to input file with valid phone numbers (string)
    # Format (+1913123434)

    output_file_path: path to to file where outputted phonenumber, cost
    data will be written
    """

    with open(input_file_path) as f:
        test_numbers = f.read().splitlines()

    with open(output_file_path, 'w') as out:
        for number in test_numbers:
            cost = search_number(costs_dict, number)
            out.write(f'{number},{cost}\n')


if __name__ == '__main__':
    from stopwatch import StopWatch
    watch = StopWatch()

    watch.mark("Compiling 10,000,000 route costs into dictionary:")
    costs_dict = get_cost_dict("data/route-costs-10000000.txt")

    watch.mark("Getting costs for 1,000 numbers and storing in results file:")
    get_all_costs(costs_dict, 'data/phone-numbers-1000.txt',
                  'data/results-2.txt')
    watch.end()