tp: merge test/metrics into test/trace_processor

These were separate becaue they developed somewhat independently but
have grown increasingly the same over time. Because we have a consistent
naming system for query tests (file ends with .sql), just merge the two
folders together and rely on the extension to define whether the test is
a query test or metric test.

Change-Id: Ia73960739260971aa40352970d765ef27eb00271
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index f661eba..8378ac1 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -25,9 +25,8 @@
         white_list=".*",
         black_list=[
             'Android[.]bp', '.*[.]json$', '.*[.]sql$', '.*[.]out$',
-            'test/trace_processor/.*/index$', 'test/metrics/.*/index$',
-            '.*\bBUILD$', 'WORKSPACE', '.*/Makefile$',
-            '/perfetto_build_flags.h$'
+            'test/trace_processor/.*/index$', '.*\bBUILD$', 'WORKSPACE',
+            '.*/Makefile$', '/perfetto_build_flags.h$'
         ])
 
   results = []
diff --git a/docs/contributing/common-tasks.md b/docs/contributing/common-tasks.md
index 2bb5c87..31987c8 100644
--- a/docs/contributing/common-tasks.md
+++ b/docs/contributing/common-tasks.md
@@ -23,8 +23,10 @@
 4. Add a new SQL file for the metric to [src/trace_processor/metrics](/src/trace_processor/metrics). The appropriate `BUILD.gn` file should be updated as well.
   * To learn how to write new metrics, see the [trace-based metrics documentation](/docs/analysis/metrics.md).
 5. Build all targets in your out directory with `tools/ninja -C out/YOUR_BUILD_DIRECTORY`.
-6. Add a new diff test for the metric. This can be done by adding files to the [test/metrics](/test/metrics) folder and modifying one of the index files listed in
-[/test/metrics/include_index](/test/metrics/include_index).
+6. Add a new diff test for the metric. This can be done by adding files to
+the [test/trace_processor](/test/trace_processor) folder and modifying one
+of the index files listed in
+[/test/trace_processor/include_index](/test/trace_processor/include_index).
 7. Run the newly added test with `tools/diff_test_trace_processor.py <path to trace processor binary>`.
 8. Upload and land your change as normal.
 
