##// END OF EJS Templates
tests: use range() in generate-churning-module.py...
Gregory Szorc -
r43380:1fcf79e9 default
parent child Browse files
Show More
@@ -1,143 +1,143 b''
1 1 #!/usr/bin/env python
2 2 #
3 3 # generate-branchy-bundle - generate a branch for a "large" branchy repository
4 4 #
5 5 # Copyright 2018 Octobus, contact@octobus.net
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9 #
10 10 # This script generates a repository suitable for testing delta computation
11 11 # strategies.
12 12 #
13 13 # The repository update a single "large" file with many updates. One fixed part
14 14 # of the files always get updated while the rest of the lines get updated over
15 15 # time. This update happens over many topological branches, some getting merged
16 16 # back.
17 17 #
18 18 # Running with `chg` in your path and `CHGHG` set is recommended for speed.
19 19
20 20 from __future__ import absolute_import, print_function
21 21
22 22 import hashlib
23 23 import os
24 24 import shutil
25 25 import subprocess
26 26 import sys
27 27 import tempfile
28 28
29 29 BUNDLE_NAME = 'big-file-churn.hg'
30 30
31 31 # constants for generating the repository
32 32 NB_CHANGESET = 5000
33 33 PERIOD_MERGING = 8
34 34 PERIOD_BRANCHING = 7
35 35 MOVE_BACK_MIN = 3
36 36 MOVE_BACK_RANGE = 5
37 37
38 38 # constants for generating the large file we keep updating
39 39 #
40 40 # At each revision, the beginning on the file change,
41 41 # and set of other lines changes too.
42 42 FILENAME = 'SPARSE-REVLOG-TEST-FILE'
43 43 NB_LINES = 10500
44 44 ALWAYS_CHANGE_LINES = 500
45 45 OTHER_CHANGES = 300
46 46
47 47
48 48 def nextcontent(previous_content):
49 49 """utility to produce a new file content from the previous one"""
50 50 return hashlib.md5(previous_content).hexdigest()
51 51
52 52
53 53 def filecontent(iteridx, oldcontent):
54 54 """generate a new file content
55 55
56 56 The content is generated according the iteration index and previous
57 57 content"""
58 58
59 59 # initial call
60 60 if iteridx is None:
61 61 current = ''
62 62 else:
63 63 current = str(iteridx)
64 64
65 for idx in xrange(NB_LINES):
65 for idx in range(NB_LINES):
66 66 do_change_line = True
67 67 if oldcontent is not None and ALWAYS_CHANGE_LINES < idx:
68 68 do_change_line = not ((idx - iteridx) % OTHER_CHANGES)
69 69
70 70 if do_change_line:
71 71 to_write = current + '\n'
72 72 current = nextcontent(current)
73 73 else:
74 74 to_write = oldcontent[idx]
75 75 yield to_write
76 76
77 77
78 78 def updatefile(filename, idx):
79 79 """update <filename> to be at appropriate content for iteration <idx>"""
80 80 existing = None
81 81 if idx is not None:
82 82 with open(filename, 'rb') as old:
83 83 existing = old.readlines()
84 84 with open(filename, 'wb') as target:
85 85 for line in filecontent(idx, existing):
86 86 target.write(line)
87 87
88 88
89 89 def hg(command, *args):
90 90 """call a mercurial command with appropriate config and argument"""
91 91 env = os.environ.copy()
92 92 if 'CHGHG' in env:
93 93 full_cmd = ['chg']
94 94 else:
95 95 full_cmd = ['hg']
96 96 full_cmd.append('--quiet')
97 97 full_cmd.append(command)
98 98 if command == 'commit':
99 99 # reproducible commit metadata
100 100 full_cmd.extend(['--date', '0 0', '--user', 'test'])
101 101 elif command == 'merge':
102 102 # avoid conflicts by picking the local variant
103 103 full_cmd.extend(['--tool', ':merge-local'])
104 104 full_cmd.extend(args)
105 105 env['HGRCPATH'] = ''
106 106 return subprocess.check_call(full_cmd, env=env)
107 107
108 108
109 109 def run(target):
110 110 tmpdir = tempfile.mkdtemp(prefix='tmp-hg-test-big-file-bundle-')
111 111 try:
112 112 os.chdir(tmpdir)
113 113 hg('init')
114 114 updatefile(FILENAME, None)
115 115 hg('commit', '--addremove', '--message', 'initial commit')
116 for idx in xrange(1, NB_CHANGESET + 1):
116 for idx in range(1, NB_CHANGESET + 1):
117 117 if sys.stdout.isatty():
118 118 print("generating commit #%d/%d" % (idx, NB_CHANGESET))
119 119 if (idx % PERIOD_BRANCHING) == 0:
120 120 move_back = MOVE_BACK_MIN + (idx % MOVE_BACK_RANGE)
121 121 hg('update', ".~%d" % move_back)
122 122 if (idx % PERIOD_MERGING) == 0:
123 123 hg('merge', 'min(head())')
124 124 updatefile(FILENAME, idx)
125 125 hg('commit', '--message', 'commit #%d' % idx)
126 126 hg('bundle', '--all', target, '--config', 'devel.bundle.delta=p1')
127 127 with open(target, 'rb') as bundle:
128 128 data = bundle.read()
129 129 digest = hashlib.md5(data).hexdigest()
130 130 with open(target + '.md5', 'wb') as md5file:
131 131 md5file.write(digest + '\n')
132 132 if sys.stdout.isatty():
133 133 print('bundle generated at "%s" md5: %s' % (target, digest))
134 134
135 135 finally:
136 136 shutil.rmtree(tmpdir)
137 137 return 0
138 138
139 139
140 140 if __name__ == '__main__':
141 141 orig = os.path.realpath(os.path.dirname(sys.argv[0]))
142 142 target = os.path.join(orig, os.pardir, 'cache', BUNDLE_NAME)
143 143 sys.exit(run(target))
General Comments 0
You need to be logged in to leave comments. Login now