Commit 024931b5 authored by Ilya Tyaptin's avatar Ilya Tyaptin

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
parent 7cff2158
......@@ -32,6 +32,7 @@ class DefinitionException(Exception):
class Definition(object):
JSONPATH_RW_PARSER = parser.ExtentedJsonPathParser()
def __init__(self, name, cfg, plugin_manager):
self.cfg = cfg
......@@ -85,7 +86,7 @@ class Definition(object):
self.getter = fields
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):
return values[0] if values else None
def make_getter(self, fields):
if fields in self.GETTERS_CACHE:
return self.GETTERS_CACHE[fields]
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."""
# 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
# 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 = [
{'fields': 'field4.`split(., 1, 1)`'},
{'fields': ['field5.arg', 'field6'], 'type': 'text'}
self.parser = mock.MagicMock()
parser_patch = mockpatch.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(["_field1"),"_field2|_field3"),"field4.`split(., 1, 1)`"),"(field5.arg)|(field6)"),
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment