fix two issues
[csit.git] / resources / libraries / python / TrafficGenerator.py
index 119702c..455a21e 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2020 Cisco and/or its affiliates.
+# Copyright (c) 2021 Cisco and/or its affiliates.
 # 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:
@@ -67,7 +67,7 @@ class TGDropRateSearchImpl(DropRateSearch):
 
     def measure_loss(
             self, rate, frame_size, loss_acceptance, loss_acceptance_type,
-            traffic_profile, skip_warmup=False):
+            traffic_profile):
         """Runs the traffic and evaluate the measured results.
 
         :param rate: Offered traffic load.
@@ -76,13 +76,11 @@ class TGDropRateSearchImpl(DropRateSearch):
         :param loss_acceptance_type: Type of permitted loss.
         :param traffic_profile: Module name as a traffic profile identifier.
             See GPL/traffic_profiles/trex for implemented modules.
-        :param skip_warmup: Start TRex without warmup traffic if true.
         :type rate: float
         :type frame_size: str
         :type loss_acceptance: float
         :type loss_acceptance_type: LossAcceptanceType
         :type traffic_profile: str
-        :type skip_warmup: bool
         :returns: Drop threshold exceeded? (True/False)
         :rtype: bool
         :raises NotImplementedError: If TG is not supported.
@@ -96,15 +94,9 @@ class TGDropRateSearchImpl(DropRateSearch):
         subtype = check_subtype(tg_instance.node)
         if subtype == NodeSubTypeTG.TREX:
             unit_rate = str(rate) + self.get_rate_type_str()
-            if skip_warmup:
-                tg_instance.trex_stl_start_remote_exec(
-                    self.get_duration(), unit_rate, frame_size, traffic_profile,
-                    warmup_time=0.0
-                )
-            else:
-                tg_instance.trex_stl_start_remote_exec(
-                    self.get_duration(), unit_rate, frame_size, traffic_profile
-                )
+            tg_instance.trex_stl_start_remote_exec(
+                self.get_duration(), unit_rate, frame_size, traffic_profile
+            )
             loss = tg_instance.get_loss()
             sent = tg_instance.get_sent()
             if self.loss_acceptance_type_is_percentage():
@@ -145,6 +137,8 @@ class TrafficGenerator(AbstractMeasurer):
     ROBOT_LIBRARY_SCOPE = u"TEST SUITE"
 
     def __init__(self):
+        # TODO: Separate into few dataclasses/dicts.
+        #       Pylint dislikes large unstructured state, and it is right.
         self._node = None
         self._mode = None
         # TG interface order mapping
@@ -160,14 +154,28 @@ class TrafficGenerator(AbstractMeasurer):
         self._l7_data = None
         # Measurement input fields, needed for async stop result.
         self._start_time = None
+        self._stop_time = None
         self._rate = None
+        self._target_duration = None
+        self._duration = None
         # Other input parameters, not knowable from measure() signature.
         self.frame_size = None
         self.traffic_profile = None
-        self.warmup_time = None
         self.traffic_directions = None
         self.negative_loss = None
         self.use_latency = None
+        self.ppta = None
+        self.resetter = None
+        self.transaction_scale = None
+        self.transaction_duration = None
+        self.sleep_till_duration = None
+        self.transaction_type = None
+        self.duration_limit = None
+        self.ramp_up_start = None
+        self.ramp_up_stop = None
+        self.ramp_up_rate = None
+        self.ramp_up_duration = None
+        self.state_timeout = None
         # Transient data needed for async measurements.
         self._xstats = (None, None)
         # TODO: Rename "xstats" to something opaque, so T-Rex is not privileged?
@@ -244,7 +252,6 @@ class TrafficGenerator(AbstractMeasurer):
         )
 
     # TODO: pylint says disable=too-many-locals.
-    # A fix is developed in https://gerrit.fd.io/r/c/csit/+/22221
     def initialize_traffic_generator(
             self, tg_node, tg_if1, tg_if2, tg_if1_adj_node, tg_if1_adj_if,
             tg_if2_adj_node, tg_if2_adj_if, osi_layer, tg_if1_dst_mac=None,
@@ -369,7 +376,8 @@ class TrafficGenerator(AbstractMeasurer):
                 # Configure TRex.
                 ports = ''
                 for port in tg_node[u"interfaces"].values():
-                    ports += f" {port.get(u'pci_address')}"
+                    if u'Mellanox' not in port.get(u'model'):
+                        ports += f" {port.get(u'pci_address')}"
 
                 cmd = f"sh -c \"cd {Constants.TREX_INSTALL_DIR}/scripts/ && " \
                     f"./dpdk_nic_bind.py -u {ports} || true\""
@@ -401,12 +409,11 @@ class TrafficGenerator(AbstractMeasurer):
                     raise RuntimeError(u"Start TRex failed!")
 
                 # Test T-Rex API responsiveness.
-                cmd = u"python3"
-                cmd += f" {Constants.REMOTE_FW_DIR}/GPL/tools/trex/"
+                cmd = f"python3 {Constants.REMOTE_FW_DIR}/GPL/tools/trex/"
                 if osi_layer in (u"L2", u"L3"):
-                    cmd += f"trex_stl_assert.py"
+                    cmd += u"trex_stl_assert.py"
                 elif osi_layer == u"L7":
-                    cmd += f"trex_astf_assert.py"
+                    cmd += u"trex_astf_assert.py"
                 else:
                     raise ValueError(u"Unknown OSI layer!")
                 try:
@@ -454,74 +461,6 @@ class TrafficGenerator(AbstractMeasurer):
                 message=u"T-Rex kill failed!"
             )
 
-    def _parse_traffic_results(self, stdout):
-        """Parse stdout of scripts into fields of self.
-
-        Block of code to reuse, by sync start, or stop after async.
-
-        :param stdout: Text containing the standard output.
-        :type stdout: str
-        """
-        subtype = check_subtype(self._node)
-        if subtype == NodeSubTypeTG.TREX:
-            # Last line from console output
-            line = stdout.splitlines()[-1]
-            results = line.split(",")
-            if results[-1] == u" ":
-                results.remove(u" ")
-            self._result = dict()
-            for result in results:
-                key, value = result.split(u"=", maxsplit=1)
-                self._result[key.strip()] = value
-            logger.info(f"TrafficGen results:\n{self._result}")
-            self._received = self._result.get(u"total_received")
-            self._sent = self._result.get(u"total_sent")
-            self._loss = self._result.get(u"frame_loss")
-            self._approximated_duration = \
-                self._result.get(u"approximated_duration")
-            self._approximated_rate = self._result.get(u"approximated_rate")
-            self._latency = list()
-            self._latency.append(self._result.get(u"latency_stream_0(usec)"))
-            self._latency.append(self._result.get(u"latency_stream_1(usec)"))
-            if self._mode == TrexMode.ASTF:
-                self._l7_data = dict()
-                self._l7_data[u"client"] = dict()
-                self._l7_data[u"client"][u"active_flows"] = \
-                    self._result.get(u"client_active_flows")
-                self._l7_data[u"client"][u"established_flows"] = \
-                    self._result.get(u"client_established_flows")
-                self._l7_data[u"server"] = dict()
-                self._l7_data[u"server"][u"active_flows"] = \
-                    self._result.get(u"server_active_flows")
-                self._l7_data[u"server"][u"established_flows"] = \
-                    self._result.get(u"server_established_flows")
-                if u"udp" in self.traffic_profile:
-                    self._l7_data[u"client"][u"udp"] = dict()
-                    self._l7_data[u"client"][u"udp"][u"established_flows"] = \
-                        self._result.get(u"client_udp_connects")
-                    self._l7_data[u"client"][u"udp"][u"closed_flows"] = \
-                        self._result.get(u"client_udp_closed")
-                    self._l7_data[u"server"][u"udp"] = dict()
-                    self._l7_data[u"server"][u"udp"][u"accepted_flows"] = \
-                        self._result.get(u"server_udp_accepts")
-                    self._l7_data[u"server"][u"udp"][u"closed_flows"] = \
-                        self._result.get(u"server_udp_closed")
-                elif u"tcp" in self.traffic_profile:
-                    self._l7_data[u"client"][u"tcp"] = dict()
-                    self._l7_data[u"client"][u"tcp"][u"initiated_flows"] = \
-                        self._result.get(u"client_tcp_connect_inits")
-                    self._l7_data[u"client"][u"tcp"][u"established_flows"] = \
-                        self._result.get(u"client_tcp_connects")
-                    self._l7_data[u"client"][u"tcp"][u"closed_flows"] = \
-                        self._result.get(u"client_tcp_closed")
-                    self._l7_data[u"server"][u"tcp"] = dict()
-                    self._l7_data[u"server"][u"tcp"][u"accepted_flows"] = \
-                        self._result.get(u"server_tcp_accepts")
-                    self._l7_data[u"server"][u"tcp"][u"established_flows"] = \
-                        self._result.get(u"server_tcp_connects")
-                    self._l7_data[u"server"][u"tcp"][u"closed_flows"] = \
-                        self._result.get(u"server_tcp_closed")
-
     def trex_astf_stop_remote_exec(self, node):
         """Execute T-Rex ASTF script on remote node over ssh to stop running
         traffic.
