From 024931b541f2c535dd2ca4f0deb300da47c801b0 Mon Sep 17 00:00:00 2001 From: Ilya Tyaptin Date: Fri, 26 Feb 2016 19:52:10 +0300 Subject: [PATCH] Cache getters for the decalarative definitions In the our declarative configs for the events and meters we use same fields for the many times. Despite this fact all fields are parsed by ExtentedJsonPathParser again and again. It affect time of notification agent starting and create additional cpu load. This patch is a trying to make this process faster and lighter. Change-Id: Ib190b5977688cdf48cc454ff133dd81ff5576310 Closes-bug: #1550436 --- ceilometer/declarative.py | 11 +++++- ceilometer/tests/unit/test_declarative.py | 48 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 ceilometer/tests/unit/test_declarative.py diff --git a/ceilometer/declarative.py b/ceilometer/declarative.py index 00381ee8..772e7937 100644 --- a/ceilometer/declarative.py +++ b/ceilometer/declarative.py @@ -32,6 +32,7 @@ class DefinitionException(Exception): class Definition(object): JSONPATH_RW_PARSER = parser.ExtentedJsonPathParser() + GETTERS_CACHE = {} def __init__(self, name, cfg, plugin_manager): self.cfg = cfg @@ -85,7 +86,7 @@ class Definition(object): self.getter = fields else: try: - self.getter = self.JSONPATH_RW_PARSER.parse(fields).find + self.getter = self.make_getter(fields) except Exception as e: raise DefinitionException( _("Parse error in JSONPath specification " @@ -123,6 +124,14 @@ class Definition(object): else: return values[0] if values else None + def make_getter(self, fields): + if fields in self.GETTERS_CACHE: + return self.GETTERS_CACHE[fields] + else: + getter = self.JSONPATH_RW_PARSER.parse(fields).find + self.GETTERS_CACHE[fields] = getter + return getter + def load_definitions(defaults, config_file, fallback_file=None): """Setup a definitions from yaml config file.""" diff --git a/ceilometer/tests/unit/test_declarative.py b/ceilometer/tests/unit/test_declarative.py new file mode 100644 index 00000000..03b1e396 --- /dev/null +++ b/ceilometer/tests/unit/test_declarative.py @@ -0,0 +1,48 @@ +# +# Copyright 2016 Mirantis, Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import mock +from oslotest import mockpatch + +from ceilometer import declarative +from ceilometer.tests import base + + +class TestDefinition(base.BaseTestCase): + + def setUp(self): + super(TestDefinition, self).setUp() + self.configs = [ + "_field1", + "_field2|_field3", + {'fields': 'field4.`split(., 1, 1)`'}, + {'fields': ['field5.arg', 'field6'], 'type': 'text'} + ] + self.parser = mock.MagicMock() + parser_patch = mockpatch.Patch( + "jsonpath_rw_ext.parser.ExtentedJsonPathParser.parse", + new=self.parser) + self.useFixture(parser_patch) + + def test_caching_parsers(self): + for config in self.configs * 2: + declarative.Definition("test", config, mock.MagicMock()) + self.assertEqual(4, self.parser.call_count) + self.parser.assert_has_calls([ + mock.call("_field1"), + mock.call("_field2|_field3"), + mock.call("field4.`split(., 1, 1)`"), + mock.call("(field5.arg)|(field6)"), + ])