-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.sh
executable file
·129 lines (113 loc) · 3.41 KB
/
benchmark.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
#!/bin/bash
cleanup() {
echo "Cleanup...."
[ -e "$INSERT_FILE" ] && unlink "$INSERT_FILE"
[ -e "$FIND_FILE" ] && unlink "$FIND_FILE"
[ -e "post.lua" ] && unlink "post.lua"
[ -e "get.lua" ] && unlink "get.lua"
[ -e "rwget.lua" ] && unlink "rwget.lua"
echo "Cleanup completed"
}
# trap for cleanup on signals
trap cleanup EXIT SIGINT SIGTERM
# Generate random numbers for insert and find operations
INSERT_FILE="inserts.json"
FIND_FILE="finds.txt"
NUM_ENTRIES=100000
# Create random insert JSON
echo "[" > $INSERT_FILE
for ((i=0; i<$NUM_ENTRIES; i++)); do
VALUE=$((RANDOM))
if [ $i -eq $((NUM_ENTRIES-1)) ]; then
echo "{\"index\": $i, \"value\": $VALUE}" >> $INSERT_FILE
echo "$VALUE" >> $FIND_FILE
else
echo "{\"index\": $i, \"value\": $VALUE}," >> $INSERT_FILE
echo "$VALUE" >> $FIND_FILE
fi
done
echo "]" >> $INSERT_FILE
# Insert the numbers into the server
echo "Inserting numbers into the server..."
while IFS= read -r line; do
index=$(echo $line | sed 's/.*"index": \([0-9]*\).*/\1/')
value=$(echo $line | sed 's/.*"value": \([0-9]*\).*/\1/')
if ! curl -s -X POST -H "Content-Type: application/json" -d "$line" http://localhost:8080/v2/numbers/$index/$value > /dev/null; then
echo "Error inserting $line"
exit 1
fi
done < <(tail -n +2 $INSERT_FILE | head -n -1 | sed 's/,$//')
# Create a script for wrk to use for POST requests
cat <<EOF > post.lua
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
request = function()
local index = math.random(0, $NUM_ENTRIES-1)
local value = math.random(0, 32767)
local body = string.format('{"index": %d, "value": %d}', index, value)
return wrk.format(nil, "/v2/numbers/" .. index .. "/" .. value, nil, body)
end
EOF
# Create a script for wrk to use for GET requests
cat <<EOF > get.lua
wrk.method = "GET"
values = {}
index = 1
$(awk '{print "table.insert(values, \"" $0 "\")"}' $FIND_FILE)
request = function()
local path = "/v2/numbers/value/" .. values[index]
index = index + 1
if index > #values then
index = 1
end
return wrk.format(nil, path)
end
EOF
# Create a script for wrk to use for GET requests for RWMutex
cat <<EOF > rwget.lua
wrk.method = "GET"
values = {}
index = 1
$(awk '{print "table.insert(values, \"" $0 "\")"}' $FIND_FILE)
request = function()
local path = "/v2/numbers/rwmutex/value/" .. values[index]
index = index + 1
if index > #values then
index = 1
end
return wrk.format(nil, path)
end
EOF
# Benchmark the insert operation
echo "Benchmarking insert operation..."
wrk -t12 -c100 -d30s -s post.lua http://localhost:8080 &
pid1=$!
# Benchmark the find operation
echo "Benchmarking find operation..."
wrk -t12 -c100 -d30s -s get.lua http://localhost:8080 &
pid2=$!
# Benchmark the RWMutex find operation
echo "Benchmarking RWMutex find operation..."
wrk -t12 -c100 -d30s -s rwget.lua http://localhost:8080 &
pid3=$!
# Wait for all background processes to finish and capture their exit statuses
wait $pid1
status1=$?
wait $pid2
status2=$?
wait $pid3
status3=$?
# Check the exit statuses and exit with an error if any command failed
if [ $status1 -ne 0 ]; then
echo "Error: Benchmarking insert operation failed"
exit 1
fi
if [ $status2 -ne 0 ]; then
echo "Error: Benchmarking find operation failed"
exit 1
fi
if [ $status3 -ne 0 ]; then
echo "Error: Benchmarking RWMutex find operation failed"
exit 1
fi
echo "Completed successfully"