diff --git a/test/metrics/include_index b/test/metrics/include_index
deleted file mode 100644
index 5109af1..0000000
--- a/test/metrics/include_index
+++ /dev/null
@@ -1,2 +0,0 @@
-legacy/index
-profiling/index
\ No newline at end of file
diff --git a/test/metrics/legacy/index b/test/metrics/legacy/index
deleted file mode 100644
index fe1be68..0000000
--- a/test/metrics/legacy/index
+++ /dev/null
@@ -1,29 +0,0 @@
-# Real traces
-../../data/memory_counters.pb android_mem android_mem_counters.out
-../../data/memory_counters.pb trace_metadata trace_metadata.out
-
-# Synthetic traces
-android_mem_by_priority.py android_mem android_mem_by_priority.out
-android_lmk.py android_lmk android_mem_lmk.out
-android_lmk_reason.py android_lmk_reason android_lmk_reason.out
-../../trace_processor/legacy/oom_kill.textproto android_lmk android_lmk_oom.out
-
-android_ion.py android_ion android_ion.out
-android_ion_stat.textproto android_ion android_ion_stat.out
-
-android_startup.py android_startup android_startup.out
-android_startup_breakdown.py android_startup android_startup_breakdown.out
-android_startup_process_track.py android_startup android_startup_process_track.out
-
-android_startup_battery.py android_batt android_batt_counters.out
-android_startup_cpu.py android_cpu android_startup_cpu.out
-android_startup_powrails.py android_powrails android_startup_powrails.out
-
-android_package_list.py android_package_list android_package_list.out
-
-# Json output
-../../data/memory_counters.pb trace_metadata trace_metadata.json.out
-
-process_uids.textproto android_task_names android_task_names.out
-
-../../trace_processor/legacy/thread_time_in_state.textproto android_thread_time_in_state android_thread_time_in_state.out
diff --git a/test/metrics/profiling/index b/test/metrics/profiling/index
deleted file mode 100644
index ab3688b..0000000
--- a/test/metrics/profiling/index
+++ /dev/null
@@ -1,7 +0,0 @@
-heap_profile.textproto heap_profile_callsites heap_profile_callsites.out
-heap_profile_no_symbols.textproto unsymbolized_frames unsymbolized_frames.out
-
-../../trace_processor/profiling/heap_graph.textproto java_heap_stats java_heap_stats.out
-heap_graph_closest_proc.textproto java_heap_stats heap_stats_closest_proc.out
-../../trace_processor/profiling/heap_graph.textproto java_heap_histogram java_heap_histogram.out
-obfuscated_heap_graph.textproto unmapped_java_symbols unmapped_java_symbols.out
diff --git a/test/metrics/legacy/android_batt_counters.out b/test/trace_processor/legacy/android_batt_counters.out
similarity index 100%
rename from test/metrics/legacy/android_batt_counters.out
rename to test/trace_processor/legacy/android_batt_counters.out
diff --git a/test/metrics/legacy/android_ion.out b/test/trace_processor/legacy/android_ion.out
similarity index 100%
rename from test/metrics/legacy/android_ion.out
rename to test/trace_processor/legacy/android_ion.out
diff --git a/test/metrics/legacy/android_ion.py b/test/trace_processor/legacy/android_ion.py
similarity index 100%
rename from test/metrics/legacy/android_ion.py
rename to test/trace_processor/legacy/android_ion.py
diff --git a/test/metrics/legacy/android_ion_stat.out b/test/trace_processor/legacy/android_ion_stat.out
similarity index 100%
rename from test/metrics/legacy/android_ion_stat.out
rename to test/trace_processor/legacy/android_ion_stat.out
diff --git a/test/metrics/legacy/android_ion_stat.textproto b/test/trace_processor/legacy/android_ion_stat.textproto
similarity index 100%
rename from test/metrics/legacy/android_ion_stat.textproto
rename to test/trace_processor/legacy/android_ion_stat.textproto
diff --git a/test/metrics/legacy/android_lmk.py b/test/trace_processor/legacy/android_lmk.py
similarity index 100%
rename from test/metrics/legacy/android_lmk.py
rename to test/trace_processor/legacy/android_lmk.py
diff --git a/test/metrics/legacy/android_lmk_oom.out b/test/trace_processor/legacy/android_lmk_oom.out
similarity index 100%
rename from test/metrics/legacy/android_lmk_oom.out
rename to test/trace_processor/legacy/android_lmk_oom.out
diff --git a/test/metrics/legacy/android_lmk_reason.out b/test/trace_processor/legacy/android_lmk_reason.out
similarity index 100%
rename from test/metrics/legacy/android_lmk_reason.out
rename to test/trace_processor/legacy/android_lmk_reason.out
diff --git a/test/metrics/legacy/android_lmk_reason.py b/test/trace_processor/legacy/android_lmk_reason.py
similarity index 100%
rename from test/metrics/legacy/android_lmk_reason.py
rename to test/trace_processor/legacy/android_lmk_reason.py
diff --git a/test/metrics/legacy/android_mem_by_priority.out b/test/trace_processor/legacy/android_mem_by_priority.out
similarity index 100%
rename from test/metrics/legacy/android_mem_by_priority.out
rename to test/trace_processor/legacy/android_mem_by_priority.out
diff --git a/test/metrics/legacy/android_mem_by_priority.py b/test/trace_processor/legacy/android_mem_by_priority.py
similarity index 100%
rename from test/metrics/legacy/android_mem_by_priority.py
rename to test/trace_processor/legacy/android_mem_by_priority.py
diff --git a/test/metrics/legacy/android_mem_counters.out b/test/trace_processor/legacy/android_mem_counters.out
similarity index 100%
rename from test/metrics/legacy/android_mem_counters.out
rename to test/trace_processor/legacy/android_mem_counters.out
diff --git a/test/metrics/legacy/android_mem_lmk.out b/test/trace_processor/legacy/android_mem_lmk.out
similarity index 100%
rename from test/metrics/legacy/android_mem_lmk.out
rename to test/trace_processor/legacy/android_mem_lmk.out
diff --git a/test/metrics/legacy/android_package_list.out b/test/trace_processor/legacy/android_package_list.out
similarity index 100%
rename from test/metrics/legacy/android_package_list.out
rename to test/trace_processor/legacy/android_package_list.out
diff --git a/test/metrics/legacy/android_package_list.py b/test/trace_processor/legacy/android_package_list.py
similarity index 100%
rename from test/metrics/legacy/android_package_list.py
rename to test/trace_processor/legacy/android_package_list.py
diff --git a/test/metrics/legacy/android_startup.out b/test/trace_processor/legacy/android_startup.out
similarity index 100%
rename from test/metrics/legacy/android_startup.out
rename to test/trace_processor/legacy/android_startup.out
diff --git a/test/metrics/legacy/android_startup.py b/test/trace_processor/legacy/android_startup.py
similarity index 100%
rename from test/metrics/legacy/android_startup.py
rename to test/trace_processor/legacy/android_startup.py
diff --git a/test/metrics/legacy/android_startup_battery.py b/test/trace_processor/legacy/android_startup_battery.py
similarity index 100%
rename from test/metrics/legacy/android_startup_battery.py
rename to test/trace_processor/legacy/android_startup_battery.py
diff --git a/test/metrics/legacy/android_startup_breakdown.out b/test/trace_processor/legacy/android_startup_breakdown.out
similarity index 100%
rename from test/metrics/legacy/android_startup_breakdown.out
rename to test/trace_processor/legacy/android_startup_breakdown.out
diff --git a/test/metrics/legacy/android_startup_breakdown.py b/test/trace_processor/legacy/android_startup_breakdown.py
similarity index 100%
rename from test/metrics/legacy/android_startup_breakdown.py
rename to test/trace_processor/legacy/android_startup_breakdown.py
diff --git a/test/metrics/legacy/android_startup_cpu.out b/test/trace_processor/legacy/android_startup_cpu.out
similarity index 100%
rename from test/metrics/legacy/android_startup_cpu.out
rename to test/trace_processor/legacy/android_startup_cpu.out
diff --git a/test/metrics/legacy/android_startup_cpu.py b/test/trace_processor/legacy/android_startup_cpu.py
similarity index 100%
rename from test/metrics/legacy/android_startup_cpu.py
rename to test/trace_processor/legacy/android_startup_cpu.py
diff --git a/test/metrics/legacy/android_startup_powrails.out b/test/trace_processor/legacy/android_startup_powrails.out
similarity index 100%
rename from test/metrics/legacy/android_startup_powrails.out
rename to test/trace_processor/legacy/android_startup_powrails.out
diff --git a/test/metrics/legacy/android_startup_powrails.py b/test/trace_processor/legacy/android_startup_powrails.py
similarity index 100%
rename from test/metrics/legacy/android_startup_powrails.py
rename to test/trace_processor/legacy/android_startup_powrails.py
diff --git a/test/metrics/legacy/android_startup_process_track.out b/test/trace_processor/legacy/android_startup_process_track.out
similarity index 100%
rename from test/metrics/legacy/android_startup_process_track.out
rename to test/trace_processor/legacy/android_startup_process_track.out
diff --git a/test/metrics/legacy/android_startup_process_track.py b/test/trace_processor/legacy/android_startup_process_track.py
similarity index 95%
rename from test/metrics/legacy/android_startup_process_track.py
rename to test/trace_processor/legacy/android_startup_process_track.py
index 9636990..a9696ad 100644
--- a/test/metrics/legacy/android_startup_process_track.py
+++ b/test/trace_processor/legacy/android_startup_process_track.py
@@ -15,7 +15,6 @@
 
 from os import sys, path
 
