]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/get_performance.rb
fixing some bugs of performance measurment
[avr-crypto-lib.git] / host / get_performance.rb
1 #!/usr/bin/ruby 
2 # get_performance.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
6
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 =end
20
21 $debug = false
22 require 'rubygems'
23 require 'serialport'
24 require 'getopt/std'
25 $conffile_check = Hash.new
26 $conffile_check.default = 0
27
28 ################################################################################
29 # readconfigfile                                                               #
30 ################################################################################
31
32 def readconfigfile(fname, conf)
33   return conf if $conffile_check[fname]==1
34   $conffile_check[fname]=1
35   section = "default"
36   if not File.exists?(fname)
37     return conf
38   end
39   file = File.open(fname, "r")
40   until file.eof
41     line = file.gets()
42   next if /[\s]*#/.match(line)
43   if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
44     section=m[1]
45     conf[m[1]] = Hash.new
46     next
47   end
48   next if not /=/.match(line)
49   m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
50   if m[1]=="include"
51     Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
52   else
53       conf[section][m[1]] = m[2]
54   end
55   end
56   file.close()
57   return conf
58 end
59
60 ################################################################################
61 # read_line                                                                    #
62 ################################################################################
63
64 def read_line(error_msg=true)
65   i=$extended_wait
66   begin
67     s = $sp.gets()
68   end until s or i==0
69   if s==nil
70     puts("ERROR: read timeout!\n") if error_msg
71         return nil
72   end   
73   s.gsub(/\006/, '');   
74 end
75
76 ################################################################################
77 # readPerformanceVector                                                        #
78 ################################################################################
79
80 def readPerformanceVector(param)
81   lb=""
82   buffer=""
83   fname=""
84   fout=0
85   begin
86     lb = read_line()
87     if lb.match(/End of performance figures/)
88       return false
89         end
90         if m=lb.match(/=== (.*) performance ===/) 
91           fout.close if fout!=0
92           fname=$dir+m[1]
93           fname+="."+param if param != ""
94           fname+=".txt"
95           fout = File.open(fname, "w+")
96           printf("> %s \n", fname)      
97           fout.write(lb)
98     else
99           if fout!=0 && lb!=""
100             fout.write(lb)
101           end   
102         end
103   end while true
104 end
105
106 ################################################################################
107 # MAIN                                                                         #
108 ################################################################################
109
110
111 opts = Getopt::Std.getopts("f:c:t:a:d")
112
113 conf = Hash.new
114 conf = readconfigfile("/etc/testport.conf", conf)
115 conf = readconfigfile("~/.testport.conf", conf)
116 conf = readconfigfile("testport.conf", conf)
117 conf = readconfigfile(opts["f"], conf) if opts["f"]
118
119 #puts conf.inspect
120
121 puts("serial port interface version: " + SerialPort::VERSION);
122 $linewidth = 64
123 params = { "baud"       => conf["PORT"]["baud"].to_i,
124             "data_bits" => conf["PORT"]["databits"].to_i,
125             "stop_bits" => conf["PORT"]["stopbits"].to_i,
126             "parity"    => SerialPort::NONE }
127 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
128 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
129 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
130 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
131
132 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
133                 params["baud"].to_s      +
134                 " "                      +
135                 params["data_bits"].to_s +
136                 conf["PORT"]["paraty"][0,1].upcase +
137                 params["stop_bits"].to_s +
138                 "\n")
139
140 $sp = SerialPort.new(conf["PORT"]["port"], params)
141
142 $sp.read_timeout=1000; # 5 minutes
143 $sp.flow_control = SerialPort::SOFT
144 =begin
145 if ARGV.size < 1
146   STDERR.print <<EOF
147   Usage: ruby #{$0} -c command [-t target_dir] [-a additional specifier]
148 EOF
149   exit(1)
150 end
151 =end
152
153 command=opts['c']+"\r";
154 $dir=(opts['t'])?opts['t']:"";
155 param=(opts['a'])?opts['a']:"";
156
157 $linewidth = 16
158 $extended_wait=100;
159 $sp.write(command);
160
161 while(readPerformanceVector(param))
162 end
163
164 exit(0);
165
166