README: document nit_env.sh
[nit.git] / benchmarks / bench_plot.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # This shell script generate gnuplot results
17
18 # perl function to compute min/max/avg.
19 # used by bench_command
20 #
21 # $1: file
22 # return: first min max avg label
23 function compute_stats()
24 {
25 file="$1"
26 # Compute statistics
27 perl - "$file" <<'END'
28 @lines = ();
29 $first = undef;
30 chomp($label = <>);
31 while(<>) {
32 chomp;
33 if (/^\d/) {
34 if (defined $first) {
35 push @lines, $_;
36 } else {
37 $first = $_;
38 }
39 }
40 }
41 $len = scalar @lines;
42 if ($len) {
43 @lines = sort {$a <=> $b} @lines;
44 $min = $lines[0];
45 $max = $lines[$#lines];
46 $avg = 0;
47 for $i (@lines) { $avg += $i; }
48 $avg = $avg / $len;
49 print "${first} ${min} ${max} ${avg} \"${label}\"\n";
50 } else {
51 print "${first} ${first} ${first} ${first} \"${label}\"\n";
52 }
53 END
54 }
55
56 # Create a new $res to be used in the next plot
57 #
58 # $1 = resfile
59 # $2 = title
60 # $3 = description
61 function prepare_res()
62 {
63 echo
64 echo "# [$2] $3 #"
65 if test -n "$html"; then
66 echo >>"$html" "<p>[$2] $3 <a href=\"$1\">data</a></p>"
67 fi
68 res=$1
69 if [ "$plots" = "" ]; then
70 plots="plot '$1' using 4:2:3:xticlabels(5) ti '$2'"
71 else
72 plots="$plots, '$1' using 4:2:3 ti '$2'"
73 fi
74 if [ "$dry_run" = "true" ]; then return; fi
75 echo "# [$2] $3 ; count=$count" > "$res"
76 echo "# first min max avg title" >> "$res"
77 }
78
79 # Plot the last $res as an histogram
80 # $1: plot file (eg toto.gnu)
81 function plot()
82 {
83 cat >"$1" <<END
84 set auto x;
85 set yrange [0:];
86 set style data histogram;
87 set style histogram cluster gap 2;
88 set style histogram errorbars linewidth 1;
89 set style fill solid 0.3 border -1;
90 set bars front;
91 set boxwidth 0.9;
92 set xtic nomirror rotate by -45 scale 0 font ',8';
93 set title "$1 ; avg. on $count-1 runs"
94 set ylabel "time (s)"
95 $plots
96 END
97 plots=
98
99 if test -n "$html"; then
100 echo "# gnuplot $1"
101 bn=`basename "$1" .gnu`
102 gnuplot -e "set term png; set output \"$bn.png\"" "$1"
103 echo gnuplot -e "set term png; set output \"$bn.png\"" "$1"
104
105 echo >>"$html" "<img src=\"$bn.png\"/>"
106 fi
107 if test -n "$DISPLAY"; then
108 echo "# gnuplot -p $1"
109 gnuplot -p "$1"
110 fi
111 }
112
113 ## GLOBAL VARIABLES ##
114
115 # The current $res (set by prepare_res, used by bench_command)
116 res=
117
118 # The current stuff to plot (set by prepare_res, used by plot)
119 plots=
120
121 # The name of the html file if output is set to html
122 html=