-sys.path.append(path.dirname(path.dirname(path.dirname(path.abspath(__file__)))))
 import synth_common
 
 
diff --git a/test/metrics/legacy/android_task_names.out b/test/trace_processor/legacy/android_task_names.out
similarity index 100%
rename from test/metrics/legacy/android_task_names.out
rename to test/trace_processor/legacy/android_task_names.out
diff --git a/test/metrics/legacy/android_thread_time_in_state.out b/test/trace_processor/legacy/android_thread_time_in_state.out
similarity index 100%
rename from test/metrics/legacy/android_thread_time_in_state.out
rename to test/trace_processor/legacy/android_thread_time_in_state.out
diff --git a/test/trace_processor/legacy/index b/test/trace_processor/legacy/index
index dd0f53c..c473e10 100644
--- a/test/trace_processor/legacy/index
+++ b/test/trace_processor/legacy/index
@@ -91,3 +91,33 @@
 
 # perf_sample table (traced_perf trace as an input).
 ../../data/perf_sample.pb perf_sample.sql perf_sample_perf_sample.out
+
+# Real traces
+../../data/memory_counters.pb android_mem android_mem_counters.out
+../../data/memory_counters.pb trace_metadata trace_metadata.out
+
+# Synthetic traces
+android_mem_by_priority.py android_mem android_mem_by_priority.out
+android_lmk.py android_lmk android_mem_lmk.out
+android_lmk_reason.py android_lmk_reason android_lmk_reason.out
+../../trace_processor/legacy/oom_kill.textproto android_lmk android_lmk_oom.out
+
+android_ion.py android_ion android_ion.out
+android_ion_stat.textproto android_ion android_ion_stat.out
+
+android_startup.py android_startup android_startup.out
+android_startup_breakdown.py android_startup android_startup_breakdown.out
+android_startup_process_track.py android_startup android_startup_process_track.out
+
+android_startup_battery.py android_batt android_batt_counters.out
+android_startup_cpu.py android_cpu android_startup_cpu.out
+android_startup_powrails.py android_powrails android_startup_powrails.out
+
+android_package_list.py android_package_list android_package_list.out
+
+# Json output
+../../data/memory_counters.pb trace_metadata trace_metadata.json.out
+
+process_uids.textproto android_task_names android_task_names.out
+
+../../trace_processor/legacy/thread_time_in_state.textproto android_thread_time_in_state android_thread_time_in_state.out
diff --git a/test/metrics/legacy/process_uids.textproto b/test/trace_processor/legacy/process_uids.textproto
similarity index 100%
rename from test/metrics/legacy/process_uids.textproto
rename to test/trace_processor/legacy/process_uids.textproto
diff --git a/test/metrics/legacy/trace_metadata.json.out b/test/trace_processor/legacy/trace_metadata.json.out
similarity index 100%
rename from test/metrics/legacy/trace_metadata.json.out
rename to test/trace_processor/legacy/trace_metadata.json.out
diff --git a/test/metrics/legacy/trace_metadata.out b/test/trace_processor/legacy/trace_metadata.out
similarity index 100%
rename from test/metrics/legacy/trace_metadata.out
rename to test/trace_processor/legacy/trace_metadata.out
diff --git a/test/metrics/profiling/heap_graph_closest_proc.textproto b/test/trace_processor/profiling/heap_graph_closest_proc.textproto
similarity index 100%
rename from test/metrics/profiling/heap_graph_closest_proc.textproto
rename to test/trace_processor/profiling/heap_graph_closest_proc.textproto
diff --git a/test/metrics/profiling/heap_profile.textproto b/test/trace_processor/profiling/heap_profile.textproto
similarity index 100%
rename from test/metrics/profiling/heap_profile.textproto
rename to test/trace_processor/profiling/heap_profile.textproto
diff --git a/test/metrics/profiling/heap_profile_callsites.out b/test/trace_processor/profiling/heap_profile_callsites.out
similarity index 100%
rename from test/metrics/profiling/heap_profile_callsites.out
rename to test/trace_processor/profiling/heap_profile_callsites.out
diff --git a/test/metrics/profiling/heap_profile_no_symbols.textproto b/test/trace_processor/profiling/heap_profile_no_symbols.textproto
similarity index 100%
rename from test/metrics/profiling/heap_profile_no_symbols.textproto
rename to test/trace_processor/profiling/heap_profile_no_symbols.textproto
diff --git a/test/metrics/profiling/heap_stats_closest_proc.out b/test/trace_processor/profiling/heap_stats_closest_proc.out
similarity index 100%
rename from test/metrics/profiling/heap_stats_closest_proc.out
rename to test/trace_processor/profiling/heap_stats_closest_proc.out
diff --git a/test/trace_processor/profiling/index b/test/trace_processor/profiling/index
index 5a2bc87..a880dd1 100644
--- a/test/trace_processor/profiling/index
+++ b/test/trace_processor/profiling/index
@@ -6,7 +6,6 @@
 heap_graph_baseapk.textproto heap_graph_object.sql heap_graph_object.out
 heap_graph_baseapk.textproto heap_graph_reference.sql heap_graph_reference.out
 
