sippwrap.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2012 by the Free Software Foundation, Inc.
  4. #
  5. # Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. import os
  21. class SippWrapper:
  22. """ Wrapper taht allow for managing sipp command line easily """
  23. def __init__(self):
  24. self.commandLine = "./sipp"
  25. self.remoteServer = ""
  26. self.remotePort = ""
  27. self.localInterface = ""
  28. self.localPort = ""
  29. self.customScenarioFile = ""
  30. self.isUserAgenClient = True
  31. self.launchInBackground = False
  32. self.numberOfCall = 0
  33. self.numberOfSimultaneousCall = 0
  34. self.enableTraceMsg = False
  35. self.enableTraceShormsg = False
  36. self.enableTraceScreen = False
  37. self.enableTraceError = False
  38. self.enableTraceStat = False
  39. self.enableTraceCounts = False
  40. self.enableTraceRtt = False
  41. self.enableTraceLogs = False
  42. def buildCommandLine(self, port):
  43. """ Fill the command line arguments based on specified parameters """
  44. self.localPort = str(port)
  45. if not self.remotePort and not self.remoteServer:
  46. self.isUserAgentClient = False
  47. elif self.remotePort and not self.remoteServer:
  48. print "Error cannot have remote port specified with no server"
  49. return
  50. if self.remoteServer:
  51. self.commandLine += " " + self.remoteServer
  52. if self.remotePort:
  53. self.commandLine += ":" + self.remotePort
  54. if self.localInterface:
  55. self.commandLine += " -i " + self.localInterface
  56. if self.localPort:
  57. self.commandLine += " -p " + self.localPort
  58. if self.customScenarioFile:
  59. self.commandLine += " -sf " + self.customScenarioFile
  60. elif self.isUserAgentClient is True:
  61. self.commandLine += " -sn uac"
  62. elif self.isUserAgentClient is False:
  63. self.commandLine += " -sn uas"
  64. if self.launchInBackground:
  65. self.commandLine += " -bg"
  66. if self.numberOfCall:
  67. self.commandLine += " -m " + str(self.numberOfCall)
  68. if self.numberOfSimultaneousCall:
  69. self.commandLine += " -l " + str(self.numberOfSimultaneousCall)
  70. if self.enableTraceMsg:
  71. self.commandLine += " -trace_msg"
  72. if self.enableTraceShormsg:
  73. self.commandLine += " -trace_shortmsg"
  74. if self.enableTraceScreen:
  75. self.commandLine += " -trace_screen"
  76. if self.enableTraceError:
  77. self.commandLine += " -trace_err"
  78. if self.enableTraceStat:
  79. self.commandLine += " -trace_stat"
  80. if self.enableTraceCounts:
  81. self.commandLine += " -trace_counts"
  82. if self.enableTraceRtt:
  83. self.commandLine += " -trace_rtt"
  84. if self.enableTraceLogs:
  85. self.commandLine += " -trace_logs"
  86. def launch(self):
  87. """ Launch the sipp instance using the specified arguments """
  88. print self.commandLine
  89. return os.system(self.commandLine + " 2>&1 > /dev/null")
  90. class SippScreenStatParser:
  91. """ Class that parse statistic reported by a sipp instance
  92. report some of the most important value """
  93. def __init__(self, filename):
  94. print "Opening " + filename
  95. self.logfile = open(filename, "r").readlines()
  96. print self.logfile[39]
  97. print self.logfile[40]
  98. def isAnyFailedCall(self):
  99. """ Look for any failed call
  100. Return true if there are failed call, false elsewhere """
  101. # TODO: Find a better way to determine which line to consider
  102. if "Failed call" not in self.logfile[40]:
  103. print "Error: Could not find 'Failed call' statistics"
  104. # We consider this as a failure
  105. return True
  106. return "1" in self.logfile[40]
  107. def isAnySuccessfulCall(self):
  108. """ Look for any successful call
  109. Return true if there are successful call, false elsewhere """
  110. # TODO: Find a better way to determine which line to consider
  111. if "Successful call" not in self.logfile[39]:
  112. print "Error: Could not find 'Successful call' statistics"
  113. return False
  114. return "1" in self.logfile[39]
  115. def test_result_parsing():
  116. dirlist = os.listdir("./")
  117. logfile = [x for x in dirlist if "screen.log" in x]
  118. testResult = SippScreenStatParser(logfile[0])
  119. assert(not testResult.isAnyFailedCall())
  120. assert(testResult.isAnySuccessfulCall())