TEST-2 load/soak test utility
[testsuite/python-testing-utils.git] / loadtest / TestController.py
1 '''
2 Created on Apr 7, 2017
3
4 @author: jf9860
5 '''
6 import time
7 import os
8 from loadtest.RunEte import RunEte
9 from loadtest.TestConfig import TestConfig
10 import logging
11
12 class TestController(object):
13     '''
14     classdocs
15     '''
16
17     threads = {}
18     threadid = 0
19     soaksubfolder = 'soak_' + str(os.getpid())
20     test_number = 0
21
22     def __init__(self, options):
23         '''
24         Constructor
25         '''
26         self.config = TestConfig(duration=options.duration)
27         logging.info(self.config.to_string())
28
29     def execute(self):
30         starttime = time.time()
31         endtime = starttime + self.config.duration
32         profileindex = 0
33         currenttime = time.time()
34         logging.info("{}:{}:{}".format(starttime, endtime, currenttime))
35         while currenttime < endtime:
36             if (profileindex >= len(self.config.profile)):
37                 profileindex = 0
38             profile = self.config.profile[profileindex]
39             sleeptime = profile[0]
40             currenttime = time.time()
41             if ((currenttime + sleeptime) < endtime):
42                 time.sleep(sleeptime)
43                 self.schedule(profile)
44                 profileindex = profileindex + 1
45                 currenttime = time.time()
46             else:
47                 currenttime = endtime
48
49         for threadname in self.threads:
50             logging.info("TestController waiting on " + threadname)
51             t = self.threads[threadname]
52             t.join()
53         logging.info("Soak test completed")
54
55     def schedule(self, profile):
56         self.remove_completed_threads()
57         tests = profile[1]
58         for test in tests:
59             self.schedule_one(test)
60
61     def schedule_one(self, test):
62         self.test_number = self.test_number + 1
63         self.threadid = self.threadid + 1
64         threadname = "RunEte_" + str(self.threadid)
65         ''' test for max threads '''
66         t = RunEte(test, self.soaksubfolder, str(self.test_number))
67         t.setName(threadname)
68         t.start()
69         self.threads[threadname] = t
70
71
72     def remove_completed_threads(self):
73         toremove = []
74         for threadname in self.threads:
75             t = self.threads[threadname]
76             if (t.isAlive() == False):
77                 toremove.append(threadname)
78         for threadname in toremove:
79             logging.info("Removing " + threadname)
80             del(self.threads[threadname])