• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2020 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
7"""Script to maintain the Telemetry benchmark default results file.
8
9This script allows the user to see and update the set of default
10results to be used in generating reports from running the Telemetry
11benchmarks.
12"""
13
14from __future__ import print_function
15
16__author__ = 'cmtice@google.com (Caroline Tice)'
17
18import os
19import sys
20import json
21
22from cros_utils import misc
23
24Defaults = {}
25
26
27class TelemetryDefaults(object):
28  """Class for handling telemetry default return result fields."""
29
30  DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json'
31
32  def __init__(self):
33    # Get the Crosperf directory; that is where the defaults
34    # file should be.
35    dirname, __ = misc.GetRoot(__file__)
36    fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME)
37    self._filename = fullname
38    self._defaults = {}
39
40  def ReadDefaultsFile(self):
41    if os.path.exists(self._filename):
42      with open(self._filename, 'r', encoding='utf-8') as fp:
43        self._defaults = json.load(fp)
44
45  def WriteDefaultsFile(self):
46    with open(self._filename, 'w', encoding='utf-8') as fp:
47      json.dump(self._defaults, fp, indent=2)
48
49  def ListCurrentDefaults(self, benchmark='all'):
50    # Show user current defaults. By default, show all.  The user
51    # can specify the name of a particular benchmark to see only that
52    # benchmark's default values.
53    if len(self._defaults) == 0:
54      print('The benchmark default results are currently empty.')
55    if benchmark == 'all':
56      for b in self._defaults.keys():
57        results = self._defaults[b]
58        out_str = b + ' : '
59        for r in results:
60          out_str += r + ' '
61        print(out_str)
62    elif benchmark in self._defaults:
63      results = self._defaults[benchmark]
64      out_str = benchmark + ' : '
65      for r in results:
66        out_str += r + ' '
67      print(out_str)
68    else:
69      print("Error:  Unrecognized benchmark '%s'" % benchmark)
70
71  def AddDefault(self, benchmark, result):
72    if benchmark in self._defaults:
73      resultList = self._defaults[benchmark]
74    else:
75      resultList = []
76    resultList.append(result)
77    self._defaults[benchmark] = resultList
78    print("Updated results set for '%s': " % benchmark)
79    print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
80
81  def RemoveDefault(self, benchmark, result):
82    if benchmark in self._defaults:
83      resultList = self._defaults[benchmark]
84      if result in resultList:
85        resultList.remove(result)
86        print("Updated results set for '%s': " % benchmark)
87        print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
88      else:
89        print(
90            "'%s' is not in '%s's default results list." % (result, benchmark))
91    else:
92      print("Cannot find benchmark named '%s'" % benchmark)
93
94  def GetDefault(self):
95    return self._defaults
96
97  def RemoveBenchmark(self, benchmark):
98    if benchmark in self._defaults:
99      del self._defaults[benchmark]
100      print("Deleted benchmark '%s' from list of benchmarks." % benchmark)
101    else:
102      print("Cannot find benchmark named '%s'" % benchmark)
103
104  def RenameBenchmark(self, old_name, new_name):
105    if old_name in self._defaults:
106      resultsList = self._defaults[old_name]
107      del self._defaults[old_name]
108      self._defaults[new_name] = resultsList
109      print("Renamed '%s' to '%s'." % (old_name, new_name))
110    else:
111      print("Cannot find benchmark named '%s'" % old_name)
112
113  def UsageError(self, user_input):
114    # Print error message, then show options
115    print("Error:Invalid user input: '%s'" % user_input)
116    self.ShowOptions()
117
118  def ShowOptions(self):
119    print("""
120Below are the valid user options and their arguments, and an explanation
121of what each option does.  You may either print out the full name of the
122option, or you may use the first letter of the option.  Case (upper or
123lower) does not matter, for the command (case of the result name DOES matter):
124
125    (L)ist                           - List all current defaults
126    (L)ist <benchmark>               - List current defaults for benchmark
127    (H)elp                           - Show this information.
128    (A)dd <benchmark> <result>       - Add a default result for a particular
129                                       benchmark (appends to benchmark's list
130                                       of results, if list already exists)
131    (D)elete <benchmark> <result>    - Delete a default result for a
132                                       particular benchmark
133    (R)emove <benchmark>             - Remove an entire benchmark (and its
134                                       results)
135    (M)ove <old-benchmark> <new-benchmark>    - Rename a benchmark
136    (Q)uit                           - Exit this program, saving changes.
137    (T)erminate                      - Exit this program; abandon changes.
138
139""")
140
141  def GetUserInput(self):
142    # Prompt user
143    print('Enter option> ')
144    # Process user input
145    inp = sys.stdin.readline()
146    inp = inp[:-1]
147    # inp = inp.lower()
148    words = inp.split(' ')
149    option = words[0]
150    option = option.lower()
151    if option in ('h', 'help'):
152      self.ShowOptions()
153    elif option in ('l', 'list'):
154      if len(words) == 1:
155        self.ListCurrentDefaults()
156      else:
157        self.ListCurrentDefaults(benchmark=words[1])
158    elif option in ('a', 'add'):
159      if len(words) < 3:
160        self.UsageError(inp)
161      else:
162        benchmark = words[1]
163        resultList = words[2:]
164        for r in resultList:
165          self.AddDefault(benchmark, r)
166    elif option in ('d', 'delete'):
167      if len(words) != 3:
168        self.UsageError(inp)
169      else:
170        benchmark = words[1]
171        result = words[2]
172        self.RemoveDefault(benchmark, result)
173    elif option in ('r', 'remove'):
174      if len(words) != 2:
175        self.UsageError(inp)
176      else:
177        benchmark = words[1]
178        self.RemoveBenchmark(benchmark)
179    elif option in ('m', 'move'):
180      if len(words) != 3:
181        self.UsageError(inp)
182      else:
183        old_name = words[1]
184        new_name = words[2]
185        self.RenameBenchmark(old_name, new_name)
186    elif option in ('q', 'quit'):
187      self.WriteDefaultsFile()
188
189    return option in ('q', 'quit', 't', 'terminate')
190
191
192def Main():
193  defaults = TelemetryDefaults()
194  defaults.ReadDefaultsFile()
195  defaults.ShowOptions()
196  done = defaults.GetUserInput()
197  while not done:
198    done = defaults.GetUserInput()
199  return 0
200
201
202if __name__ == '__main__':
203  retval = Main()
204  sys.exit(retval)
205