@@ -578,59 +517,103 @@ class TrafficGenerator(AbstractMeasurer):
         :raises ValueError: If TG traffic profile is not supported.
         """
         subtype = check_subtype(self._node)
-        if subtype == NodeSubTypeTG.TREX:
-            if u"trex-astf" in self.traffic_profile:
-                self.trex_astf_stop_remote_exec(self._node)
-            elif u"trex-sl" in self.traffic_profile:
-                self.trex_stl_stop_remote_exec(self._node)
-            else:
-                raise ValueError(u"Unsupported T-Rex traffic profile!")
+        if subtype != NodeSubTypeTG.TREX:
+            raise ValueError(f"Unsupported TG subtype: {subtype!r}")
+        if u"trex-astf" in self.traffic_profile:
+            self.trex_astf_stop_remote_exec(self._node)
+        elif u"trex-stl" in self.traffic_profile:
+            self.trex_stl_stop_remote_exec(self._node)
+        else:
+            raise ValueError(u"Unsupported T-Rex traffic profile!")
+        self._stop_time = time.monotonic()
+
+        return self._get_measurement_result()
 
-        return self.get_measurement_result()
+    def _compute_duration(self, duration, multiplier):
+        """Compute duration for profile driver.
+
+        The final result is influenced by transaction scale and duration limit.
+        It is assumed a higher level function has already set those to self.
+        The duration argument is the target value from search point of view,
+        before the overrides are applied here.
+
+        Minus one (signalling async traffic start) is kept.
+
+        Completeness flag is also included. Duration limited or async trials
+        are not considered complete for ramp-up purposes.
+
+        :param duration: Time expressed in seconds for how long to send traffic.
+        :param multiplier: Traffic rate in transactions per second.
+        :type duration: float
+        :type multiplier: float
+        :returns: New duration and whether it was a complete ramp-up candidate.
+        :rtype: float, bool
+        """
+        if duration < 0.0:
+            # Keep the async -1.
+            return duration, False
+        computed_duration = duration
+        if self.transaction_scale:
+            computed_duration = self.transaction_scale / multiplier
+            # Log the computed duration,
+            # so we can compare with what telemetry suggests
+            # the real duration was.
+            logger.debug(f"Expected duration {computed_duration}")
+            computed_duration += 0.1115
+        if not self.duration_limit:
+            return computed_duration, True
+        limited_duration = min(computed_duration, self.duration_limit)
+        return limited_duration, (limited_duration == computed_duration)
 
     def trex_astf_start_remote_exec(
-            self, duration, mult, frame_size, traffic_profile, async_call=False,
-            latency=True, warmup_time=5.0, traffic_directions=2, tx_port=0,
-            rx_port=1):
+            self, duration, multiplier, async_call=False):
         """Execute T-Rex ASTF script on remote node over ssh to start running
         traffic.
 
         In sync mode, measurement results are stored internally.
         In async mode, initial data including xstats are stored internally.
 
-        :param duration: Time expresed in seconds for how long to send traffic.
-        :param mult: Traffic rate expressed with units (pps, %)
-        :param frame_size: L2 frame size to send (without padding and IPG).
-        :param traffic_profile: Module name as a traffic profile identifier.
-            See GPL/traffic_profiles/trex for implemented modules.
+        This method contains the logic to compute duration as maximum time
+        if transaction_scale is nonzero.
+        The transaction_scale argument defines (limits) how many transactions
+        will be started in total. As that amount of transaction can take
+        considerable time (sometimes due to explicit delays in the profile),
+        the real time a trial needs to finish is computed here. For now,
+        in that case the duration argument is ignored, assuming it comes
+        from ASTF-unaware search algorithm. The overall time a single
+        transaction needs is given in parameter transaction_duration,
+        it includes both explicit delays and implicit time it takes
+        to transfer data (or whatever the transaction does).
+
+        Currently it is observed TRex does not start the ASTF traffic
+        immediately, an ad-hoc constant is added to the computed duration
+        to compensate for that.
+
+        If transaction_scale is zero, duration is not recomputed.
+        It is assumed the subsequent result parsing gets the real duration
+        if the traffic stops sooner for any reason.
+
+        Currently, it is assumed traffic profile defines a single transaction.
+        To avoid heavy logic here, the input rate is expected to be in
+        transactions per second, as that directly translates to TRex multiplier,
+        (assuming the profile does not override the default cps value of one).
+
+        :param duration: Time expressed in seconds for how long to send traffic.
+        :param multiplier: Traffic rate in transactions per second.
         :param async_call: If enabled then don't wait for all incoming traffic.
-        :param latency: With latency measurement.
-        :param warmup_time: Warmup time period.
-        :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
-            Default: 2
-        :param tx_port: Traffic generator transmit port for first flow.
-            Default: 0
-        :param rx_port: Traffic generator receive port for first flow.
-            Default: 1
         :type duration: float
-        :type mult: int
-        :type frame_size: str
-        :type traffic_profile: str
+        :type multiplier: int
         :type async_call: bool
-        :type latency: bool
-        :type warmup_time: float
-        :type traffic_directions: int
-        :type tx_port: int
-        :type rx_port: int
         :raises RuntimeError: In case of T-Rex driver issue.
         """
         self.check_mode(TrexMode.ASTF)
-        p_0, p_1 = (rx_port, tx_port) if self._ifaces_reordered \
-            else (tx_port, rx_port)
+        p_0, p_1 = (1, 0) if self._ifaces_reordered else (0, 1)
         if not isinstance(duration, (float, int)):
             duration = float(duration)
-        if not isinstance(warmup_time, (float, int)):
-            warmup_time = float(warmup_time)
+
+        # TODO: Refactor the code so duration is computed only once,
+        # and both the initial and the computed durations are logged.
+        computed_duration, _ = self._compute_duration(duration, multiplier)
 
         command_line = OptionString().add(u"python3")
         dirname = f"{Constants.REMOTE_FW_DIR}/GPL/tools/trex"
@@ -638,57 +621,62 @@ class TrafficGenerator(AbstractMeasurer):
         command_line.change_prefix(u"--")
         dirname = f"{Constants.REMOTE_FW_DIR}/GPL/traffic_profiles/trex"
         command_line.add_with_value(
-            u"profile", f"'{dirname}/{traffic_profile}.py'"
+            u"profile", f"'{dirname}/{self.traffic_profile}.py'"
         )
-        command_line.add_with_value(u"duration", f"{duration!r}")
-        command_line.add_with_value(u"frame_size", frame_size)
-        command_line.add_with_value(u"mult", int(mult))
-        command_line.add_with_value(u"warmup_time", f"{warmup_time!r}")
+        command_line.add_with_value(u"duration", f"{computed_duration!r}")
+        command_line.add_with_value(u"frame_size", self.frame_size)
+        command_line.add_with_value(u"multiplier", multiplier)
         command_line.add_with_value(u"port_0", p_0)
         command_line.add_with_value(u"port_1", p_1)
-        command_line.add_with_value(u"traffic_directions", traffic_directions)
+        command_line.add_with_value(
+            u"traffic_directions", self.traffic_directions
+        )
         command_line.add_if(u"async_start", async_call)
-        command_line.add_if(u"latency", latency)
+        command_line.add_if(u"latency", self.use_latency)
         command_line.add_if(u"force", Constants.TREX_SEND_FORCE)
 
+        self._start_time = time.monotonic()
+        self._rate = multiplier
         stdout, _ = exec_cmd_no_error(
-            self._node, command_line,
-            timeout=int(duration) + 600 if u"tcp" in self.traffic_profile
-            else 60,
+            self._node, command_line, timeout=computed_duration + 10.0,
             message=u"T-Rex ASTF runtime error!"
         )
 
-        self.traffic_directions = traffic_directions
         if async_call:
             # no result
-            self._start_time = time.time()
-            self._rate = float(mult)
+            self._target_duration = None
+            self._duration = None
             self._received = None
             self._sent = None
             self._loss = None
             self._latency = None
             xstats = [None, None]
+            self._l7_data = dict()
             self._l7_data[u"client"] = dict()
             self._l7_data[u"client"][u"active_flows"] = None
             self._l7_data[u"client"][u"established_flows"] = None
+            self._l7_data[u"client"][u"traffic_duration"] = None
             self._l7_data[u"server"] = dict()
             self._l7_data[u"server"][u"active_flows"] = None
             self._l7_data[u"server"][u"established_flows"] = None
+            self._l7_data[u"server"][u"traffic_duration"] = None
             if u"udp" in self.traffic_profile:
                 self._l7_data[u"client"][u"udp"] = dict()
-                self._l7_data[u"client"][u"udp"][u"established_flows"] = None
+                self._l7_data[u"client"][u"udp"][u"connects"] = None
                 self._l7_data[u"client"][u"udp"][u"closed_flows"] = None
+                self._l7_data[u"client"][u"udp"][u"err_cwf"] = None
                 self._l7_data[u"server"][u"udp"] = dict()
                 self._l7_data[u"server"][u"udp"][u"accepted_flows"] = None
                 self._l7_data[u"server"][u"udp"][u"closed_flows"] = None
             elif u"tcp" in self.traffic_profile:
                 self._l7_data[u"client"][u"tcp"] = dict()
                 self._l7_data[u"client"][u"tcp"][u"initiated_flows"] = None
-                self._l7_data[u"client"][u"tcp"][u"established_flows"] = None
+                self._l7_data[u"client"][u"tcp"][u"connects"] = None
                 self._l7_data[u"client"][u"tcp"][u"closed_flows"] = None
+                self._l7_data[u"client"][u"tcp"][u"connattempt"] = None
                 self._l7_data[u"server"][u"tcp"] = dict()
                 self._l7_data[u"server"][u"tcp"][u"accepted_flows"] = None
-                self._l7_data[u"server"][u"tcp"][u"established_flows"] = None
+                self._l7_data[u"server"][u"tcp"][u"connects"] = None
                 self._l7_data[u"server"][u"tcp"][u"closed_flows"] = None
             else:
                 logger.warn(u"Unsupported T-Rex ASTF traffic profile!")
@@ -701,53 +689,38 @@ class TrafficGenerator(AbstractMeasurer):
                     break
             self._xstats = tuple(xstats)
         else:
+            self._target_duration = duration
+            self._duration = computed_duration
             self._parse_traffic_results(stdout)
-            self._start_time = None
-            self._rate = None
 
-    def trex_stl_start_remote_exec(
-            self, duration, rate, frame_size, traffic_profile, async_call=False,
-            latency=False, warmup_time=5.0, traffic_directions=2, tx_port=0,
-            rx_port=1):
+    def trex_stl_start_remote_exec(self, duration, rate, async_call=False):
         """Execute T-Rex STL script on remote node over ssh to start running
         traffic.
 
         In sync mode, measurement results are stored internally.
         In async mode, initial data including xstats are stored internally.
 
+        Mode-unaware code (e.g. in search algorithms) works with transactions.
+        To keep the logic simple, multiplier is set to that value.
+        As bidirectional traffic profiles send packets in both directions,
+        they are treated as transactions with two packets (one per direction).
+
         :param duration: Time expressed in seconds for how long to send traffic.
-        :param rate: Traffic rate expressed with units (pps, %)
-        :param frame_size: L2 frame size to send (without padding and IPG).
-        :param traffic_profile: Module name as a traffic profile identifier.
-            See GPL/traffic_profiles/trex for implemented modules.
+        :param rate: Traffic rate in transactions per second.
         :param async_call: If enabled then don't wait for all incoming traffic.
-        :param latency: With latency measurement.
-        :param warmup_time: Warmup time period.
-        :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
-            Default: 2
-        :param tx_port: Traffic generator transmit port for first flow.
-            Default: 0
-        :param rx_port: Traffic generator receive port for first flow.
-            Default: 1
         :type duration: float
         :type rate: str
-        :type frame_size: str
-        :type traffic_profile: str
         :type async_call: bool
-        :type latency: bool
-        :type warmup_time: float
-        :type traffic_directions: int
-        :type tx_port: int
-        :type rx_port: int
         :raises RuntimeError: In case of T-Rex driver issue.
         """
         self.check_mode(TrexMode.STL)
-        p_0, p_1 = (rx_port, tx_port) if self._ifaces_reordered \
-            else (tx_port, rx_port)
+        p_0, p_1 = (1, 0) if self._ifaces_reordered else (0, 1)
         if not isinstance(duration, (float, int)):
             duration = float(duration)
-        if not isinstance(warmup_time, (float, int)):
-            warmup_time = float(warmup_time)
+
+        # TODO: Refactor the code so duration is computed only once,
+        # and both the initial and the computed durations are logged.
+        duration, _ = self._compute_duration(duration=duration, multiplier=rate)
 
         command_line = OptionString().add(u"python3")
         dirname = f"{Constants.REMOTE_FW_DIR}/GPL/tools/trex"
@@ -755,29 +728,32 @@ class TrafficGenerator(AbstractMeasurer):
         command_line.change_prefix(u"--")
         dirname = f"{Constants.REMOTE_FW_DIR}/GPL/traffic_profiles/trex"
         command_line.add_with_value(
-            u"profile", f"'{dirname}/{traffic_profile}.py'"
+            u"profile", f"'{dirname}/{self.traffic_profile}.py'"
         )
         command_line.add_with_value(u"duration", f"{duration!r}")
-        command_line.add_with_value(u"frame_size", frame_size)
+        command_line.add_with_value(u"frame_size", self.frame_size)
         command_line.add_with_value(u"rate", f"{rate!r}")
-        command_line.add_with_value(u"warmup_time", f"{warmup_time!r}")
         command_line.add_with_value(u"port_0", p_0)
         command_line.add_with_value(u"port_1", p_1)
-        command_line.add_with_value(u"traffic_directions", traffic_directions)
+        command_line.add_with_value(
+            u"traffic_directions", self.traffic_directions
+        )
         command_line.add_if(u"async_start", async_call)
-        command_line.add_if(u"latency", latency)
+        command_line.add_if(u"latency", self.use_latency)
         command_line.add_if(u"force", Constants.TREX_SEND_FORCE)
 
+        # TODO: This is ugly. Handle parsing better.
+        self._start_time = time.monotonic()
+        self._rate = float(rate[:-3]) if u"pps" in rate else float(rate)
         stdout, _ = exec_cmd_no_error(
             self._node, command_line, timeout=int(duration) + 60,
             message=u"T-Rex STL runtime error"
         )
 
-        self.traffic_directions = traffic_directions
         if async_call:
             # no result
-            self._start_time = time.time()
-            self._rate = float(rate[:-3]) if u"pps" in rate else float(rate)
+            self._target_duration = None
+            self._duration = None
             self._received = None
             self._sent = None
             self._loss = None
@@ -793,14 +769,29 @@ class TrafficGenerator(AbstractMeasurer):
                     break
             self._xstats = tuple(xstats)
         else:
+            self._target_duration = duration
+            self._duration = duration
             self._parse_traffic_results(stdout)
-            self._start_time = None
-            self._rate = None
 
     def send_traffic_on_tg(
-            self, duration, rate, frame_size, traffic_profile, warmup_time=5,
-            async_call=False, latency=False, traffic_directions=2, tx_port=0,
-            rx_port=1):
+            self,
+            duration,
+            rate,
+            frame_size,
+            traffic_profile,
+            async_call=False,
+            ppta=1,
+            traffic_directions=2,
+            transaction_duration=0.0,
+            transaction_scale=0,
+            transaction_type=u"packet",
+            duration_limit=0.0,
+            use_latency=False,
+            ramp_up_rate=None,
+            ramp_up_duration=None,
+            state_timeout=300.0,
+            ramp_up_only=False,
+        ):
         """Send traffic from all configured interfaces on TG.
 
         In async mode, xstats is stored internally,
