| 1 |
|
| 2 |
|
| 3 |
. |
| 4 |
. |
| 5 |
|
| 6 |
"""\ |
| 7 |
NOTE: This script has not been revised yet as a main program. Currently, |
| 8 |
it is only meant as a part of the Recode test suite. |
| 9 |
|
| 10 |
-------------------------------------------------------------------------- |
| 11 |
|
| 12 |
Produce statistics from the results of the bigauto check. |
| 13 |
|
| 14 |
Usage: bigauto [RECODE_OPTION]... [CHARSET_OPTION]... |
| 15 |
|
| 16 |
This script makes a simple analysis for the connectivity of the various |
| 17 |
charsets and produce a report on standard output. The reports include |
| 18 |
statistics about the number of steps before and after optimisation. |
| 19 |
|
| 20 |
The option `-hNAME' would affect the resulting output, because there are |
| 21 |
more merging rules when this option is in effect. Other options affect |
| 22 |
the result: `-d', `-g' and, notably, `-s'. |
| 23 |
|
| 24 |
All non-option arguments are interpreted as charset names. If any is given, |
| 25 |
the study is limited to those recodings having any of the given charsets |
| 26 |
both as a starting and ending points. If there is no such non-option |
| 27 |
argument, all possible possible recodings are considered. |
| 28 |
""" |
| 29 |
|
| 30 |
import os, sys |
| 31 |
from common import py, Recode |
| 32 |
from common import setup_module, teardown_module |
| 33 |
|
| 34 |
class Test: |
| 35 |
avoid_as_before = b'count-characters', b'dump-with-names', b'flat' |
| 36 |
|
| 37 |
def test_1(self): |
| 38 |
if Recode is None: |
| 39 |
py.test.skip() |
| 40 |
self.outer = Recode.Outer(strict=False) |
| 41 |
self.charsets = sorted(self.outer.all_charsets()) |
| 42 |
for before in self.charsets: |
| 43 |
if before not in self.avoid_as_before: |
| 44 |
yield self.validate, before |
| 45 |
|
| 46 |
def test_2(self): |
| 47 |
if Recode is None: |
| 48 |
py.test.skip() |
| 49 |
self.outer = Recode.Outer(strict=True) |
| 50 |
self.charsets = sorted(self.outer.all_charsets()) |
| 51 |
for before in self.charsets: |
| 52 |
if before not in self.avoid_as_before: |
| 53 |
yield self.validate, before |
| 54 |
|
| 55 |
def validate(self, before): |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
print(before) |
| 61 |
for after in self.charsets: |
| 62 |
if after is not before: |
| 63 |
request = Recode.Request(self.outer) |
| 64 |
request.scan(b'%s..%s' % (before, after)) |
| 65 |
|
| 66 |
def main(*arguments): |
| 67 |
recode_options = [] |
| 68 |
charset_options = [] |
| 69 |
for argument in arguments: |
| 70 |
if arguments[0] == '-': |
| 71 |
recode_options.append(argument) |
| 72 |
else: |
| 73 |
charset_options.append(argument) |
| 74 |
report = Report() |
| 75 |
report.digest_data(file(work_name).readline) |
| 76 |
report.produce_report(sys.stdout.write) |
| 77 |
|
| 78 |
class Report: |
| 79 |
|
| 80 |
def __init__(self): |
| 81 |
self.recode_calls = 0 |
| 82 |
self.original_count = {} |
| 83 |
self.original_example = {} |
| 84 |
self.original_total = 0 |
| 85 |
self.shrunk_count = {} |
| 86 |
self.shrunk_example = {} |
| 87 |
self.shrunk_total = 0 |
| 88 |
|
| 89 |
def digest_data(self, readline): |
| 90 |
lensep = len(os.linesep) |
| 91 |
line = readline() |
| 92 |
while line: |
| 93 |
type, request = line[:-lensep].split(':', 1) |
| 94 |
if type == 'Request': |
| 95 |
steps = self.get_steps(request) |
| 96 |
self.count_original_request(steps, request) |
| 97 |
line = readline() |
| 98 |
if line: |
| 99 |
if len(line[:-lensep].split(':', 1)) != 2: |
| 100 |
print('*', line, end='') |
| 101 |
type, shrunk_to = line[:-lensep].split(':', 1) |
| 102 |
if type == 'Shrunk to': |
| 103 |
steps = self.get_steps(shrunk_to) |
| 104 |
self.count_shrunk_request(steps, shrunk_to) |
| 105 |
line = readline() |
| 106 |
else: |
| 107 |
self.count_shrunk_request(steps, request) |
| 108 |
else: |
| 109 |
self.count_shrunk_request(steps, request) |
| 110 |
else: |
| 111 |
sys.stderr.write('Unrecognized line: ' + line) |
| 112 |
line = readline() |
| 113 |
|
| 114 |
def get_steps(self, text): |
| 115 |
if text == '*mere copy*': |
| 116 |
return 0 |
| 117 |
if text[-1] == '/': |
| 118 |
text = text[:-1] |
| 119 |
text = text.replace('/..', '..') |
| 120 |
count = 0 |
| 121 |
for fragment in text.split('..'): |
| 122 |
count += len(fragment.split('/')) |
| 123 |
return count - 1 |
| 124 |
|
| 125 |
def count_original_request(self, steps, text): |
| 126 |
self.recode_calls += 1 |
| 127 |
if steps in self.original_count: |
| 128 |
self.original_count[steps] += 1 |
| 129 |
else: |
| 130 |
self.original_count[steps] = 1 |
| 131 |
self.original_example[steps] = text.strip() |
| 132 |
if self.original_total == 0: |
| 133 |
self.original_minimum = self.original_maximum = steps |
| 134 |
else: |
| 135 |
if steps < self.original_minimum: |
| 136 |
self.original_minimum = steps |
| 137 |
if steps > self.original_maximum: |
| 138 |
self.original_maximum = steps |
| 139 |
self.original_total = self.original_total + steps |
| 140 |
|
| 141 |
def count_shrunk_request(self, steps, text): |
| 142 |
if steps in self.shrunk_count: |
| 143 |
self.shrunk_count[steps] += 1 |
| 144 |
else: |
| 145 |
self.shrunk_count[steps] = 1 |
| 146 |
self.shrunk_example[steps] = text.strip() |
| 147 |
if self.shrunk_total == 0: |
| 148 |
self.shrunk_minimum = self.shrunk_maximum = steps |
| 149 |
else: |
| 150 |
if steps < self.shrunk_minimum: |
| 151 |
self.shrunk_minimum = steps |
| 152 |
if steps > self.shrunk_maximum: |
| 153 |
self.shrunk_maximum = steps |
| 154 |
self.shrunk_total = self.shrunk_total + steps |
| 155 |
|
| 156 |
def produce_report(self, write): |
| 157 |
if self.recode_calls == 0: |
| 158 |
sys.stderr.write("No call to report\n") |
| 159 |
return |
| 160 |
write("\n" |
| 161 |
"Optimisation Original Shrunk\n" |
| 162 |
" .-------------------\n" |
| 163 |
"Minimum | %2d %2d\n" |
| 164 |
"Maximum | %2d %2d\n" |
| 165 |
"Average | %4.1f %4.1f\n" |
| 166 |
% (self.original_minimum, self.shrunk_minimum, |
| 167 |
self.original_maximum, self.shrunk_maximum, |
| 168 |
float(self.original_total) / float(self.recode_calls), |
| 169 |
float(self.shrunk_total) / float(self.recode_calls))) |
| 170 |
write("\n" |
| 171 |
"Histogram for original requests\n") |
| 172 |
for steps in range(self.original_minimum, self.original_maximum + 1): |
| 173 |
if steps in self.original_count: |
| 174 |
write("%5d steps, %5d times %s\n" |
| 175 |
% (steps, self.original_count[steps], |
| 176 |
self.original_example[steps])) |
| 177 |
write("\n" |
| 178 |
"Histogram for shrunk requests\n") |
| 179 |
for steps in range(self.shrunk_minimum, self.shrunk_maximum + 1): |
| 180 |
if steps in self.shrunk_count: |
| 181 |
write("%5d steps, %5d times %s\n" |
| 182 |
% (steps, self.shrunk_count[steps], |
| 183 |
self.shrunk_example[steps])) |
| 184 |
|
| 185 |
if __name__ == '__main__': |
| 186 |
main(*sys.argv[1:]) |