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
This commit is contained in:
parent
7cff215803
commit
024931b541
@ -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."""
|
||||
|
48
ceilometer/tests/unit/test_declarative.py
Normal file
48
ceilometer/tests/unit/test_declarative.py
Normal file
@ -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)"),
|
||||
])
|
Loading…
Reference in New Issue
Block a user