@@ -808,68 +799,225 @@ class TrafficGenerator(AbstractMeasurer):
         In both modes, stdout is returned,
         but _parse_traffic_results only works in sync output.
 
-        Note that bidirectional traffic also contains flows
-        transmitted from rx_port and received in tx_port.
-        But some tests use asymmetric traffic, so those arguments are relevant.
-
-        Also note that traffic generator uses DPDK driver which might
+        Note that traffic generator uses DPDK driver which might
         reorder port numbers based on wiring and PCI numbering.
         This method handles that, so argument values are invariant,
         but you can see swapped valued in debug logs.
 
+        When transaction_scale is specified, the duration value is ignored
+        and the needed time is computed. For cases where this results in
+        to too long measurement (e.g. teardown trial with small rate),
+        duration_limit is applied (of non-zero), so the trial is stopped sooner.
+
+        Bidirectional STL profiles are treated as transactions with two packets.
+
+        The return value is None for async.
+
         :param duration: Duration of test traffic generation in seconds.
-        :param rate: Traffic rate.
-            - T-Rex stateless mode => Offered load per interface in pps,
-            - T-Rex advanced stateful mode => multiplier of profile CPS.
+        :param rate: Traffic rate in transactions per second.
         :param frame_size: Frame size (L2) in Bytes.
         :param traffic_profile: Module name as a traffic profile identifier.
             See GPL/traffic_profiles/trex for implemented modules.
-        :param warmup_time: Warmup phase in seconds.
         :param async_call: Async mode.
-        :param latency: With latency measurement.
+        :param ppta: Packets per transaction, aggregated over directions.
+            Needed for udp_pps which does not have a good transaction counter,
+            so we need to compute expected number of packets.
+            Default: 1.
         :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
             Default: 2
-        :param tx_port: Traffic generator transmit port for first flow.
-            Default: 0
-        :param rx_port: Traffic generator receive port for first flow.
-            Default: 1
+        :param transaction_duration: Total expected time to close transaction.
+        :param transaction_scale: Number of transactions to perform.
+            0 (default) means unlimited.
+        :param transaction_type: An identifier specifying which counters
+            and formulas to use when computing attempted and failed
+            transactions. Default: "packet".
+        :param duration_limit: Zero or maximum limit for computed (or given)
+            duration.
+        :param use_latency: Whether to measure latency during the trial.
+            Default: False.
+        :param ramp_up_rate: Rate to use in ramp-up trials [pps].
+        :param ramp_up_duration: Duration of ramp-up trials [s].
+        :param state_timeout: Time of life of DUT state [s].
+        :param ramp_up_only: If true, do not perform main trial measurement.
         :type duration: float
         :type rate: float
         :type frame_size: str
         :type traffic_profile: str
-        :type warmup_time: float
         :type async_call: bool
-        :type latency: bool
+        :type ppta: int
         :type traffic_directions: int
-        :type tx_port: int
-        :type rx_port: int
+        :type transaction_duration: float
+        :type transaction_scale: int
+        :type transaction_type: str
+        :type duration_limit: float
+        :type use_latency: bool
+        :type ramp_up_rate: float
+        :type ramp_up_duration: float
+        :type state_timeout: float
+        :type ramp_up_only: bool
         :returns: TG results.
-        :rtype: str
+        :rtype: ReceiveRateMeasurement or None
+        :raises ValueError: If TG traffic profile is not supported.
+        """
+        self.set_rate_provider_defaults(
+            frame_size=frame_size,
+            traffic_profile=traffic_profile,
+            ppta=ppta,
+            traffic_directions=traffic_directions,
+            transaction_duration=transaction_duration,
+            transaction_scale=transaction_scale,
+            transaction_type=transaction_type,
+            duration_limit=duration_limit,
+            use_latency=use_latency,
+            ramp_up_rate=ramp_up_rate,
+            ramp_up_duration=ramp_up_duration,
+            state_timeout=state_timeout,
+        )
+        return self._send_traffic_on_tg_with_ramp_up(
+            duration=duration,
+            rate=rate,
+            async_call=async_call,
+            ramp_up_only=ramp_up_only,
+        )
+
+    def _send_traffic_on_tg_internal(
+            self, duration, rate, async_call=False):
+        """Send traffic from all configured interfaces on TG.
+
+        This is an internal function, it assumes set_rate_provider_defaults
+        has been called to remember most values.
+        The reason why need to remember various values is that
+        the traffic can be asynchronous, and parsing needs those values.
+        The reason why this is is a separate function from the one
+        which calls set_rate_provider_defaults is that some search algorithms
+        need to specify their own values, and we do not want the measure call
+        to overwrite them with defaults.
+
+        This function is used both for automated ramp-up trials
+        and for explicitly called trials.
+
+        :param duration: Duration of test traffic generation in seconds.
+        :param rate: Traffic rate in transactions per second.
+        :param async_call: Async mode.
+        :type duration: float
+        :type rate: float
+        :type async_call: bool
+        :returns: TG results.
+        :rtype: ReceiveRateMeasurement or None
         :raises ValueError: If TG traffic profile is not supported.
         """
         subtype = check_subtype(self._node)
         if subtype == NodeSubTypeTG.TREX:
-            self.set_rate_provider_defaults(
-                frame_size, traffic_profile,
-                traffic_directions=traffic_directions)
             if u"trex-astf" in self.traffic_profile:
                 self.trex_astf_start_remote_exec(
-                    duration, int(rate), frame_size, traffic_profile,
-                    async_call, latency, warmup_time, traffic_directions,
-                    tx_port, rx_port
+                    duration, float(rate), async_call
                 )
-            # TODO: rename all t-rex stateless profiles to use 'trex-stl'
-            elif u"trex-sl" in self.traffic_profile:
+            elif u"trex-stl" in self.traffic_profile:
                 unit_rate_str = str(rate) + u"pps"
+                # TODO: Suport transaction_scale et al?
                 self.trex_stl_start_remote_exec(
-                    duration, unit_rate_str, frame_size, traffic_profile,
-                    async_call, latency, warmup_time, traffic_directions,
-                    tx_port, rx_port
+                    duration, unit_rate_str, async_call
                 )
             else:
                 raise ValueError(u"Unsupported T-Rex traffic profile!")
 
-        return self._result
+        return None if async_call else self._get_measurement_result()
+
+    def _send_traffic_on_tg_with_ramp_up(
+            self, duration, rate, async_call=False, ramp_up_only=False):
+        """Send traffic from all interfaces on TG, maybe after ramp-up.
+
+        This is an internal function, it assumes set_rate_provider_defaults
+        has been called to remember most values.
+        The reason why need to remember various values is that
+        the traffic can be asynchronous, and parsing needs those values.
+        The reason why this is a separate function from the one
+        which calls set_rate_provider_defaults is that some search algorithms
+        need to specify their own values, and we do not want the measure call
+        to overwrite them with defaults.
+
+        If ramp-up tracking is detected, a computation is performed,
+        and if state timeout is near, trial at ramp-up rate and duration
+        is inserted before the main trial measurement.
+
+        The ramp_up_only parameter forces a ramp-up without immediate
+        trial measurement, which is useful in case self remembers
+        a previous ramp-up trial that belongs to a different test (phase).
+
+        Return None if trial is async or ramp-up only.
+
+        :param duration: Duration of test traffic generation in seconds.
+        :param rate: Traffic rate in transactions per second.
+        :param async_call: Async mode.
+        :param ramp_up_only: If true, do not perform main trial measurement.
+        :type duration: float
+        :type rate: float
+        :type async_call: bool
+        :type ramp_up_only: bool
+        :returns: TG results.
+        :rtype: ReceiveRateMeasurement or None
+        :raises ValueError: If TG traffic profile is not supported.
+        """
+        complete = False
+        if self.ramp_up_rate:
+            # Figure out whether we need to insert a ramp-up trial.
+            # TODO: Give up on async_call=True?
+            if ramp_up_only or self.ramp_up_start is None:
+                # We never ramped up yet (at least not in this test case).
+                ramp_up_needed = True
+            else:
+                # We ramped up before, but maybe it was too long ago.
+                # Adding a constant overhead to be safe.
+                time_now = time.monotonic() + 1.0
+                computed_duration, complete = self._compute_duration(
+                    duration=duration,
+                    multiplier=rate,
+                )
+                # There are two conditions for inserting ramp-up.
+                # If early sessions are expiring already,
+                # or if late sessions are to expire before measurement is over.
+                ramp_up_start_delay = time_now - self.ramp_up_start
+                ramp_up_stop_delay = time_now - self.ramp_up_stop
+                ramp_up_stop_delay += computed_duration
+                bigger_delay = max(ramp_up_start_delay, ramp_up_stop_delay)
+                # Final boolean decision.
+                ramp_up_needed = (bigger_delay >= self.state_timeout)
+            if ramp_up_needed:
+                logger.debug(
+                    u"State may time out during next real trial, "
+                    u"inserting a ramp-up trial."
+                )
+                self.ramp_up_start = time.monotonic()
+                self._send_traffic_on_tg_internal(
+                    duration=self.ramp_up_duration,
+                    rate=self.ramp_up_rate,
+                    async_call=async_call,
+                )
+                self.ramp_up_stop = time.monotonic()
+                logger.debug(u"Ramp-up done.")
+            else:
+                logger.debug(
+                    u"State will probably not time out during next real trial, "
+                    u"no ramp-up trial needed just yet."
+                )
+        if ramp_up_only:
+            return None
+        trial_start = time.monotonic()
+        result = self._send_traffic_on_tg_internal(
+            duration=duration,
+            rate=rate,
+            async_call=async_call,
+        )
+        trial_end = time.monotonic()
+        if self.ramp_up_rate:
+            # Optimization: No loss acts as a good ramp-up, if it was complete.
+            if complete and result is not None and result.loss_count == 0:
+                logger.debug(u"Good trial acts as a ramp-up")
+                self.ramp_up_start = trial_start
+                self.ramp_up_stop = trial_end
+            else:
+                logger.debug(u"Loss or incomplete, does not act as a ramp-up.")
+        return result
 
     def no_traffic_loss_occurred(self):
         """Fail if loss occurred in traffic run.
