loadtest.tcl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/tclsh
  2. #
  3. # Usage (as root):
  4. #
  5. # $ tclsh loadtest.tcl
  6. #
  7. # Copyleft 2005 by Chris Maj <cmaj_at_freedomcorpse_dot_com>
  8. #
  9. # Create a (huge) bunch of call files to dial via pbx_spool.
  10. # Defaults are selected with 'Enter' and, if all defaults
  11. # are selected, you'll dial DAHDI/1/s into default|s|1
  12. #
  13. # where Asterisk's pbx/pbx_spool.c will be looking for work
  14. set SPOOLDIR /var/spool/asterisk/outgoing
  15. # pbx_spool is fairly aggresive, so make files here first
  16. set TEMPDIR /tmp
  17. if { ![file writable $SPOOLDIR] } {
  18. puts "Do you need to be root to write to $SPOOLDIR ?"
  19. exit
  20. }
  21. if { ![file readable $TEMPDIR] } {
  22. puts "Do you need to be root to read from $TEMPDIR ?"
  23. exit
  24. }
  25. if { ![file writable $TEMPDIR] } {
  26. puts "Do you need to be root to write to $TEMPDIR ?"
  27. exit
  28. }
  29. # gets some input from the user
  30. proc get {var_ default_ prompt_} {
  31. global $var_
  32. puts $prompt_
  33. if { $default_ != "" } {
  34. puts -nonewline "(default: $default_) ? "
  35. } else {
  36. puts -nonewline "? "
  37. }
  38. flush stdout
  39. gets stdin $var_
  40. if { [set $var_] == "" && $default_ != "" } {
  41. set $var_ $default_
  42. }
  43. }
  44. # puts the user requested channels into a neat, ordered list
  45. proc splitchans {inch_} {
  46. global changroup
  47. set outch [list]
  48. foreach range [split $inch_ {, }] {
  49. set start [lindex [split $range -] 0]
  50. set stop [lindex [split $range -] end]
  51. if { [string is digit $start] && [string is digit $stop] } {
  52. set ::changroup "channel"
  53. for {set ch $start} {$ch <= $stop} {incr ch} {
  54. if { [lsearch $outch $ch] == -1 } {
  55. lappend outch $ch
  56. }
  57. }
  58. } else {
  59. set ::changroup "group"
  60. foreach ch [split $range -] {
  61. lappend outch $ch
  62. }
  63. }
  64. }
  65. return [lsort -dictionary $outch]
  66. }
  67. # writes out a file in the temporary directory,
  68. # then changes the mtime of the file before
  69. # sticking it into the outgoing spool directory
  70. # (where pbx_spool will be looking)
  71. proc spool {channel_ callcnt_ when_} {
  72. set callstr "
  73. Channel: $::technology/$channel_/$::destination
  74. Context: $::context
  75. Extension: $::extension
  76. Priority: $::priority
  77. WaitTime: $::timeout
  78. RetryTime: $::retrytime
  79. MaxRetries: $::maxretries
  80. Callerid: $::clid
  81. SetVar: $::astvar
  82. Account: $::account
  83. "
  84. set fn "loadtest.call$callcnt_.ch$channel_"
  85. set fd [open $::TEMPDIR/$fn w]
  86. puts $fd $callstr
  87. close $fd
  88. file mtime $::TEMPDIR/$fn $when_
  89. file rename -force $::TEMPDIR/$fn $::SPOOLDIR/$fn
  90. }
  91. # prompt the user for some info
  92. get technology "DAHDI" "\nEnter technology type
  93. DAHDI, IAX, SIP, etc."
  94. get chans "1" "\nEnter channel(s) or group to test in formats like
  95. 2\n1-4\n3 5 7 9\n1-23,25-47,49-71,73-95\ng4\ng2,g1"
  96. set channels [splitchans $chans]
  97. get destination "s" "\nEnter destination number"
  98. get context "default" "\nEnter context"
  99. get extension "s" "\nEnter extension"
  100. get priority "1" "\nEnter priority"
  101. get timeout "45" "\nEnter timeout for call to be answered in seconds"
  102. get maxretries "0" "\nEnter maximum number of retries"
  103. if { $maxretries > 0 } {
  104. get retrytime "300" "\nEnter time between retries in seconds"
  105. } else {
  106. set retrytime 300
  107. }
  108. get clid "" "\nEnter callerid"
  109. get astvar "" "\nEnter some extra variables"
  110. get account "loadtest" "\nEnter account code"
  111. get calls "1" "\nEnter number of test calls per $changroup"
  112. get period "60" "\nEnter period between placing calls on a particular $changroup in seconds"
  113. if { [llength $channels] > 1 } {
  114. get rate "0" "\nEnter period between placing each call in seconds
  115. 0 will send a call on each $changroup every $period seconds
  116. 1 will send a call on $changroup [lindex $channels 0] at [expr {$period + 0}]s, [lindex $channels 1] at [expr {$period + 1 }]s, etc.
  117. 5 will send a call on $changroup [lindex $channels 0] at [expr {$period + 0}]s, [lindex $channels 1] at [expr {$period + 5 }]s, etc."
  118. } else {
  119. set rate 0
  120. }
  121. puts -nonewline "\nCreating spooled call files... "
  122. set now [clock seconds]
  123. set spoolcnt 0
  124. set spinner [list / - \\ |]
  125. for {set i 0} {$i < $calls} {incr i} {
  126. foreach ch $channels {
  127. set chidx [lsearch $channels $ch]
  128. spool $ch [incr spoolcnt] [expr {$now + ($i * $period) + ($rate * $chidx)}]
  129. puts -nonewline "\b"
  130. puts -nonewline [lindex $spinner [expr {$spoolcnt % 4}]]
  131. flush stdout
  132. }
  133. }
  134. puts "\b$spoolcnt calls placed into $SPOOLDIR !"