-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
daemon-test.sh
executable file
·199 lines (174 loc) · 4.27 KB
/
daemon-test.sh
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
# -- vw daemon test
#
NAME='vw-daemon-test'
export PATH="vowpalwabbit:../build/vowpalwabbit:${PATH}"
# The VW under test
VW=`which vw`
MODEL=$NAME.model
TRAINSET=$NAME.train
PREDREF=$NAME.predref
PREDOUT=$NAME.predict
NETCAT_STATUS=$NAME.netcat-status
PORT=54248
while [ $# -gt 0 ]
do
case "$1" in
--foreground)
Foreground="$1"
;;
--json)
JSON="$1 --chain_hash"
;;
--port)
PORT="$2"
shift
;;
--vw)
VW="$2"
shift
;;
*)
echo "$NAME: unknown argument $1"
exit 1
;;
esac
shift
done
# Test if the VW command is available
${VW} --version > /dev/null 2>&1
# -- make sure we can find vw first
if [ $? -eq 0 ]; then
: cool found vw at: $VW
else
echo "$NAME: can not find 'vw' in $PATH - sorry"
exit 1
fi
# -- and netcat
NETCAT=`which netcat`
if [ -x "$NETCAT" ]; then
: cool found netcat at: $NETCAT
else
NETCAT=`which nc`
if [ -x "$NETCAT" ]; then
: "no netcat but found 'nc' at: $NETCAT"
else
echo "$NAME: can not find 'netcat' not 'nc' in $PATH - sorry"
exit 1
fi
fi
# -- and pkill
PKILL=`which pkill`
if [ -x "$PKILL" ]; then
: cool found pkill at: $PKILL
else
echo "$NAME: can not find 'pkill' in $PATH - sorry"
exit 1
fi
# A command (+pattern) that is unlikely to match anything but our own test
DaemonCmd="$VW -t -i $MODEL --daemon $Foreground --num_children 1 --quiet --port $PORT $JSON"
# libtool may wrap vw with '.libs/lt-vw' so we need to be flexible
# on the exact process pattern we try to kill.
DaemonPat=`echo $DaemonCmd | sed 's/^[^ ]*vw /.*vw /'`
stop_daemon() {
# Make sure we are not running. May ignore 'error' that we're not
$PKILL -9 -f "$DaemonPat" 2>&1 | grep -q 'no process found'
# relinquish CPU by forcing some context switches to be safe
# (let existing vw daemon procs die)
if echo "$DaemonPat" | grep -q -v -- --foreground; then
wait
else
sleep 0.1
fi
}
start_daemon() {
# echo starting daemon
$DaemonCmd </dev/null >/dev/null &
PID=$!
# give it time to be ready
if echo "$DaemonCmd" | grep -q -v -- --foreground; then
wait; wait; wait
else
sleep 0.1
fi
echo "$PID"
}
cleanup() {
/bin/rm -f $MODEL $TRAINSET $PREDREF $PREDOUT $NETCAT_STATUS
stop_daemon
}
# -- main
cleanup
txt_dataset() {
# prepare training set
cat > $TRAINSET <<EOF
0.55 1 '1| a
0.99 1 '2| b c
EOF
}
json_dataset() {
# prepare training set json
cat > $TRAINSET <<EOF
{"_label":{"Weight":1, "Label":0.55}, "_tag":"1", "a":true}
{"_label":{"Weight":1, "Label":0.99}, "_tag":"2", "b":true, "c":true}
EOF
}
if [ -n "$JSON" ]; then
json_dataset
else
txt_dataset
fi
# prepare expected predict output
cat > $PREDREF <<EOF
0.553585 1
0.733882 2
EOF
# Train
$VW -b 10 --quiet -d $TRAINSET -f $MODEL $JSON
DaemonPid=`start_daemon`
# Test --foreground argument
PidsAreEqual=false
for ProcessPid in $(pgrep -f "$DaemonPat" 2>&1)
do
if [ $DaemonPid -eq $ProcessPid ]; then
PidsAreEqual=true
fi
done
if [ $Foreground ]; then
if ! $PidsAreEqual ; then
echo "$NAME FAILED: --foreground, but vw has run in the background"
stop_daemon
exit 1
fi
else
if $PidsAreEqual ; then
echo "$NAME FAILED: vw has not run in the background"
stop_daemon
exit 1
fi
fi
# Test on train-set
# OpenBSD netcat quits immediately after stdin EOF
# nc.traditional does not, so let's use -w 1 which will silently close the connection if the connection
# or stdin are silent for more than 1 second
DELAY_OPT="-w 1"
# In case the VW command run immediately before is not yet ready to accept connections wait for a little bit
sleep 1
$NETCAT $DELAY_OPT localhost $PORT < $TRAINSET > $PREDOUT
$PKILL -9 $NETCAT
# We should ignore small (< $Epsilon) floating-point differences (fuzzy compare)
diff <(cut -c-5 $PREDREF) <(cut -c-5 $PREDOUT)
case $? in
0) echo "$NAME: OK"
cleanup
exit 0
;;
1) echo "$NAME FAILED: see $PREDREF vs $PREDOUT"
stop_daemon
exit 1
;;
*) echo "$NAME: diff failed - something is fishy"
stop_daemon
exit 2
;;
esac