@@ -885,12 +1033,14 @@ class TrafficGenerator(AbstractMeasurer):
     def fail_if_no_traffic_forwarded(self):
         """Fail if no traffic forwarded.
 
+        TODO: Check number of passed transactions instead.
+
         :returns: nothing
         :raises Exception: If no traffic forwarded.
         """
         if self._received is None:
             raise RuntimeError(u"The traffic generation has not been issued")
-        if self._received == u"0":
+        if self._received == 0:
             raise RuntimeError(u"No traffic forwarded")
 
     def partial_traffic_loss_accepted(
@@ -919,74 +1069,236 @@ class TrafficGenerator(AbstractMeasurer):
                 f"Traffic loss {loss} above loss acceptance: {loss_acceptance}"
             )
 
-    def set_rate_provider_defaults(
-            self, frame_size, traffic_profile, warmup_time=0.0,
-            traffic_directions=2, negative_loss=True, latency=False):
-        """Store values accessed by measure().
+    def _parse_traffic_results(self, stdout):
+        """Parse stdout of scripts into fields of self.
 
-        :param frame_size: Frame size identifier or value [B].
-        :param traffic_profile: Module name as a traffic profile identifier.
-            See GPL/traffic_profiles/trex for implemented modules.
-        :param warmup_time: Traffic duration before measurement starts [s].
-        :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
-            Default: 2
-        :param negative_loss: If false, negative loss is reported as zero loss.
-        :param latency: Whether to measure latency during the trial.
-            Default: False.
-        :type frame_size: str or int
-        :type traffic_profile: str
-        :type warmup_time: float
-        :type traffic_directions: int
-        :type negative_loss: bool
-        :type latency: bool
+        Block of code to reuse, by sync start, or stop after async.
+
+        :param stdout: Text containing the standard output.
+        :type stdout: str
         """
-        self.frame_size = frame_size
-        self.traffic_profile = str(traffic_profile)
-        self.warmup_time = float(warmup_time)
-        self.traffic_directions = traffic_directions
-        self.negative_loss = negative_loss
-        self.use_latency = latency
+        subtype = check_subtype(self._node)
+        if subtype == NodeSubTypeTG.TREX:
+            # Last line from console output
+            line = stdout.splitlines()[-1]
+            results = line.split(u";")
+            if results[-1] in (u" ", u""):
+                results.pop(-1)
+            self._result = dict()
+            for result in results:
+                key, value = result.split(u"=", maxsplit=1)
+                self._result[key.strip()] = value
+            logger.info(f"TrafficGen results:\n{self._result}")
+            self._received = int(self._result.get(u"total_received"), 0)
+            self._sent = int(self._result.get(u"total_sent", 0))
+            self._loss = int(self._result.get(u"frame_loss", 0))
+            self._approximated_duration = \
+                self._result.get(u"approximated_duration", 0.0)
+            if u"manual" not in str(self._approximated_duration):
+                self._approximated_duration = float(self._approximated_duration)
+            self._latency = list()
+            self._latency.append(self._result.get(u"latency_stream_0(usec)"))
+            self._latency.append(self._result.get(u"latency_stream_1(usec)"))
+            if self._mode == TrexMode.ASTF:
+                self._l7_data = dict()
+                self._l7_data[u"client"] = dict()
+                self._l7_data[u"client"][u"sent"] = \
+                    int(self._result.get(u"client_sent", 0))
+                self._l7_data[u"client"][u"received"] = \
+                    int(self._result.get(u"client_received", 0))
+                self._l7_data[u"client"][u"active_flows"] = \
+                    int(self._result.get(u"client_active_flows", 0))
+                self._l7_data[u"client"][u"established_flows"] = \
+                    int(self._result.get(u"client_established_flows", 0))
+                self._l7_data[u"client"][u"traffic_duration"] = \
+                    float(self._result.get(u"client_traffic_duration", 0.0))
+                self._l7_data[u"client"][u"err_rx_throttled"] = \
+                    int(self._result.get(u"client_err_rx_throttled", 0))
+                self._l7_data[u"client"][u"err_c_nf_throttled"] = \
+                    int(self._result.get(u"client_err_nf_throttled", 0))
+                self._l7_data[u"client"][u"err_flow_overflow"] = \
+                    int(self._result.get(u"client_err_flow_overflow", 0))
+                self._l7_data[u"server"] = dict()
+                self._l7_data[u"server"][u"active_flows"] = \
+                    int(self._result.get(u"server_active_flows", 0))
+                self._l7_data[u"server"][u"established_flows"] = \
+                    int(self._result.get(u"server_established_flows", 0))
+                self._l7_data[u"server"][u"traffic_duration"] = \
+                    float(self._result.get(u"server_traffic_duration", 0.0))
+                self._l7_data[u"server"][u"err_rx_throttled"] = \
+                    int(self._result.get(u"client_err_rx_throttled", 0))
+                if u"udp" in self.traffic_profile:
+                    self._l7_data[u"client"][u"udp"] = dict()
+                    self._l7_data[u"client"][u"udp"][u"connects"] = \
+                        int(self._result.get(u"client_udp_connects", 0))
+                    self._l7_data[u"client"][u"udp"][u"closed_flows"] = \
+                        int(self._result.get(u"client_udp_closed", 0))
+                    self._l7_data[u"client"][u"udp"][u"tx_bytes"] = \
+                        int(self._result.get(u"client_udp_tx_bytes", 0))
+                    self._l7_data[u"client"][u"udp"][u"rx_bytes"] = \
+                        int(self._result.get(u"client_udp_rx_bytes", 0))
+                    self._l7_data[u"client"][u"udp"][u"tx_packets"] = \
+                        int(self._result.get(u"client_udp_tx_packets", 0))
+                    self._l7_data[u"client"][u"udp"][u"rx_packets"] = \
+                        int(self._result.get(u"client_udp_rx_packets", 0))
+                    self._l7_data[u"client"][u"udp"][u"keep_drops"] = \
+                        int(self._result.get(u"client_udp_keep_drops", 0))
+                    self._l7_data[u"client"][u"udp"][u"err_cwf"] = \
+                        int(self._result.get(u"client_err_cwf", 0))
+                    self._l7_data[u"server"][u"udp"] = dict()
+                    self._l7_data[u"server"][u"udp"][u"accepted_flows"] = \
+                        int(self._result.get(u"server_udp_accepts", 0))
+                    self._l7_data[u"server"][u"udp"][u"closed_flows"] = \
+                        int(self._result.get(u"server_udp_closed", 0))
+                    self._l7_data[u"server"][u"udp"][u"tx_bytes"] = \
+                        int(self._result.get(u"server_udp_tx_bytes", 0))
+                    self._l7_data[u"server"][u"udp"][u"rx_bytes"] = \
+                        int(self._result.get(u"server_udp_rx_bytes", 0))
+                    self._l7_data[u"server"][u"udp"][u"tx_packets"] = \
+                        int(self._result.get(u"server_udp_tx_packets", 0))
+                    self._l7_data[u"server"][u"udp"][u"rx_packets"] = \
+                        int(self._result.get(u"server_udp_rx_packets", 0))
+                elif u"tcp" in self.traffic_profile:
+                    self._l7_data[u"client"][u"tcp"] = dict()
+                    self._l7_data[u"client"][u"tcp"][u"initiated_flows"] = \
+                        int(self._result.get(u"client_tcp_connect_inits", 0))
+                    self._l7_data[u"client"][u"tcp"][u"connects"] = \
+                        int(self._result.get(u"client_tcp_connects", 0))
+                    self._l7_data[u"client"][u"tcp"][u"closed_flows"] = \
+                        int(self._result.get(u"client_tcp_closed", 0))
+                    self._l7_data[u"client"][u"tcp"][u"connattempt"] = \
+                        int(self._result.get(u"client_tcp_connattempt", 0))
+                    self._l7_data[u"client"][u"tcp"][u"tx_bytes"] = \
+                        int(self._result.get(u"client_tcp_tx_bytes", 0))
+                    self._l7_data[u"client"][u"tcp"][u"rx_bytes"] = \
+                        int(self._result.get(u"client_tcp_rx_bytes", 0))
+                    self._l7_data[u"server"][u"tcp"] = dict()
+                    self._l7_data[u"server"][u"tcp"][u"accepted_flows"] = \
+                        int(self._result.get(u"server_tcp_accepts", 0))
+                    self._l7_data[u"server"][u"tcp"][u"connects"] = \
+                        int(self._result.get(u"server_tcp_connects", 0))
+                    self._l7_data[u"server"][u"tcp"][u"closed_flows"] = \
+                        int(self._result.get(u"server_tcp_closed", 0))
+                    self._l7_data[u"server"][u"tcp"][u"tx_bytes"] = \
+                        int(self._result.get(u"server_tcp_tx_bytes", 0))
+                    self._l7_data[u"server"][u"tcp"][u"rx_bytes"] = \
+                        int(self._result.get(u"server_tcp_rx_bytes", 0))
 
-    def get_measurement_result(self, duration=None, transmit_rate=None):
+    def _get_measurement_result(self):
         """Return the result of last measurement as ReceiveRateMeasurement.
 
         Separate function, as measurements can end either by time
         or by explicit call, this is the common block at the end.
 
