Skip to contentSkip to conversation

Adding stressor validation for stress-ng

PATCH New Sana Sharma 1 participant 1 message Aug 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Adding stressor validation for stress-ng

Previously stressor names were unvalidated.
This has been changed so that now if the user inputs an invalid stressor name, they get an error message telling them to check "stress-ng --help". The list of stressors is created dynamically during "WorkloadPrepare()". The helper functions are currently a part of the class but we could also make them static methods, depending on opinions.

---
 rteval/modules/loads/stressng.py | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
rteval/modules/loads/stressng.py +20 -0
index 32e2dbd..a72ab9d 100644
@@ -31,6 +31,25 @@ class Stressng(CommandLineLoad):
         # When this module runs, other load modules should not
         self.set_exclusive()
 
+    def get_valid_stressors(self):
+        """Query stress-ng for list of valid stressor names."""
+        try:
+            result = subprocess.run(['stress-ng', '--stressors'],
+                                  capture_output=True, text=True, check=True)
+            return result.stdout.strip().split()
+        except (subprocess.CalledProcessError, FileNotFoundError):
+            return []
+
+    def validate_stressor(self,stressor_name):
+        """Validate a single stressor name against stress-ng's available stressors."""
+        valid = self.get_valid_stressors()
+        if not valid:
+            return
+
+        if stressor_name not in valid:
+            raise ValueError(f"Invalid stress-ng stressor: '{stressor_name}'. "
+                            f"Run 'stress-ng --stressors' to see valid options.")
+
     def _WorkloadSetup(self):
         " Since there is nothing to build, we don't need to do anything here "
         return
@@ -51,6 +70,7 @@ class Stressng(CommandLineLoad):
 
         # stress-ng is only run if the user specifies an stressor
         self.args = ['stress-ng']
+        self.validate_stressor(self.cfg.stressor)
         self.args.append(f'--{str(self.cfg.stressor)}')
         if self.cfg.workers is not None:
             self.args.append(self.cfg.workers) #default is 0
Signature · 1 line
2.54.0