Debian Codegraph @

contrib/perf-utils/search-discovery-case

raw
1 #!/usr/bin/env python3
2 # Search for interesting discovery instance
3 #
4 # search-discovery-case REPO [REPO]…
5 #
6 # This use a subsetmaker extension (next to this script) to generate a steam of
7 # random discovery instance. When interesting case are discovered, information
8 # about them are print on the stdout.
9
10 import json
11 import os
12 import queue
13 import random
14 import signal
15 import subprocess
16 import sys
17 import threading
18
19 this_script = os.path.abspath(sys.argv[0])
20 this_dir = os.path.dirname(this_script)
21 hg_dir = os.path.join(this_dir, '..', '..')
22 HG_REPO = os.path.normpath(hg_dir)
23 HG_BIN = os.path.join(HG_REPO, 'hg')
24
25 JOB = int(os.environ.get('NUMBER_OF_PROCESSORS', 8))
26
27
28 SLICING = ('scratch', 'randomantichain', 'rev')
29
30
31 def nb_revs(repo_path):
32 cmd = [
33 HG_BIN,
34 '--repository',
35 repo_path,
36 'log',
37 '--template',
38 '{rev}',
39 '--rev',
40 'tip',
41 ]
42 s = subprocess.Popen(cmd, stdout=subprocess.PIPE)
43 out, err = s.communicate()
44 return int(out)
45
46
47 repos = []
48 for repo in sys.argv[1:]:
49 size = nb_revs(repo)
50 repos.append((repo, size))
51
52
53 def pick_one(repo):
54 pick = random.choice(SLICING)
55 seed = random.randint(0, 100000)
56 if pick == 'scratch':
57 start = int(repo[1] * 0.3)
58 end = int(repo[1] * 0.7)
59 nb = random.randint(start, end)
60 return ('scratch', nb, seed)
61 elif pick == 'randomantichain':
62 return ('randomantichain', seed)
63 elif pick == 'rev':
64 start = int(repo[1] * 0.3)
65 end = int(repo[1])
66 rev = random.randint(start, end)
67 return ('rev', rev)
68 else:
69 assert False
70
71
72 done = threading.Event()
73 cases = queue.Queue(maxsize=10 * JOB)
74 results = queue.Queue()
75
76
77 def worker():
78 while not done.is_set():
79 c = cases.get()
80 if c is None:
81 return
82 try:
83 res = process(c)
84 results.put((c, res))
85 except Exception as exc:
86 print('processing-failed: %s %s' % (c, exc), file=sys.stderr)
87 c = (c[0], c[2], c[1])
88 try:
89 res = process(c)
90 results.put((c, res))
91 except Exception as exc:
92 print('processing-failed: %s %s' % (c, exc), file=sys.stderr)
93
94
95 SUBSET_PATH = os.path.join(HG_REPO, 'contrib', 'perf-utils', 'subsetmaker.py')
96
97
98 CMD_BASE = (
99 HG_BIN,
100 'debugdiscovery',
101 '--template',
102 'json',
103 '--config',
104 'extensions.subset=%s' % SUBSET_PATH,
105 )
106 # '--local-as-revs "$left" --local-as-revs "$right"'
107 # > /data/discovery-references/results/disco-mozilla-unified-$1-$2.txt
108 # )
109
110
111 def to_revsets(case):
112 t = case[0]
113 if t == 'scratch':
114 return 'not scratch(all(), %d, "%d")' % (case[1], case[2])
115 elif t == 'randomantichain':
116 return '::randomantichain(all(), "%d")' % case[1]
117 elif t == 'rev':
118 return '::%d' % case[1]
119 else:
120 assert False
121
122
123 def process(case):
124 (repo, left, right) = case
125 cmd = list(CMD_BASE)
126 cmd.append('-R')
127 cmd.append(repo[0])
128 cmd.append('--local-as-revs')
129 cmd.append(to_revsets(left))
130 cmd.append('--remote-as-revs')
131 cmd.append(to_revsets(right))
132 s = subprocess.Popen(cmd, stdout=subprocess.PIPE)
133 out, err = s.communicate()
134 return json.loads(out)[0]
135
136
137 def interesting_boundary(res):
138 """check if a case is interesting or not
139
140 For now we are mostly interrested in case were we do multiple roundstrip
141 and where the boundary is somewhere in the middle of the undecided set.
142
143 Ideally, we would make this configurable, but this is not a focus for now
144
145 return None or (
146 round-trip,
147 undecided-common,
148 undecided-missing,
149 total-revs,
150 common-revs,
151 missing-revs,
152 )
153 """
154 roundtrips = res["total-roundtrips"]
155 if roundtrips <= 1:
156 return None
157 total_revs = res["nb-revs"]
158 common_revs = res["nb-revs-common"]
159 missing_revs = res["nb-revs-missing"]
160 undecided_common = res["nb-ini_und-common"]
161 undecided_missing = res["nb-ini_und-missing"]
162 if undecided_common == 0:
163 return None
164 if undecided_missing == 0:
165 return None
166 return (
167 roundtrips,
168 undecided_common,
169 undecided_missing,
170 total_revs,
171 common_revs,
172 missing_revs,
173 )
174
175
176 def end(*args, **kwargs):
177 done.set()
178
179
180 def format_case(case):
181 return '-'.join(str(s) for s in case)
182
183
184 signal.signal(signal.SIGINT, end)
185
186 for i in range(JOB):
187 threading.Thread(target=worker).start()
188
189 nb_cases = 0
190 while not done.is_set():
191 repo = random.choice(repos)
192 left = pick_one(repo)
193 right = pick_one(repo)
194 cases.put((repo, left, right))
195 while not results.empty():
196 # results has a single reader so this is fine
197 c, res = results.get_nowait()
198 boundary = interesting_boundary(res)
199 if boundary is not None:
200 print(c[0][0], format_case(c[1]), format_case(c[2]), *boundary)
201 sys.stdout.flush()
202
203 nb_cases += 1
204 if not nb_cases % 100:
205 print('[%d cases generated]' % nb_cases, file=sys.stderr)
206
207 for i in range(JOB):
208 try:
209 cases.put_nowait(None)
210 except queue.Full:
211 pass
212
213 print('[%d cases generated]' % nb_cases, file=sys.stderr)
214 print('[ouput generation is over]' % nb_cases, file=sys.stderr)