+        The target_tr field of ReceiveRateMeasurement is in
+        transactions per second. Transmit count and loss count units
+        depend on the transaction type. Usually they are in transactions
+        per second, or aggregate packets per second.
+
         TODO: Fail on running or already reported measurement.
 
-        :param duration: Measurement duration [s] if known beforehand.
-            For explicitly stopped measurement it is estimated.
-        :param transmit_rate: Target aggregate transmit rate [pps].
-            If not given, computed assuming it was bidirectional.
-        :type duration: float or NoneType
-        :type transmit_rate: float or NoneType
         :returns: Structure containing the result of the measurement.
         :rtype: ReceiveRateMeasurement
         """
-        if duration is None:
-            duration = time.time() - self._start_time
-            self._start_time = None
-        if transmit_rate is None:
-            transmit_rate = self._rate * self.traffic_directions
-        transmit_count = int(self.get_sent())
-        loss_count = int(self.get_loss())
-        if loss_count < 0 and not self.negative_loss:
-            loss_count = 0
+        try:
+            # Client duration seems to include a setup period
+            # where TRex does not send any packets yet.
+            # Server duration does not include it.
+            server_data = self._l7_data[u"server"]
+            approximated_duration = float(server_data[u"traffic_duration"])
+        except (KeyError, AttributeError, ValueError, TypeError):
+            approximated_duration = None
+        try:
+            if not approximated_duration:
+                approximated_duration = float(self._approximated_duration)
+        except ValueError:  # "manual"
+            approximated_duration = None
+        if not approximated_duration:
+            if self._duration and self._duration > 0:
+                # Known recomputed or target duration.
+                approximated_duration = self._duration
+            else:
+                # It was an explicit stop.
+                if not self._stop_time:
+                    raise RuntimeError(u"Unable to determine duration.")
+                approximated_duration = self._stop_time - self._start_time
+        target_duration = self._target_duration
+        if not target_duration:
+            target_duration = approximated_duration
+        transmit_rate = self._rate
+        if self.transaction_type == u"packet":
+            partial_attempt_count = self._sent
+            expected_attempt_count = self._sent
+            fail_count = self._loss
+        elif self.transaction_type == u"udp_cps":
+            if not self.transaction_scale:
+                raise RuntimeError(u"Add support for no-limit udp_cps.")
+            partial_attempt_count = self._l7_data[u"client"][u"sent"]
+            # We do not care whether TG is slow, it should have attempted all.
+            expected_attempt_count = self.transaction_scale
+            pass_count = self._l7_data[u"client"][u"received"]
+            fail_count = expected_attempt_count - pass_count
+        elif self.transaction_type == u"tcp_cps":
+            if not self.transaction_scale:
+                raise RuntimeError(u"Add support for no-limit tcp_cps.")
+            ctca = self._l7_data[u"client"][u"tcp"][u"connattempt"]
+            partial_attempt_count = ctca
+            # We do not care whether TG is slow, it should have attempted all.
+            expected_attempt_count = self.transaction_scale
+            # From TCP point of view, server/connects counts full connections,
+            # but we are testing NAT session so client/connects counts that
+            # (half connections from TCP point of view).
+            pass_count = self._l7_data[u"client"][u"tcp"][u"connects"]
+            fail_count = expected_attempt_count - pass_count
+        elif self.transaction_type == u"udp_pps":
+            if not self.transaction_scale:
+                raise RuntimeError(u"Add support for no-limit udp_pps.")
+            partial_attempt_count = self._sent
+            expected_attempt_count = self.transaction_scale * self.ppta
+            fail_count = self._loss + (expected_attempt_count - self._sent)
+        elif self.transaction_type == u"tcp_pps":
+            if not self.transaction_scale:
+                raise RuntimeError(u"Add support for no-limit tcp_pps.")
+            partial_attempt_count = self._sent
+            expected_attempt_count = self.transaction_scale * self.ppta
+            # One loss-like scenario happens when TRex receives all packets
+            # on L2 level, but is not fast enough to process them all
+            # at L7 level, which leads to retransmissions.
+            # Those manifest as opackets larger than expected.
+            # A simple workaround is to add absolute difference.
+            # Probability of retransmissions exactly cancelling
+            # packets unsent due to duration stretching is quite low.
+            fail_count = self._loss + abs(expected_attempt_count - self._sent)
+        else:
+            raise RuntimeError(f"Unknown parsing {self.transaction_type!r}")
+        if fail_count < 0 and not self.negative_loss:
+            fail_count = 0
         measurement = ReceiveRateMeasurement(
-            duration, transmit_rate, transmit_count, loss_count
+            duration=target_duration,
+            target_tr=transmit_rate,
+            transmit_count=expected_attempt_count,
+            loss_count=fail_count,
+            approximated_duration=approximated_duration,
+            partial_transmit_count=partial_attempt_count,
         )
         measurement.latency = self.get_latency_int()
         return measurement
 
     def measure(self, duration, transmit_rate):
-        """Run trial measurement, parse and return aggregate results.
+        """Run trial measurement, parse and return results.
+
+        The input rate is for transactions. Stateles bidirectional traffic
+        is understood as sequence of (asynchronous) transactions,
+        two packets each.
 
-        Aggregate means sum over traffic directions.
+        The result units depend on test type, generally
+        the count either transactions or packets (aggregated over directions).
+
+        Optionally, this method sleeps if measurement finished before
+        the time specified as duration.
 
         :param duration: Trial duration [s].
-        :param transmit_rate: Target aggregate transmit rate [pps] / Connections
-        per second (CPS) for UDP/TCP flows.
+        :param transmit_rate: Target rate in transactions per second.
         :type duration: float
         :type transmit_rate: float
         :returns: Structure containing the result of the measurement.
@@ -996,14 +1308,104 @@ class TrafficGenerator(AbstractMeasurer):
         :raises NotImplementedError: If TG is not supported.
         """
         duration = float(duration)
