| 1 | #!/usr/bin/env python2
 | 
| 2 | """
 | 
| 3 | files.py
 | 
| 4 | """
 | 
| 5 | from __future__ import print_function
 | 
| 6 | 
 | 
| 7 | import os
 | 
| 8 | import sys
 | 
| 9 | 
 | 
| 10 | from mycpp import mylib
 | 
| 11 | from mycpp.mylib import log
 | 
| 12 | 
 | 
| 13 | 
 | 
| 14 | def run_tests():
 | 
| 15 |     # type: () -> None
 | 
| 16 | 
 | 
| 17 |     f = mylib.BufWriter()
 | 
| 18 |     for i in xrange(30):
 | 
| 19 |         f.write(chr(i + 65))
 | 
| 20 | 
 | 
| 21 |     contents = f.getvalue()
 | 
| 22 |     log('Wrote %d bytes to StringIO', len(contents))
 | 
| 23 | 
 | 
| 24 |     # the f(g(), h()) problem with mark and sweep!!!
 | 
| 25 |     if 1:
 | 
| 26 |         log('contents = %s ... %s', contents[:10], contents[-10:])
 | 
| 27 | 
 | 
| 28 |     # Does not have the problem
 | 
| 29 |     if 0:
 | 
| 30 |         a = contents[:10]
 | 
| 31 |         b = contents[-10:]
 | 
| 32 |         log('contents = %s ... %s', a, b)
 | 
| 33 | 
 | 
| 34 |     f2 = mylib.Stdout()
 | 
| 35 |     f2.write('stdout\n')
 | 
| 36 | 
 | 
| 37 | 
 | 
| 38 | def run_benchmarks():
 | 
| 39 |     # type: () -> None
 | 
| 40 |     n = 10000
 | 
| 41 | 
 | 
| 42 |     result = 0
 | 
| 43 | 
 | 
| 44 |     i = 0
 | 
| 45 |     while i < n:
 | 
| 46 |         f = mylib.BufWriter()
 | 
| 47 |         for j in xrange(30):
 | 
| 48 |             f.write(chr(j + 65))
 | 
| 49 | 
 | 
| 50 |         result += len(f.getvalue())
 | 
| 51 | 
 | 
| 52 |         mylib.MaybeCollect()
 | 
| 53 | 
 | 
| 54 |         i += 1
 | 
| 55 |     log('Ran %d iterations', n)
 | 
| 56 |     log('result = %d', result)
 | 
| 57 | 
 | 
| 58 | 
 | 
| 59 | if __name__ == '__main__':
 | 
| 60 |     if os.getenv('BENCHMARK'):
 | 
| 61 |         log('Benchmarking...')
 | 
| 62 |         run_benchmarks()
 | 
| 63 |     else:
 | 
| 64 |         run_tests()
 |