-
 heap_graph_duplicate.textproto heap_graph_flamegraph.sql heap_graph_duplicate_flamegraph.out
 
 heap_graph.textproto heap_graph_flamegraph.sql heap_graph_flamegraph.out
@@ -29,3 +28,12 @@
 heap_graph_superclass.textproto heap_graph_superclass.sql heap_graph_superclass.out
 
 stack_profile_tracker_empty_callstack.textproto stack_profile_tracker_empty_callstack.sql stack_profile_tracker_empty_callstack.out
+
+# Metrics
+heap_profile.textproto heap_profile_callsites heap_profile_callsites.out
+heap_profile_no_symbols.textproto unsymbolized_frames unsymbolized_frames.out
+
+../../trace_processor/profiling/heap_graph.textproto java_heap_stats java_heap_stats.out
+heap_graph_closest_proc.textproto java_heap_stats heap_stats_closest_proc.out
+../../trace_processor/profiling/heap_graph.textproto java_heap_histogram java_heap_histogram.out
+obfuscated_heap_graph.textproto unmapped_java_symbols unmapped_java_symbols.out
diff --git a/test/metrics/profiling/java_heap_histogram.out b/test/trace_processor/profiling/java_heap_histogram.out
similarity index 100%
rename from test/metrics/profiling/java_heap_histogram.out
rename to test/trace_processor/profiling/java_heap_histogram.out
diff --git a/test/metrics/profiling/java_heap_stats.out b/test/trace_processor/profiling/java_heap_stats.out
similarity index 100%
rename from test/metrics/profiling/java_heap_stats.out
rename to test/trace_processor/profiling/java_heap_stats.out
diff --git a/test/metrics/profiling/obfuscated_heap_graph.textproto b/test/trace_processor/profiling/obfuscated_heap_graph.textproto
similarity index 100%
rename from test/metrics/profiling/obfuscated_heap_graph.textproto
rename to test/trace_processor/profiling/obfuscated_heap_graph.textproto
diff --git a/test/metrics/profiling/unmapped_java_symbols.out b/test/trace_processor/profiling/unmapped_java_symbols.out
similarity index 100%
rename from test/metrics/profiling/unmapped_java_symbols.out
rename to test/trace_processor/profiling/unmapped_java_symbols.out
diff --git a/test/metrics/profiling/unsymbolized_frames.out b/test/trace_processor/profiling/unsymbolized_frames.out
similarity index 100%
rename from test/metrics/profiling/unsymbolized_frames.out
rename to test/trace_processor/profiling/unsymbolized_frames.out
diff --git a/tools/diff_test_trace_processor.py b/tools/diff_test_trace_processor.py
index 61ea9c2..c9298b5 100755
--- a/tools/diff_test_trace_processor.py
+++ b/tools/diff_test_trace_processor.py
@@ -57,20 +57,6 @@
     self.real_time_ns = int(real_time_ns_str)
 
 
