Wejn s.r.o.

Solving complicated IT problems is our hobby.

Wowza Automatic Performance Tuning

If you’re like me and need to install and tune a new Wowza Media Server instance every once in a while, you’ve no doubt found the editing of conf/VHost.xml, as described on the How to do performance tuning page, quite tedious.

Fortunately, there’s a better way – you can let a simple bash+xmlstarlet script do the dirty work of tuning conf/VHost.xml and bin/setenv.sh for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash

# =====================================================================
# Wowza Media Server auto-tuning script
#
# Allows you to automatically perform basic performance tuning,
# according to guidelines posted by Wowza Media Systems:
# http://www.wowza.com/forums/content.php?46
#
# ---------------------------------------------------------------------
#
# (C) 2003, Michal "Wejn" Jirku (box at wejn dot com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# =====================================================================

# prereqs
(exec >/dev/null 2>&1; type xmlstarlet && type grep && type sed)
if [ $? -ne 0 ]; then
  echo "Prereqs (xmlstarlet, grep, sed) are not satisfied." >&2
  exit 1
fi

# path to WMS
if [ $# -ne 1 ]; then
  WMS_DIR=/usr/local/WowzaMediaServer
else
  WMS_DIR=$1
fi

if [ -d "$WMS_DIR" -a -f "$WMS_DIR/conf/VHost.xml" \
      -a -f "$WMS_DIR/bin/setenv.sh" ]; then
  true
else
  echo "Directory $WMS_DIR is not valid WMS install." >&2
  echo "Please supply valid dir as first parameter." >&2
  exit 1
fi

# Mem & arch
grep -q '\<lm\>' /proc/cpuinfo
if [ $? -eq 0 ]; then
  # 64 bit
  SIXTYFOUR=1
else
  SIXTYFOUR=0
fi

MEMKB=$(sed '/^MemTotal/{s/.*: *//;s/ .*//;p};d' /proc/meminfo)
MB=$[ $MEMKB / 1000 ]
# less than 3GB or 32bit
if [ $MB -lt 3000 -o $SIXTYFOUR -eq 0 ]; then
  HEAP=1500
else
  # over 16GB
  if [ $MB -gt 16000 ]; then
      HEAP=8000
  else
      # 75% of memory, less than 5GB
      HEAP=$[ $MB * 750 / 1000 ]
      if [ $HEAP -gt 5000 ]; then
          HEAP=5000
      fi
  fi
fi

# Core count
CPUS=$(grep ^processor /proc/cpuinfo | wc -l)
if [ $CPUS -gt 12 ]; then
  CPUS=12
fi
CPUS2=$[ $CPUS * 2 ]

# HandlerThreadPool
HTP=$[ $CPUS * 60 ]
if [ $HTP -gt 480 ]; then
  HTP=480
fi

# TransportThreadPool
TTP=$[ $CPUS * 40 ]
if [ $TTP -gt 320 ]; then
  HTP=320
fi

echo -e "Will set:"
echo -e "------------------------"
echo -e "Heap size:\t${HEAP} MB"
echo -e "CPUs:\t\t$CPUS"
echo -e "Threadpools:\t$HTP/$TTP"
echo -e "(enter to confirm)"
read A

# let's do the work
sed -i "s/-Xmx[0-9]*[mMkKgG]/-Xmx${HEAP}M/" $WMS_DIR/bin/setenv.sh
sed -i '/^#.*UseParNewGC/{s/^.//}' $WMS_DIR/bin/setenv.sh
R='/Root/VHost'
f() {
  xmlstarlet ed -O -L -P -u "$1" -v "$2" $WMS_DIR/conf/VHost.xml
}
f $R'/HostPortList/HostPort/Port[text()!=8086]/../ProcessorCount' \
  $CPUS2
f $R'/IdleWorkers/WorkerCount' $CPUS2
f $R'/NetConnections/ProcessorCount' $CPUS2
f $R'/RTP/UnicastIncoming/ProcessorCount' $CPUS
f $R'/RTP/UnicastOutgoing/ProcessorCount' $CPUS2
f $R'/RTP/MulticastIncoming/ProcessorCount' $CPUS
f $R'/RTP/MulticastOutgoing/ProcessorCount' $CPUS
f $R'/HandlerThreadPool/PoolSize' $HTP
f $R'/TransportThreadPool/PoolSize' $TTP

echo "All done."

If you’re interested, you can download the script so you don’t have to copy & paste it.

Btw, to get xmlstarlet on CentOS you’ll have to add EPEL repo (or so I’m told). On Debian Squeeze it’s just simple apt-get install xmlstarlet.

Update 2013-07-08: xmlstarlet 1.3.1 (on Debian Wheeze) formats the conf/VHost.xml file in a strange fashion when you use both -P -S. Updated the script to reflect (avoid) that.