2 # Copyright 2017 Cisco Systems, Inc. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
18 # This is an example of python application controling a nfvbench server
19 # using the nfvbench client API.
20 # The nfvbench server must run in background using the --server option.
21 # Since HTML pages are not required, the path to pass to --server can be any directory on the host.
27 from client import NfvbenchClient
31 # At the CLI, the user can either:
32 # - pass an nfvbench configuration as a string (-c <config>)
33 # - pass an nfvbench configuration as a file name containing the
34 # configuration (-f <config_file_path>)
35 # - or pass a test config (-e <config>) that will be echoed back by the server as is
38 parser = argparse.ArgumentParser()
40 parser.add_argument('-f', '--file', dest='file',
42 help='NFVbench config file to execute (json format)',
43 metavar='<config_file_path>')
44 parser.add_argument('-c', '--config', dest='config',
46 help='NFVbench config to execute (json format)',
48 parser.add_argument('-e', '--echo', dest='echo',
50 help='NFVbench config to echo (json format)',
52 parser.add_argument('-t', '--timeout', dest='timeout',
55 help='time (seconds) to wait for NFVbench result',
57 parser.add_argument('--use-socketio', dest='use_socketio',
59 help='NFVbench config to echo (json format)')
60 parser.add_argument('url', help='nfvbench server url (e.g. http://10.0.0.1:5000)')
61 opts = parser.parse_args()
63 if not opts.file and not opts.config and not opts.echo:
64 print('at least one of -f or -c or -e required')
67 nfvbench = NfvbenchClient(opts.url, opts.use_socketio)
68 # convert JSON into a dict
70 timeout = int(opts.timeout)
72 with open(opts.file) as fd:
73 config = json.loads(fd.read())
74 result = nfvbench.run_config(config, timeout=timeout)
76 config = json.loads(opts.config)
77 result = nfvbench.run_config(config, timeout=timeout)
79 config = json.loads(opts.echo)
80 result = nfvbench.echo_config(config, timeout=timeout)
81 print('Result:', result)
82 except ValueError as ex:
83 print('Input configuration is invalid: ' + str(ex))
87 if __name__ == "__main__":