1# Copyright 2018 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5
6"""Writes a Perf-formated json file with stats about the given web file."""
7
8
9import json
10import os
11import subprocess
12import sys
13
14
15def main():
16  input_file = sys.argv[1]
17  out_dir = sys.argv[2]
18  keystr = sys.argv[3]
19  propstr = sys.argv[4]
20
21  results = {
22    'key': { },
23    'results': { }
24  }
25
26  props = propstr.split(' ')
27  for i in range(0, len(props), 2):
28    results[props[i]] = props[i+1]
29
30  keys = keystr.split(' ')
31  for i in range(0, len(keys), 2):
32    results['key'][keys[i]] = keys[i+1]
33
34  r = {
35    'total_size_bytes': os.path.getsize(input_file)
36  }
37
38  # Make a copy to avoid destroying the hardlinked file.
39  # Swarming hardlinks in the builds from isolated cache.
40  temp_file = input_file + '_tmp'
41  subprocess.check_call(['cp', input_file, temp_file])
42  subprocess.check_call(['gzip', temp_file])
43
44  r['gzip_size_bytes'] = os.path.getsize(temp_file + '.gz')
45
46  name = os.path.basename(input_file)
47
48  results['results'][name] = {
49    # We need this top level layer 'config'/slice
50    # Other analysis methods (e.g. libskia) might have
51    # slices for data on the 'code' section, etc.
52    'default' : r,
53  }
54
55  # Make debugging easier
56  print json.dumps(results, indent=2)
57
58  with open(os.path.join(out_dir, name+'.json'), 'w') as output:
59    output.write(json.dumps(results, indent=2))
60
61
62if '__main__' == __name__:
63  main()
64