From: Paul Davies C <[email protected]>

Here is the test module for time command(/usr/bin/time).Aspects that tested are
1.real time
2.user time
3.system time
4.Max and Min page faults
5.Voluntary and non-voluntary contex switches
6.Max Resident set size (Max RSS)

Max RSS is already broken.The output given for Max RSS is 4 times the actual 
value.

---
 test_time/consumer.c   |   36 ++++++++++++++
 test_time/control      |   13 +++++
 test_time/test_time.py |  120 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 169 insertions(+)
 create mode 100644 test_time/consumer.c
 create mode 100644 test_time/control
 create mode 100644 test_time/test_time.py

diff --git a/test_time/consumer.c b/test_time/consumer.c
new file mode 100644
index 0000000..1dcb232
--- /dev/null
+++ b/test_time/consumer.c
@@ -0,0 +1,36 @@
+#include<stdio.h>
+#include<string.h>
+#include<signal.h>
+#include<unistd.h>
+#include<fcntl.h>
+int main(int argc,char *argv[])
+{
+       char fileName[50];
+       int pid;
+       if(fork() == 0){
+               sleep(4); /*Wait,so that parent can do something before getting 
paused */
+               kill(getppid(), SIGSTOP); //Pause the parent process
+               /*Copy the proc/ files */
+               sprintf(fileName,"/proc/%d/stat",getppid());
+               if(fork() == 0)
+                       execl("/bin/cp","cp",fileName,argv[1],NULL);
+               sprintf(fileName,"/proc/%d/smaps",getppid());
+               if(fork() == 0)
+                       execl("/bin/cp","cp",fileName,argv[2],NULL);
+               sprintf(fileName,"/proc/%d/status",getppid());
+               if(fork() == 0)
+                       execl("/bin/cp","cp",fileName,argv[3],NULL);
+               else{
+                       sleep(2);
+                       /*Kill the parent*/
+                       kill(getppid(),SIGKILL);
+               }
+       }
+       else{
+               while(1){
+                       /* System calls so that it adds to sys time!*/
+                       pid = getpid();
+                       pid = getppid();
+               }
+       }
+}
diff --git a/test_time/control b/test_time/control
new file mode 100644
index 0000000..687cb6a
--- /dev/null
+++ b/test_time/control
@@ -0,0 +1,13 @@
+AUTHOR = '[email protected] (Paul Davies C)'
+TIME = 'SHORT'
+NAME = 'time'
+TEST_TYPE = 'client'
+TEST_CLASS = 'Kernel'
+TEST_CATEGORY = 'Functional'
+
+DOC = '''
+Tests the functionality of the time(/usr/bin/time) command.
+'''
+tests=['real_time','other_resources']
+for i in tests:
+    job.run_test('test_time',test=i,tag=i)
diff --git a/test_time/test_time.py b/test_time/test_time.py
new file mode 100644
index 0000000..5e2aa63
--- /dev/null
+++ b/test_time/test_time.py
@@ -0,0 +1,120 @@
+#!/usr/bin/python
+import os, shutil, glob, logging,subprocess,time
+from autotest.client import test, utils
+from autotest.client.shared import error
+class test_time(test.test):
+    version = 1
+    nfail = 0
+    def setup(self):
+        shutil.copyfile(os.path.join(self.bindir, 'consumer.c'),
+                        os.path.join(self.srcdir, 'consumer.c'))
+        os.chdir(self.srcdir)
+        utils.system(utils.get_cc() + ' consumer.c -o consumer')
+
+    def initialize(self,test):
+        self.statfile = "/tmp/stat-%s" % int(time.time())
+        self.statusfile = "/tmp/status-%s" % int(time.time())
+        self.smapsfile = "/tmp/smaps-%s" % int(time.time())
+        self.test = test
+
+    def run_once(self,test):
+        '''Test 1 : Verifies the real time .
+           Test 2 : Verifies the user and system time,min and max page faults,
+                    voluntary and non-voluntary context switches and the max 
Rss'''
+        test = getattr(self,test);
+        try:
+            test()
+        except Exception as e:
+            self.nfail = self.nfail + 1
+            logging.error(e)
+
+    def real_time(self):
+        arg=['/usr/bin/time','-f','%E','sleep','1.37']
+        op=subprocess.Popen(arg, stderr = 
subprocess.PIPE).stderr.read().rstrip('\n').split(':')
+        if float(op[1]) == 1.37:
+            logging.info('Test: Real time.Test Sucess')
+        else:
+            raise error.TestError('Test: Real time.Test failure ')
+
+    def other_resources(self):        
+        '''The *time* is made to run the consumer.c program. consumer.c forks 
+           a child process,which will pause its parent and copies the stat,
+           status and the smaps file of parent.Imediately after copying, the 
+           parent process is killed. The copied files are used as a reference
+           for verifying the output of *time*'''
+        consumer = os.path.join(self.srcdir,'consumer')
+        utils.system('chmod +x '+consumer)
+        arg = ['/usr/bin/time','-f','%U %S %R %F %M %c %w %x', 
consumer,self.statfile,self.smapsfile,self.statusfile]
+        op = subprocess.Popen(arg,stderr=subprocess.PIPE).stderr.read().split()
+        fd = open(self.statfile,'r')
+        stat = fd.readlines()[0].split(' ')
+
+        '''Verify user time'''
+        if float(stat[13])/100 == float(op[5]):
+            logging.info('Test : User time. Success')
+        else:
+            raise error.TestError('Test: User Time.Failed')
+
+        '''Verify system time'''
+        if float(stat[14])/100 == float(op[6]):
+            logging.info('Test : System time. Sucess')
+        else:
+            raise error.TestError('Test: System Time. Failed')
+    
+        '''Verify min page faults'''
+        if int(stat[9]) == int(op[7]):
+            logging.info('Test : Min page faults. Success')
+        else:
+            raise error.TestError('Test: Min page faults. Failed')
+
+        '''Verify max page faults'''
+        if int(stat[11]) == int(op[8]):
+            logging.info('Test : Max page faults. Sucess')
+        else:
+            raise error.TestError('Test: Max page faults. Failed')
+        fd.close()
+        
+        '''Verify Context switches'''
+        j = 0
+        fd = open(self.statusfile,"r");
+        for i in fd.readlines():
+            if "voluntary_ctxt_switches:" in i:
+                if j == 0:
+                    csw=int(i.split(":")[1]) + 1
+                    j = j + 1
+                else:
+                    ncsw = int(i.split(":")[1])
+
+        '''Verify non voluntary Context switches'''
+        if ncsw == int(op[10]):
+            logging.info('Test : Non voluntary context switch. Sucess')
+        else:
+            raise error.TestError('Test: Non voluntary context switch. Failed')
+
+        '''Verify voluntary Context switches'''
+        if csw == int(op[11]):
+            logging.info('Test : Voluntary context switch. Sucess')
+        else:
+            raise error.TestError('Test: Voluntary context switch. Failed')
+            
+        '''Verify Max Rss'''
+        rss = self.getMaxRss()
+        if rss == int(op[8]):
+            logging.info('Test : Max Rss.Max Rss test : Sucess')
+        else:
+            raise error.TestError('Test: Max Rss.Max Rss test : failed')
+       
+    def getMaxRss(self):
+        '''Reads the smaps file and adds up all the RSS values to evaluate max 
rss '''
+        maxRss = 0
+        fd = open(self.smapsfile)
+        for i in fd.readlines():
+            if "Rss:" in i:
+                maxRss =  maxRss+int(i.split(':')[1].strip().split(" ")[0])
+        return maxRss
+
+    def postprocess(self):
+        if self.nfail != 0:
+            raise error.TestError('\nTest failed : %s' % self.test)
+        else :
+            logging.info('\nTest completed successfully : %s' % self.test)

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to