Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-examples / python / usrp / benchmark_usb.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2004,2005 Free Software Foundation, Inc.
4
5 # This file is part of GNU Radio
6
7 # GNU Radio 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, or (at your option)
10 # any later version.
11
12 # GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
19 # the Free Software Foundation, Inc., 51 Franklin Street,
20 # Boston, MA 02110-1301, USA.
21
22
23 """
24 Benchmark the USB/USRP throughput.  Finds the maximum full-duplex speed
25 the USRP/USB combination can sustain without errors.
26
27 This program does not currently give reliable results.  Sorry about that...
28 """
29
30 from gnuradio import gr
31 from gnuradio import usrp
32 from gnuradio import eng_notation
33
34 import sys
35
36 def run_test (usb_throughput, verbose):
37     # usb_throughput is in bytes/sec.
38     #
39     # Returns True or False
40     
41     nsec = 1
42     stream_length = int (usb_throughput/2 * nsec)   # length of stream to examine
43
44     adc_freq =  64e6
45     dac_freq = 128e6
46     sizeof_sample = 2 * gr.sizeof_short
47
48     usb_throughput_in_samples = usb_throughput / sizeof_sample
49
50     # allocate usb throughput 50/50 between Tx and Rx
51
52     tx_interp = int (dac_freq) / int (usb_throughput_in_samples / 2)
53     rx_decim  = int (adc_freq) / int (usb_throughput_in_samples / 2)
54
55     # print "tx_interp =", tx_interp, "rx_decim =", rx_decim
56     assert (tx_interp == 2 * rx_decim)
57     
58     fg = gr.flow_graph ()
59
60     # Build the Tx pipeline
61     data_src = gr.lfsr_32k_source_s ()
62     src_head = gr.head (gr.sizeof_short, int (stream_length * 2))
63     usrp_tx = usrp.sink_s (0, tx_interp)
64     fg.connect (data_src, src_head, usrp_tx)
65
66     # and the Rx pipeline
67     usrp_rx = usrp.source_s (0, rx_decim, 1, 0x32103210, usrp.FPGA_MODE_LOOPBACK)
68     head = gr.head (gr.sizeof_short, stream_length)
69     check = gr.check_lfsr_32k_s ()
70     fg.connect (usrp_rx, head, check)
71
72     fg.run ()
73
74     ntotal = check.ntotal ()
75     nright = check.nright ()
76     runlength = check.runlength ()
77
78     if verbose:
79         print "usb_throughput =", eng_notation.num_to_str (usb_throughput)
80         print "ntotal    =", ntotal
81         print "nright    =", nright
82         print "runlength =", runlength
83         print "delta     =", ntotal - runlength
84
85     return runlength >= stream_length - 80000
86     
87 def main ():
88     verbose = True
89     best_rate = 0
90     usb_rate = [ 2e6, 4e6, 8e6, 16e6, 32e6 ]
91     #usb_rate = [ 32e6, 32e6, 32e6, 32e6, 32e6 ]
92     # usb_rate.reverse ()
93     for rate in usb_rate:
94         sys.stdout.write ("Testing %sB/sec... " % (eng_notation.num_to_str (rate)))
95         sys.stdout.flush ()
96         ok = run_test (rate, verbose)
97         if ok:
98             best_rate = max (best_rate, rate)
99             sys.stdout.write ("OK\n")
100         else:
101             sys.stdout.write ("FAILED\n")
102
103     print "Max USB/USRP throughput = %sB/sec" % (eng_notation.num_to_str (best_rate),)
104
105 if __name__ == '__main__':
106     main ()