-def create_metrics_message_factory(metrics_descriptor_path):
-  return create_message_factory(metrics_descriptor_path,
-                                'perfetto.protos.TraceMetrics')
-
-
-def write_diff(expected, actual):
-  expected_lines = expected.splitlines(True)
-  actual_lines = actual.splitlines(True)
-  diff = difflib.unified_diff(
-      expected_lines, actual_lines, fromfile='expected', tofile='actual')
-  for line in diff:
-    sys.stderr.write(line)
-
-
 class TestResult(object):
 
   def __init__(self, test_type, input_name, trace, cmd, expected, actual,
@@ -85,6 +71,20 @@
     self.exit_code = exit_code
 
 
+def create_metrics_message_factory(metrics_descriptor_path):
+  return create_message_factory(metrics_descriptor_path,
+                                'perfetto.protos.TraceMetrics')
+
+
+def write_diff(expected, actual):
+  expected_lines = expected.splitlines(True)
+  actual_lines = actual.splitlines(True)
+  diff = difflib.unified_diff(
+      expected_lines, actual_lines, fromfile='expected', tofile='actual')
+  for line in diff:
+    sys.stderr.write(line)
+
+
 def run_metrics_test(trace_processor_path, gen_trace_path, metric,
                      expected_path, perf_path, metrics_message_factory):
   with open(expected_path, 'r') as expected_file:
@@ -215,7 +215,7 @@
             'Command to generate trace:\n'
             'tools/serialize_test_trace.py --descriptor {} {} > {}\n'.format(
                 os.path.relpath(trace_descriptor_path, ROOT_DIR),
-                os.path.relpath(trace_descriptor_path, trace_path),
+                os.path.relpath(trace_path, ROOT_DIR),
                 os.path.relpath(gen_trace_path, ROOT_DIR)))
       sys.stderr.write('Command line:\n{}\n'.format(' '.join(result.cmd)))
 
