2 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
19 from .packet_stats import PacketPathStatsManager
20 from .stats_collector import IntervalCollector
23 class StatsManager(object):
24 """A class to collect detailed stats and handle fixed rate runs for all chain types."""
26 def __init__(self, chain_runner):
27 self.chain_runner = chain_runner
28 self.config = chain_runner.config
29 self.traffic_client = chain_runner.traffic_client
30 self.specs = chain_runner.specs
31 self.notifier = chain_runner.notifier
32 self.interval_collector = None
33 self.factory = chain_runner.factory
34 # create a packet path stats manager for fixed rate runs only
35 if self.config.single_run:
37 self.traffic_client.insert_interface_stats(pps_list)
38 self.pps_mgr = PacketPathStatsManager(self.config, pps_list)
43 def create_worker(self):
44 """Create a worker to fetch custom data.
46 This is done late as we need to know the dest MAC for all VNFs, which can happen
47 as late as after ARP discovery.
49 if not self.worker and self.specs.openstack:
50 WORKER_CLASS = self.factory.get_chain_worker(self.specs.openstack.encaps,
51 self.config.service_chain)
52 self.worker = WORKER_CLASS(self)
54 def _generate_traffic(self):
55 if self.config.no_traffic:
58 self.interval_collector = IntervalCollector(time.time())
59 self.interval_collector.attach_notifier(self.notifier)
60 LOG.info('Starting to generate traffic...')
62 for stats in self.traffic_client.run_traffic():
63 self.interval_collector.add(stats)
65 LOG.info('...traffic generating ended.')
69 return self.interval_collector.get() if self.interval_collector else []
71 def get_version(self):
72 return self.worker.get_version() if self.worker else {}
74 def _update_interface_stats(self, diff=False):
75 """Update interface stats for both the traffic generator and the worker."""
76 self.traffic_client.update_interface_stats(diff)
78 self.worker.update_interface_stats(diff)
80 def run_fixed_rate(self):
81 """Run a fixed rate and analyze results."""
82 # Baseline the packet path stats
83 self._update_interface_stats()
85 in_flight_stats = self._generate_traffic()
87 'stats': in_flight_stats
89 # New analysis code with packet path stats
90 # Diff all interface stats and return packet path stats analysis
91 # Diff the packet path stats
92 self._update_interface_stats(diff=True)
93 result['packet_path_stats'] = self.pps_mgr.get_results()
96 def get_compute_nodes_bios(self):
97 return self.worker.get_compute_nodes_bios() if self.worker else {}