summaryrefslogtreecommitdiff
path: root/site_scons
diff options
context:
space:
mode:
authorDaniel Moody <daniel.moody@mongodb.com>2022-06-18 05:13:13 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-06-18 05:42:00 +0000
commita76e4ce18640287c3ddd839c7cb820f63b672746 (patch)
tree723f2f6ceb1b882d2cef10110f2c54add4c7b2bd /site_scons
parent0d5aceed10e616e21d37b1e8993797e2f55cd741 (diff)
downloadmongo-a76e4ce18640287c3ddd839c7cb820f63b672746.tar.gz
SERVER-67109 added json validator for build metrics tool
Diffstat (limited to 'site_scons')
-rw-r--r--site_scons/site_tools/build_metrics/__init__.py96
-rw-r--r--site_scons/site_tools/build_metrics/build_metrics_format.schema10
2 files changed, 106 insertions, 0 deletions
diff --git a/site_scons/site_tools/build_metrics/__init__.py b/site_scons/site_tools/build_metrics/__init__.py
new file mode 100644
index 00000000000..0f3bd6c078d
--- /dev/null
+++ b/site_scons/site_tools/build_metrics/__init__.py
@@ -0,0 +1,96 @@
+# Copyright 2020 MongoDB Inc.
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+"""Configure the build to track build performance."""
+
+import atexit
+import json
+import os
+import sys
+import time
+
+from jsonschema import validate
+import psutil
+
+_SEC_TO_NANOSEC_FACTOR = 1000000000.0
+
+_BUILD_METRIC_DATA = {}
+
+
+def get_build_metric_dict():
+ global _BUILD_METRIC_DATA
+ return _BUILD_METRIC_DATA
+
+
+# This section is an excerpt of the original
+# https://stackoverflow.com/a/63029332/1644736
+class CaptureAtexits:
+ def __init__(self):
+ self.captured = []
+
+ def __eq__(self, other):
+ self.captured.append(other)
+ return False
+
+
+def finalize_build_metrics(env):
+ metrics = get_build_metric_dict()
+ metrics['end_time'] = time.time_ns()
+
+ with open(os.path.join(os.path.dirname(__file__), "build_metrics_format.schema")) as f:
+ validate(metrics, json.load(f))
+
+ build_metrics_file = env.GetOption('build-metrics')
+ if build_metrics_file == '-':
+ json.dump(metrics, sys.stdout, indent=4, sort_keys=True)
+ else:
+ with open(build_metrics_file, 'w') as f:
+ json.dump(metrics, f, indent=4, sort_keys=True)
+
+
+def add_meta_data(env, key, value):
+ get_build_metric_dict()[key] = value
+
+
+def generate(env, **kwargs):
+
+ # This will force our at exit to the of the stack ensuring
+ # that it is the last thing called when exiting.
+ c = CaptureAtexits()
+ atexit.unregister(c)
+ for func in c.captured:
+ atexit.unregister(func)
+ atexit.register(finalize_build_metrics, env)
+ for func in c.captured:
+ atexit.register(func)
+
+ env.AddMethod(get_build_metric_dict, "GetBuildMetricDictionary")
+ env.AddMethod(add_meta_data, "AddBuildMetricsMetaData")
+
+ metrics = get_build_metric_dict()
+ p = psutil.Process(os.getpid())
+
+ metrics['start_time'] = int(p.create_time() * _SEC_TO_NANOSEC_FACTOR)
+ metrics['scons_command'] = " ".join([sys.executable] + sys.argv)
+
+
+def exists(env):
+ return True
diff --git a/site_scons/site_tools/build_metrics/build_metrics_format.schema b/site_scons/site_tools/build_metrics/build_metrics_format.schema
new file mode 100644
index 00000000000..5b05b1491c9
--- /dev/null
+++ b/site_scons/site_tools/build_metrics/build_metrics_format.schema
@@ -0,0 +1,10 @@
+{
+ "type" : "object",
+ "properties" : {
+ "start_time" : {"type" : "integer"},
+ "end_time" : {"type" : "integer"},
+ "evg_id" : {"type" : "string"},
+ "variant" : {"type" : "string"},
+ "scons_command" : {"type" : "string"}
+ }
+} \ No newline at end of file