Home | History | Annotate | Download | only in user_activity_benchmarks
      1 #!/usr/bin/python2
      2 
      3 # Copyright 2016 The Chromium OS Authors. All rights reserved.
      4 # Use of this source code is governed by a BSD-style license that can be
      5 # found in the LICENSE file.
      6 """Unit tests for the benchmark_metrics module."""
      7 
      8 import mock
      9 import unittest
     10 import benchmark_metrics
     11 
     12 
     13 class MetricsComputationTest(unittest.TestCase):
     14   """Test class for MetricsComputation class."""
     15 
     16   def __init__(self, *args, **kwargs):
     17     super(MetricsComputationTest, self).__init__(*args, **kwargs)
     18 
     19   def testComputeDistanceForFunction(self):
     20     child_functions_statistics_sample = {
     21         'f,file_f': 0.1,
     22         'g,file_g': 0.2,
     23         'h,file_h': 0.3,
     24         'i,file_i': 0.4
     25     }
     26     child_functions_statistics_reference = {
     27         'f,file_f': 0.4,
     28         'i,file_i': 0.4,
     29         'h,file_h2': 0.2
     30     }
     31     distance = benchmark_metrics.ComputeDistanceForFunction(
     32         child_functions_statistics_sample, child_functions_statistics_reference)
     33     self.assertEqual(distance, 2.0)
     34 
     35     distance = benchmark_metrics.ComputeDistanceForFunction({}, {})
     36     self.assertEqual(distance, 1.0)
     37 
     38     distance = benchmark_metrics.ComputeDistanceForFunction(
     39         child_functions_statistics_sample, {})
     40     self.assertEqual(distance, 2.0)
     41 
     42     distance = benchmark_metrics.ComputeDistanceForFunction(
     43         {}, child_functions_statistics_reference)
     44     self.assertEqual(distance, 2.0)
     45 
     46   def testComputeScoreForFunction(self):
     47     score = benchmark_metrics.ComputeScoreForFunction(1.2, 0.3, 0.4)
     48     self.assertEqual(score, 0.1)
     49 
     50   def testComputeMetricsForComponents(self):
     51     function_metrics = {
     52         'func_f,/a/b/file_f': (1.0, 2.3),
     53         'func_g,/a/b/file_g': (1.1, 1.5),
     54         'func_h,/c/d/file_h': (2.0, 1.7),
     55         'func_i,/c/d/file_i': (1.9, 1.8),
     56         'func_j,/c/d/file_j': (1.8, 1.9),
     57         'func_k,/e/file_k': (1.2, 2.1),
     58         'func_l,/e/file_l': (1.3, 3.1)
     59     }
     60     cwp_function_groups = [('ab', '/a/b'), ('cd', '/c/d'), ('e', '/e')]
     61     expected_metrics = {'ab': ('/a/b', 2.0, 2.1, 1.05, 3.8, 1.9),
     62                         'e': ('/e', 2.0, 2.5, 1.25, 5.2, 2.6),
     63                         'cd': ('/c/d', 3.0, 5.7, 1.9000000000000001, 5.4, 1.8)}
     64     result_metrics = benchmark_metrics.ComputeMetricsForComponents(
     65         cwp_function_groups, function_metrics)
     66 
     67     self.assertDictEqual(expected_metrics, result_metrics)
     68 
     69   def testComputeMetricsForBenchmark(self):
     70     function_metrics = {'func_f': (1.0, 2.0),
     71                         'func_g': (1.1, 2.1),
     72                         'func_h': (1.2, 2.2),
     73                         'func_i': (1.3, 2.3)}
     74     expected_benchmark_metrics = \
     75         (4, 4.6000000000000005, 1.1500000000000001, 8.6, 2.15)
     76     result_benchmark_metrics = \
     77         benchmark_metrics.ComputeMetricsForBenchmark(function_metrics)
     78 
     79     self.assertEqual(expected_benchmark_metrics, result_benchmark_metrics)
     80 
     81   def testComputeMetricsForBenchmarkSet(self):
     82     """TODO(evelinad): Add unit test for ComputeMetricsForBenchmarkSet."""
     83     pass
     84 
     85 
     86 if __name__ == '__main__':
     87   unittest.main()
     88