-        # TG needs target Tr per stream, but reports aggregate Tx and Dx.
-        unit_rate_int = transmit_rate / float(self.traffic_directions)
-        self.send_traffic_on_tg(
-            duration, unit_rate_int, self.frame_size, self.traffic_profile,
-            warmup_time=self.warmup_time, latency=self.use_latency,
-            traffic_directions=self.traffic_directions
+        time_start = time.monotonic()
+        time_stop = time_start + duration
+        if self.resetter:
+            self.resetter()
+        result = self._send_traffic_on_tg_with_ramp_up(
+            duration=duration,
+            rate=transmit_rate,
+            async_call=False,
         )
-        return self.get_measurement_result(duration, transmit_rate)
+        logger.debug(f"trial measurement result: {result!r}")
+        # In PLRsearch, computation needs the specified time to complete.
+        if self.sleep_till_duration:
+            sleeptime = time_stop - time.monotonic()
+            if sleeptime > 0.0:
+                # TODO: Sometimes we have time to do additional trials here,
+                # adapt PLRsearch to accept all the results.
+                time.sleep(sleeptime)
+        return result
+
+    def set_rate_provider_defaults(
+            self,
+            frame_size,
+            traffic_profile,
+            ppta=1,
+            resetter=None,
+            traffic_directions=2,
+            transaction_duration=0.0,
+            transaction_scale=0,
+            transaction_type=u"packet",
+            duration_limit=0.0,
+            negative_loss=True,
+            sleep_till_duration=False,
+            use_latency=False,
+            ramp_up_rate=None,
+            ramp_up_duration=None,
+            state_timeout=300.0,
+        ):
+        """Store values accessed by measure().
+
+        :param frame_size: Frame size identifier or value [B].
+        :param traffic_profile: Module name as a traffic profile identifier.
+            See GPL/traffic_profiles/trex for implemented modules.
+        :param ppta: Packets per transaction, aggregated over directions.
+            Needed for udp_pps which does not have a good transaction counter,
+            so we need to compute expected number of packets.
+            Default: 1.
+        :param resetter: Callable to reset DUT state for repeated trials.
+        :param traffic_directions: Traffic from packet counting point of view
+            is bi- (2) or uni- (1) directional.
+            Default: 2
+        :param transaction_duration: Total expected time to close transaction.
+        :param transaction_scale: Number of transactions to perform.
+            0 (default) means unlimited.
+        :param transaction_type: An identifier specifying which counters
+            and formulas to use when computing attempted and failed
+            transactions. Default: "packet".
+            TODO: Does this also specify parsing for the measured duration?
+        :param duration_limit: Zero or maximum limit for computed (or given)
+            duration.
+        :param negative_loss: If false, negative loss is reported as zero loss.
+        :param sleep_till_duration: If true and measurement returned faster,
+            sleep until it matches duration. Needed for PLRsearch.
+        :param use_latency: Whether to measure latency during the trial.
+            Default: False.
+        :param ramp_up_rate: Rate to use in ramp-up trials [pps].
+        :param ramp_up_duration: Duration of ramp-up trials [s].
+        :param state_timeout: Time of life of DUT state [s].
+        :type frame_size: str or int
+        :type traffic_profile: str
+        :type ppta: int
+        :type resetter: Optional[Callable[[], None]]
+        :type traffic_directions: int
+        :type transaction_duration: float
+        :type transaction_scale: int
+        :type transaction_type: str
+        :type duration_limit: float
+        :type negative_loss: bool
+        :type sleep_till_duration: bool
+        :type use_latency: bool
+        :type ramp_up_rate: float
+        :type ramp_up_duration: float
+        :type state_timeout: float
+        """
+        self.frame_size = frame_size
+        self.traffic_profile = str(traffic_profile)
+        self.resetter = resetter
+        self.ppta = ppta
+        self.traffic_directions = int(traffic_directions)
+        self.transaction_duration = float(transaction_duration)
+        self.transaction_scale = int(transaction_scale)
+        self.transaction_type = str(transaction_type)
+        self.duration_limit = float(duration_limit)
+        self.negative_loss = bool(negative_loss)
+        self.sleep_till_duration = bool(sleep_till_duration)
+        self.use_latency = bool(use_latency)
+        self.ramp_up_rate = float(ramp_up_rate)
+        self.ramp_up_duration = float(ramp_up_duration)
+        self.state_timeout = float(state_timeout)
 
 
 class OptimizedSearch:
@@ -1015,20 +1417,41 @@ class OptimizedSearch:
 
     @staticmethod
     def perform_optimized_ndrpdr_search(
-            frame_size, traffic_profile, minimum_transmit_rate,
-            maximum_transmit_rate, packet_loss_ratio=0.005,
-            final_relative_width=0.005, final_trial_duration=30.0,
-            initial_trial_duration=1.0, number_of_intermediate_phases=2,
-            timeout=720.0, doublings=1, traffic_directions=2):
+            frame_size,
+            traffic_profile,
+            minimum_transmit_rate,
+            maximum_transmit_rate,
+            packet_loss_ratio=0.005,
+            final_relative_width=0.005,
+            final_trial_duration=30.0,
+            initial_trial_duration=1.0,
+            number_of_intermediate_phases=2,
+            timeout=720.0,
+            doublings=1,
+            ppta=1,
+            resetter=None,
+            traffic_directions=2,
+            transaction_duration=0.0,
+            transaction_scale=0,
+            transaction_type=u"packet",
+            use_latency=False,
+            ramp_up_rate=None,
+            ramp_up_duration=None,
+            state_timeout=300.0,
+    ):
         """Setup initialized TG, perform optimized search, return intervals.
 
+        If transaction_scale is nonzero, all non-init trial durations
+        are set to 2.0 (as they do not affect the real trial duration)
+        and zero intermediate phases are used.
+        The initial phase still uses 1.0 seconds, to force remeasurement.
+        That makes initial phase act as a warmup.
+
         :param frame_size: Frame size identifier or value [B].
         :param traffic_profile: Module name as a traffic profile identifier.
             See GPL/traffic_profiles/trex for implemented modules.
-        :param minimum_transmit_rate: Minimal uni-directional
-            target transmit rate [pps].
-        :param maximum_transmit_rate: Maximal uni-directional
-            target transmit rate [pps].
+        :param minimum_transmit_rate: Minimal load in transactions per second.
+        :param maximum_transmit_rate: Maximal load in transactions per second.
         :param packet_loss_ratio: Fraction of packets lost, for PDR [1].
         :param final_relative_width: Final lower bound transmit rate
             cannot be more distant that this multiple of upper bound [1].
@@ -1042,8 +1465,24 @@ class OptimizedSearch:
         :param doublings: How many doublings to do in external search step.
             Default 1 is suitable for fairly stable tests,
             less stable tests might get better overal duration with 2 or more.
+        :param ppta: Packets per transaction, aggregated over directions.
+            Needed for udp_pps which does not have a good transaction counter,
+            so we need to compute expected number of packets.
+            Default: 1.
+        :param resetter: Callable to reset DUT state for repeated trials.
         :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
             Default: 2
+        :param transaction_duration: Total expected time to close transaction.
+        :param transaction_scale: Number of transactions to perform.
+            0 (default) means unlimited.
+        :param transaction_type: An identifier specifying which counters
+            and formulas to use when computing attempted and failed
+            transactions. Default: "packet".
+        :param use_latency: Whether to measure latency during the trial.
+            Default: False.
+        :param ramp_up_rate: Rate to use in ramp-up trials [pps].
+        :param ramp_up_duration: Duration of ramp-up trials [s].
+        :param state_timeout: Time of life of DUT state [s].
         :type frame_size: str or int
         :type traffic_profile: str
         :type minimum_transmit_rate: float
@@ -1055,48 +1494,94 @@ class OptimizedSearch:
         :type number_of_intermediate_phases: int
         :type timeout: float
         :type doublings: int
+        :type ppta: int
+        :type resetter: Optional[Callable[[], None]]
         :type traffic_directions: int
+        :type transaction_duration: float
+        :type transaction_scale: int
+        :type transaction_type: str
+        :type use_latency: bool
+        :type ramp_up_rate: float
+        :type ramp_up_duration: float
+        :type state_timeout: float
         :returns: Structure containing narrowed down NDR and PDR intervals
             and their measurements.
         :rtype: NdrPdrResult
         :raises RuntimeError: If total duration is larger than timeout.
         """
