Extend framework_test with cf_segments

This will allow us to benchmark cf_segments.

Change-Id: Id1956301e9feacbb5de10adbcd16052ab0971301
diff --git a/tools/test_r8cfsegments.py b/tools/test_r8cfsegments.py
new file mode 100755
index 0000000..d160b03
--- /dev/null
+++ b/tools/test_r8cfsegments.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+# Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+# for details. All rights reserved. Use of this source code is governed by a
+# BSD-style license that can be found in the LICENSE file.
+
+# Run R8 or PG on 'third_party/r8/r8.jar'.
+# Report Golem-compatible CodeSize and RunTimeRaw values:
+#
+#     <NAME>-Total(CodeSize): <size>
+#     <NAME>-Total(RunTimeRaw>: <time> ms
+#
+# and also detailed segment sizes for each classfile segment:
+#
+#    <NAME>-Code(CodeSize): <size>
+#    <NAME>-AnnotationSets(CodeSize): <size>
+#    ...
+#
+# Uses the R8CfSegments Java tool (Gradle target).
+
+from __future__ import print_function
+import argparse
+import golem
+import minify_tool
+import os
+import sys
+import utils
+
+def parse_arguments():
+  parser = argparse.ArgumentParser(
+      description = 'Run R8 or PG on'
+          ' third_party/r8/r8.jar.'
+          ' Report Golem-compatible CodeSize and RunTimeRaw values.')
+  parser.add_argument('--tool',
+      choices = ['pg', 'r8'],
+      required = True,
+      help = 'Compiler tool to use.')
+  parser.add_argument('--golem',
+      help = 'Running on golem, link in third_party resources.',
+      default = False,
+      action = 'store_true')
+  parser.add_argument('--name',
+      required = True,
+      help = 'Results will be printed using the specified benchmark name (e.g.'
+          ' <NAME>-<segment>(CodeSize): <bytes>), the full size is reported'
+          ' with <NAME>-Total(CodeSize)')
+  parser.add_argument('--print-memoryuse',
+      help = 'Prints the line \'<NAME>-Total(MemoryUse):'
+             ' <mem>\' at the end where <mem> is the peak'
+             ' peak resident set size (VmHWM) in bytes.',
+      default = False,
+      action = 'store_true')
+  parser.add_argument('--output',
+                      help = 'Output directory to keep the generated files')
+  return parser.parse_args()
+
+
+def Main():
+  utils.check_java_version()
+  args = parse_arguments()
+  output_dir = args.output
+  if args.golem:
+    golem.link_third_party()
+  with utils.TempDir() as temp_dir:
+    if not output_dir:
+      output_dir = temp_dir
+    track_memory_file = None
+    if args.print_memoryuse:
+      track_memory_file = os.path.join(output_dir, utils.MEMORY_USE_TMP_FILE)
+    if args.tool == 'pg':
+      utils.print_cfsegments(args.name, [utils.PINNED_PGR8_JAR])
+    else:
+      out_file = os.path.join(output_dir, 'out.jar')
+      return_code = minify_tool.minify_tool(
+          input_jar=utils.PINNED_R8_JAR,
+          output_jar=out_file,
+          debug=False,
+          build=False,
+          track_memory_file=track_memory_file,
+          benchmark_name=args.name + "-Total")
+      if return_code != 0:
+        sys.exit(return_code)
+
+      utils.print_cfsegments(args.name, [out_file])
+
+
+if __name__ == '__main__':
+  sys.exit(Main())