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