-
Notifications
You must be signed in to change notification settings - Fork 3
/
million_scale.py
223 lines (187 loc) · 7.26 KB
/
million_scale.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from scripts.utils.file_utils import load_json, dump_json
from scripts.analysis.analysis import *
from default import *
if __name__ == "__main__":
# set to True to use your own datasets/measurements
run_repro = True
if run_repro:
# DATASET FILES
PROBES_FILE = REPRO_PROBES_FILE
PROBES_AND_ANCHORS_FILE = REPRO_PROBES_AND_ANCHORS_FILE
FILTERED_PROBES_FILE = REPRO_FILTERED_PROBES_FILE
GREEDY_PROBES_FILE = REPRO_GREEDY_PROBES_FILE
PAIRWISE_DISTANCE_FILE = REPRO_PAIRWISE_DISTANCE_FILE
VPS_TO_TARGET_TABLE = PROBES_TO_ANCHORS_PING_TABLE
VPS_TO_PREFIX_TABLE = PROBES_TO_PREFIX_TABLE
# RESULT FILES
PROBES_TO_ANCHORS_RESULT_FILE = REPRO_PROBES_TO_ANCHORS_RESULT_FILE
ROUND_BASED_ALGORITHM_FILE = REPRO_ROUND_BASED_ALGORITHM_FILE
ACCURACY_VS_N_VPS_PROBES_FILE = REPRO_ACCURACY_VS_N_VPS_PROBES_FILE
VP_SELECTION_ALGORITHM_PROBES_1_FILE = (
REPRO_VP_SELECTION_ALGORITHM_PROBES_1_FILE
)
VP_SELECTION_ALGORITHM_PROBES_3_FILE = (
REPRO_VP_SELECTION_ALGORITHM_PROBES_3_FILE
)
VP_SELECTION_ALGORITHM_PROBES_10_FILE = (
REPRO_VP_SELECTION_ALGORITHM_PROBES_10_FILE
)
else:
# DATASET FILES
PROBES_FILE = USER_PROBES_FILE
PROBES_AND_ANCHORS_FILE = USER_PROBES_AND_ANCHORS_FILE
FILTERED_PROBES_FILE = USER_FILTERED_PROBES_FILE
GREEDY_PROBES_FILE = USER_GREEDY_PROBES_FILE
PAIRWISE_DISTANCE_FILE = USER_PAIRWISE_DISTANCE_FILE
VPS_TO_TARGET_TABLE = USER_VPS_TO_TARGET_TABLE
VPS_TO_PREFIX_TABLE = USER_VPS_TO_PREFIX_TABLE
# RESULT FILES
PROBES_TO_ANCHORS_RESULT_FILE = USER_PROBES_TO_ANCHORS_RESULT_FILE
ROUND_BASED_ALGORITHM_FILE = USER_ROUND_BASED_ALGORITHM_FILE
ACCURACY_VS_N_VPS_PROBES_FILE = USER_ACCURACY_VS_N_VPS_PROBES_FILE
VP_SELECTION_ALGORITHM_PROBES_1_FILE = USER_VP_SELECTION_ALGORITHM_PROBES_1_FILE
VP_SELECTION_ALGORITHM_PROBES_3_FILE = USER_VP_SELECTION_ALGORITHM_PROBES_3_FILE
VP_SELECTION_ALGORITHM_PROBES_10_FILE = (
USER_VP_SELECTION_ALGORITHM_PROBES_10_FILE
)
LIMIT = 1000
filtered_probes = load_json(FILTERED_PROBES_FILE)
filter = ""
if len(filtered_probes) > 0:
# Remove probes that are wrongly geolocated
in_clause = f"".join([f",toIPv4('{p}')" for p in filtered_probes])[1:]
filter += f"AND dst not in ({in_clause}) AND src not in ({in_clause}) "
logger.info("Step 1: Compute errors")
all_probes = load_json(PROBES_AND_ANCHORS_FILE)
(
vp_coordinates_per_ip,
ip_per_coordinates,
country_per_vp,
asn_per_vp,
vp_distance_matrix,
probes_per_ip,
) = compute_geo_info(all_probes, PAIRWISE_DISTANCE_FILE)
rtt_per_srcs_dst = compute_rtts_per_dst_src(
PROBES_TO_ANCHORS_PING_TABLE, filter, threshold=70
)
vps_per_target = {
dst: set(vp_coordinates_per_ip.keys()) for dst in rtt_per_srcs_dst
}
features = compute_geolocation_features_per_ip(
rtt_per_srcs_dst,
vp_coordinates_per_ip,
THRESHOLD_DISTANCES,
vps_per_target=vps_per_target,
distance_operator=">",
max_vps=100000,
is_use_prefix=False,
vp_distance_matrix=vp_distance_matrix,
)
dump_json(features, PROBES_TO_ANCHORS_RESULT_FILE)
logger.info("Step 2: Round Algorithm")
all_probes = load_json(PROBES_AND_ANCHORS_FILE)
asn_per_vp_ip = {}
vp_coordinates_per_ip = {}
for probe in all_probes:
if (
"address_v4" in probe
and "geometry" in probe
and "coordinates" in probe["geometry"]
):
ip_v4_address = probe["address_v4"]
if ip_v4_address is None:
continue
long, lat = probe["geometry"]["coordinates"]
asn_v4 = probe["asn_v4"]
asn_per_vp_ip[ip_v4_address] = asn_v4
vp_coordinates_per_ip[ip_v4_address] = lat, long
# clickhouse is required here
rtt_per_srcs_dst = compute_rtts_per_dst_src(
PROBES_TO_ANCHORS_PING_TABLE, filter, threshold=100
)
vp_distance_matrix = load_json(PAIRWISE_DISTANCE_FILE)
TIER1_VPS = [10, 100, 300, 500, 1000]
greedy_probes = load_json(GREEDY_PROBES_FILE)
error_cdf_per_tier1_vps = {}
for tier1_vps in TIER1_VPS:
print(f"Using {tier1_vps} tier1_vps")
error_cdf = round_based_algorithm(
greedy_probes,
rtt_per_srcs_dst,
vp_coordinates_per_ip,
asn_per_vp_ip,
tier1_vps,
threshold=40,
)
error_cdf_per_tier1_vps[tier1_vps] = error_cdf
dump_json(error_cdf_per_tier1_vps, ROUND_BASED_ALGORITHM_FILE)
logger.info("Accuracy vs number of vps probes")
logger.warning("this step might takes several hours")
all_probes = load_json(PROBES_AND_ANCHORS_FILE)
(
vp_coordinates_per_ip,
ip_per_coordinates,
country_per_vp,
asn_per_vp,
vp_distance_matrix,
probe_per_ip,
) = compute_geo_info(all_probes, serialized_file=PAIRWISE_DISTANCE_FILE)
logger.info("Accuracy vs number of vps probes")
subset_sizes = []
subset_sizes.extend([i for i in range(100, 1000, 100)])
# subset_sizes.extend([i for i in range(1000, 10001, 1000)])
rtt_per_srcs_dst = compute_rtts_per_dst_src(
PROBES_TO_ANCHORS_PING_TABLE, filter, threshold=50
)
available_vps = list(vp_coordinates_per_ip.keys())
accuracy_vs_nb_vps = compute_accuracy_vs_number_of_vps(
available_vps,
rtt_per_srcs_dst,
vp_coordinates_per_ip,
vp_distance_matrix,
subset_sizes,
)
dump_json(accuracy_vs_nb_vps, ACCURACY_VS_N_VPS_PROBES_FILE)
logger.info("vp selection algorithm")
all_probes = load_json(PROBES_AND_ANCHORS_FILE)
(
vp_coordinates_per_ip,
ip_per_coordinates,
country_per_vp,
asn_per_vp,
vp_distance_matrix,
probes_per_ip,
) = compute_geo_info(all_probes, PAIRWISE_DISTANCE_FILE)
ping_table_prefix = PROBES_TO_PREFIX_TABLE
ping_table = PROBES_TO_ANCHORS_PING_TABLE
N_VPS_SELECTION_ALGORITHM = [1, 3, 10]
results_files = [
VP_SELECTION_ALGORITHM_PROBES_1_FILE,
VP_SELECTION_ALGORITHM_PROBES_3_FILE,
VP_SELECTION_ALGORITHM_PROBES_10_FILE,
]
rtt_per_srcs_dst_prefix = compute_rtts_per_dst_src(
ping_table_prefix, filter, threshold=100, is_per_prefix=True
)
rtt_per_srcs_dst = compute_rtts_per_dst_src(ping_table, filter, threshold=70)
for i, n_vp in enumerate(N_VPS_SELECTION_ALGORITHM):
vps_per_target = compute_closest_rtt_probes(
rtt_per_srcs_dst_prefix,
vp_coordinates_per_ip,
vp_distance_matrix,
n_shortest=n_vp,
is_prefix=True,
)
features = compute_geolocation_features_per_ip(
rtt_per_srcs_dst,
vp_coordinates_per_ip,
[0],
vps_per_target=vps_per_target,
distance_operator=">",
max_vps=100000,
is_use_prefix=True,
vp_distance_matrix=vp_distance_matrix,
is_multiprocess=True,
)
ofile = results_files[i]
dump_json(features, ofile)