-        minimum_transmit_rate *= traffic_directions
-        maximum_transmit_rate *= traffic_directions
         # we need instance of TrafficGenerator instantiated by Robot Framework
         # to be able to use trex_stl-*()
         tg_instance = BuiltIn().get_library_instance(
             u"resources.libraries.python.TrafficGenerator"
         )
+        # Overrides for fixed transaction amount.
+        # TODO: Move to robot code? We have two call sites, so this saves space,
+        #       even though this is surprising for log readers.
+        if transaction_scale:
+            initial_trial_duration = 1.0
+            final_trial_duration = 2.0
+            number_of_intermediate_phases = 0
+            timeout += transaction_scale * 3e-4
         tg_instance.set_rate_provider_defaults(
-            frame_size, traffic_profile, traffic_directions=traffic_directions)
+            frame_size=frame_size,
+            traffic_profile=traffic_profile,
+            sleep_till_duration=False,
+            ppta=ppta,
+            resetter=resetter,
+            traffic_directions=traffic_directions,
+            transaction_duration=transaction_duration,
+            transaction_scale=transaction_scale,
+            transaction_type=transaction_type,
+            use_latency=use_latency,
+            ramp_up_rate=ramp_up_rate,
+            ramp_up_duration=ramp_up_duration,
+            state_timeout=state_timeout,
+        )
         algorithm = MultipleLossRatioSearch(
-            measurer=tg_instance, final_trial_duration=final_trial_duration,
+            measurer=tg_instance,
+            final_trial_duration=final_trial_duration,
             final_relative_width=final_relative_width,
             number_of_intermediate_phases=number_of_intermediate_phases,
-            initial_trial_duration=initial_trial_duration, timeout=timeout,
-            doublings=doublings
+            initial_trial_duration=initial_trial_duration,
+            timeout=timeout,
+            doublings=doublings,
         )
         result = algorithm.narrow_down_ndr_and_pdr(
-            minimum_transmit_rate, maximum_transmit_rate, packet_loss_ratio
+            min_rate=minimum_transmit_rate,
+            max_rate=maximum_transmit_rate,
+            packet_loss_ratio=packet_loss_ratio,
         )
         return result
 
     @staticmethod
     def perform_soak_search(
-            frame_size, traffic_profile, minimum_transmit_rate,
-            maximum_transmit_rate, plr_target=1e-7, tdpt=0.1,
-            initial_count=50, timeout=1800.0, trace_enabled=False,
-            traffic_directions=2, latency=False):
+            frame_size,
+            traffic_profile,
+            minimum_transmit_rate,
+            maximum_transmit_rate,
+            plr_target=1e-7,
+            tdpt=0.1,
+            initial_count=50,
+            timeout=7200.0,
+            ppta=1,
+            resetter=None,
+            trace_enabled=False,
+            traffic_directions=2,
+            transaction_duration=0.0,
+            transaction_scale=0,
+            transaction_type=u"packet",
+            use_latency=False,
+            ramp_up_rate=None,
+            ramp_up_duration=None,
+            state_timeout=300.0,
+    ):
         """Setup initialized TG, perform soak search, return avg and stdev.
 
         :param frame_size: Frame size identifier or value [B].
         :param traffic_profile: Module name as a traffic profile identifier.
             See GPL/traffic_profiles/trex for implemented modules.
-        :param minimum_transmit_rate: Minimal uni-directional
-            target transmit rate [pps].
-        :param maximum_transmit_rate: Maximal uni-directional
-            target transmit rate [pps].
+        :param minimum_transmit_rate: Minimal load in transactions per second.
+        :param maximum_transmit_rate: Maximal load in transactions per second.
         :param plr_target: Fraction of packets lost to achieve [1].
         :param tdpt: Trial duration per trial.
             The algorithm linearly increases trial duration with trial number,
@@ -1106,11 +1591,28 @@ class OptimizedSearch:
             This is needed because initial "search" phase of integrator
             takes significant time even without any trial results.
         :param timeout: The search will stop after this overall time [s].
+        :param ppta: Packets per transaction, aggregated over directions.
+            Needed for udp_pps which does not have a good transaction counter,
+            so we need to compute expected number of packets.
+            Default: 1.
+        :param resetter: Callable to reset DUT state for repeated trials.
         :param trace_enabled: True if trace enabled else False.
+            This is very verbose tracing on numeric computations,
+            do not use in production.
+            Default: False
         :param traffic_directions: Traffic is bi- (2) or uni- (1) directional.
             Default: 2
-        :param latency: Whether to measure latency during the trial.
+        :param transaction_duration: Total expected time to close transaction.
+        :param transaction_scale: Number of transactions to perform.
+            0 (default) means unlimited.
+        :param transaction_type: An identifier specifying which counters
+            and formulas to use when computing attempted and failed
+            transactions. Default: "packet".
+        :param use_latency: Whether to measure latency during the trial.
             Default: False.
+        :param ramp_up_rate: Rate to use in ramp-up trials [pps].
+        :param ramp_up_duration: Duration of ramp-up trials [s].
+        :param state_timeout: Time of life of DUT state [s].
         :type frame_size: str or int
         :type traffic_profile: str
         :type minimum_transmit_rate: float
@@ -1118,25 +1620,56 @@ class OptimizedSearch:
         :type plr_target: float
         :type initial_count: int
         :type timeout: float
+        :type ppta: int
+        :type resetter: Optional[Callable[[], None]]
         :type trace_enabled: bool
         :type traffic_directions: int
-        :type latency: bool
+        :type transaction_duration: float
+        :type transaction_scale: int
+        :type transaction_type: str
+        :type use_latency: bool
+        :type ramp_up_rate: float
+        :type ramp_up_duration: float
+        :type state_timeout: float
         :returns: Average and stdev of estimated aggregate rate giving PLR.
         :rtype: 2-tuple of float
         """
-        minimum_transmit_rate *= traffic_directions
-        maximum_transmit_rate *= traffic_directions
         tg_instance = BuiltIn().get_library_instance(
             u"resources.libraries.python.TrafficGenerator"
         )
+        # Overrides for fixed transaction amount.
+        # TODO: Move to robot code? We have a single call site
+        #       but MLRsearch has two and we want the two to be used similarly.
+        if transaction_scale:
+            # TODO: What is a good value for max scale?
+            # TODO: Scale the timeout with transaction scale.
+            timeout = 7200.0
         tg_instance.set_rate_provider_defaults(
-            frame_size, traffic_profile, traffic_directions=traffic_directions,
-            negative_loss=False, latency=latency)
+            frame_size=frame_size,
+            traffic_profile=traffic_profile,
+            negative_loss=False,
+            sleep_till_duration=True,
+            ppta=ppta,
+            resetter=resetter,
+            traffic_directions=traffic_directions,
+            transaction_duration=transaction_duration,
+            transaction_scale=transaction_scale,
+            transaction_type=transaction_type,
+            use_latency=use_latency,
+            ramp_up_rate=ramp_up_rate,
+            ramp_up_duration=ramp_up_duration,
+            state_timeout=state_timeout,
+        )
         algorithm = PLRsearch(
-            measurer=tg_instance, trial_duration_per_trial=tdpt,
+            measurer=tg_instance,
+            trial_duration_per_trial=tdpt,
             packet_loss_ratio_target=plr_target,
-            trial_number_offset=initial_count, timeout=timeout,
-            trace_enabled=trace_enabled
+            trial_number_offset=initial_count,
+            timeout=timeout,
+            trace_enabled=trace_enabled,
+        )
+        result = algorithm.search(
+            min_rate=minimum_transmit_rate,
+            max_rate=maximum_transmit_rate,
         )
-        result = algorithm.search(minimum_transmit_rate, maximum_transmit_rate)
         return result