src: move debug agent from deps/ to src/
[platform/upstream/nodejs.git] / benchmark / http-flamegraph.sh
1 #!/bin/bash
2 cd "$(dirname "$(dirname $0)")"
3
4 node=${NODE:-./node}
5
6 name=${NAME:-stacks}
7
8 if type sysctl &>/dev/null; then
9   # darwin and linux
10   sudo sysctl -w net.inet.ip.portrange.first=12000
11   sudo sysctl -w net.inet.tcp.msl=1000
12   sudo sysctl -w kern.maxfiles=1000000 kern.maxfilesperproc=1000000
13 elif type /usr/sbin/ndd &>/dev/null; then
14   # sunos
15   /usr/sbin/ndd -set /dev/tcp tcp_smallest_anon_port 12000
16   /usr/sbin/ndd -set /dev/tcp tcp_largest_anon_port 65535
17   /usr/sbin/ndd -set /dev/tcp tcp_max_buf 2097152
18   /usr/sbin/ndd -set /dev/tcp tcp_xmit_hiwat 1048576
19   /usr/sbin/ndd -set /dev/tcp tcp_recv_hiwat 1048576
20 fi
21
22 ulimit -n 100000
23 $node benchmark/http_simple.js &
24 nodepid=$!
25 echo "node pid = $nodepid"
26 sleep 1
27
28 # has to stay alive until dtrace exits
29 dtrace -n 'profile-97/pid == '$nodepid' && arg1/{ @[jstack(150, 8000)] = count(); } tick-60s { exit(0); }' \
30   | grep -v _ZN2v88internalL21Builtin_HandleApiCallENS0_12_GLOBAL__N_116BuiltinA \
31   > "$name".src &
32
33 dtracepid=$!
34
35 echo "dtrace pid = $dtracepid"
36
37 sleep 1
38
39 test () {
40   c=$1
41   t=$2
42   l=$3
43   k=$4
44   ab $k -t 10 -c $c http://127.0.0.1:8000/$t/$l \
45     2>&1 | grep Req
46 }
47
48 #test 100 bytes 1024
49 #test 10  bytes 100 -k
50 #test 100 bytes 1024 -k
51 #test 100 bytes 1024 -k
52 #test 100 bytes 1024 -k
53
54 echo 'Keep going until dtrace stops listening...'
55 while pargs $dtracepid &>/dev/null; do
56   test 100 bytes ${LENGTH:-1} -k
57 done
58
59 kill $nodepid
60
61 echo 'Turn the stacks into a svg'
62 stackvis dtrace flamegraph-svg < "$name".src > "$name".raw.svg
63
64 echo 'Prune tiny stacks out of the graph'
65 node -e '
66 var infile = process.argv[1];
67 var outfile = process.argv[2];
68 var output = "";
69 var fs = require("fs");
70 var input = fs.readFileSync(infile, "utf8");
71
72 input = input.split("id=\"details\" > </text>");
73 var head = input.shift() + "id=\"details\" > </text>";
74 input = input.join("id=\"details\" > </text>");
75
76 var tail = "</svg>";
77 input = input.split("</svg>")[0];
78
79 var minyKept = Infinity;
80 var minyOverall = Infinity;
81 var rects = input.trim().split(/\n/).filter(function(rect) {
82   var my = rect.match(/y="([0-9\.]+)"/);
83
84   if (!my)
85     return false;
86   var y = +my[1];
87   if (!y)
88     return false;
89   minyOverall = Math.min(minyOverall, y);
90
91   // pluck off everything that will be less than one pixel.
92   var mw = rect.match(/width="([0-9\.]+)"/)
93   if (mw) {
94     var width = +mw[1];
95     if (!(width >= 1))
96       return false;
97   }
98   minyKept = Math.min(minyKept, y);
99   return true;
100 });
101
102 // move everything up to the top of the page.
103 var ydiff = minyKept - minyOverall;
104 rects = rects.map(function(rect) {
105   var my = rect.match(/y="([0-9\.]+)"/);
106   var y = +my[1];
107   var newy = y - ydiff;
108   rect = rect.replace(/y="([0-9\.]+)"/, "y=\"" + newy + "\"");
109   return rect;
110 });
111
112 fs.writeFileSync(outfile, head + "\n" + rects.join("\n") + "\n" + tail);
113 ' "$name".raw.svg "$name".svg
114
115 echo ''
116 echo 'done. Results in '"$name"'.svg'