@@ -256,8 +256,7 @@
   return test_failure, perf_data
 
 
-def read_all_tests_from_index(test_type, index_path, query_metric_pattern,
-                              trace_pattern):
+def read_all_tests_from_index(index_path, query_metric_pattern, trace_pattern):
   index_dir = os.path.dirname(index_path)
 
   with open(index_path, 'r') as index_file:
@@ -281,32 +280,28 @@
     trace_path = os.path.abspath(os.path.join(index_dir, trace_fname))
     expected_path = os.path.abspath(os.path.join(index_dir, expected_fname))
 
-    query_path_or_metric = query_fname_or_metric
-    if test_type == 'queries':
+    if query_fname_or_metric.endswith('.sql'):
+      test_type = 'queries'
       query_path_or_metric = os.path.abspath(
           os.path.join(index_dir, query_fname_or_metric))
+    else:
+      test_type = 'metrics'
+      query_path_or_metric = query_fname_or_metric
 
     tests.append(
         Test(test_type, trace_path, query_path_or_metric, expected_path))
   return tests
 
 
-def read_all_tests(test_type, query_metric_pattern, trace_pattern):
-  if test_type == 'queries':
-    include_index = os.path.join(ROOT_DIR, 'test', 'trace_processor',
-                                 'include_index')
-  elif test_type == 'metrics':
-    include_index = os.path.join(ROOT_DIR, 'test', 'metrics', 'include_index')
-  else:
-    assert False
-
-  include_index_dir = os.path.dirname(include_index)
+def read_all_tests(query_metric_pattern, trace_pattern):
+  include_index_dir = os.path.join(ROOT_DIR, 'test', 'trace_processor')
+  include_index = os.path.join(include_index_dir, 'include_index')
   tests = []
   with open(include_index, 'r') as include_file:
     for index_relpath in include_file.readlines():
       index_path = os.path.join(include_index_dir, index_relpath.strip())
       tests.extend(
-          read_all_tests_from_index(test_type, index_path, query_metric_pattern,
+          read_all_tests_from_index(index_path, query_metric_pattern,
                                     trace_pattern))
   return tests
 
@@ -336,22 +331,10 @@
       'trace_processor', type=str, help='location of trace processor binary')
   args = parser.parse_args()
 
-  test_type = args.test_type
-  if test_type != 'all' and test_type != 'queries' and test_type != 'metrics':
-    print('Unknown test type {}. Supported: all, queries, metrics'.format(
-        test_type))
-    return 1
-
   query_metric_pattern = re.compile(args.query_metric_filter)
   trace_pattern = re.compile(args.trace_filter)
 
-  tests = []
-  if test_type == 'all' or test_type == 'metrics':
-    tests += read_all_tests('metrics', query_metric_pattern, trace_pattern)
-
-  if test_type == 'all' or test_type == 'queries':
-    tests += read_all_tests('queries', query_metric_pattern, trace_pattern)
-
+  tests = read_all_tests(query_metric_pattern, trace_pattern)
   sys.stderr.write('[==========] Running {} tests.\n'.format(len(tests)))
 
   if args.trace_descriptor: