Jens Geyer | bd52f1a | 2014-07-28 01:25:30 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package; |
| 21 | |
| 22 | import org.apache.thrift.*; |
| 23 | import org.apache.thrift.protocol.*; |
| 24 | import org.apache.thrift.transport.*; |
| 25 | import org.apache.thrift.server.*; |
| 26 | import org.apache.thrift.meta_data.*; |
| 27 | |
| 28 | using StringTools; |
| 29 | |
| 30 | |
| 31 | enum Prot { |
| 32 | binary; |
| 33 | json; |
| 34 | } |
| 35 | |
| 36 | enum Trns { |
| 37 | socket; |
| 38 | http; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | class Arguments |
| 43 | { |
| 44 | public var server(default,null) : Bool = false; |
| 45 | public var framed(default,null) : Bool = false; |
| 46 | public var buffered(default,null) : Bool = false; |
| 47 | public var protocol(default,null) : Prot = binary; |
| 48 | public var transport(default,null) : Trns = socket; |
| 49 | |
| 50 | public var host(default,null) : String = "localhost"; |
| 51 | public var port(default,null) : Int = 9090; |
| 52 | |
| 53 | public var numIterations(default,null) : Int = 1; |
| 54 | public var numThreads(default,null) : Int = 1; |
| 55 | |
| 56 | |
| 57 | public function new() { |
| 58 | #if sys |
| 59 | try { |
| 60 | ParseArgs(); |
| 61 | } catch (e : String) { |
| 62 | trace(GetHelp()); |
| 63 | throw e; |
| 64 | } |
| 65 | #else |
| 66 | trace("WN: Platform does not support program arguments, using defaults."); |
| 67 | #end |
| 68 | } |
| 69 | |
| 70 | #if sys |
| 71 | |
| 72 | private static function GetHelp() : String { |
| 73 | return "\n" |
| 74 | +Sys.executablePath()+" [client|server] [options]\n" |
| 75 | +"Modus: Either client or server, the default is client.\n" |
| 76 | +"\n" |
| 77 | +"Options:\n" |
| 78 | +" -f, --framed framed transport (supersedes buffered)\n" |
| 79 | +" -b, --buffered buffered transport\n" |
| 80 | +" --json JSON protocol\n" |
| 81 | +" --protocol=<prot> Choose protocol: json, binary (default binary).\n" |
| 82 | +" --port=<port> Port number for socket transport, default 9090\n" |
| 83 | +"\n" |
| 84 | +"Client only options:\n" |
| 85 | +" --host=<host> Host name, IP or URL, default localhost\n" |
| 86 | +" -n=<iterations> Number of test iterations\n" |
| 87 | +" -t=<threads> Number of test threads\n" |
| 88 | +" -u=<url> Target Host/URL (same as --host)\n" |
| 89 | +"\n" |
| 90 | +"All arguments are optional.\n"; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | private function ParseArgs() : Void { |
| 95 | var step = 0; |
| 96 | for (arg in Sys.args()) { |
| 97 | |
| 98 | // server|client |
| 99 | switch(step) { |
| 100 | case 0: |
| 101 | ++step; |
| 102 | if ( arg == "client") |
| 103 | server = false; |
| 104 | else if ( arg == "server") |
| 105 | server = true; |
| 106 | else |
| 107 | throw "First argument must be 'server' or 'client'"; |
| 108 | |
| 109 | case 1: |
| 110 | if ( (arg == "-f") || (arg == "--framed")) { |
| 111 | framed = true; |
| 112 | } else if (( arg == "-b") || ( arg == "--buffered")) { |
| 113 | buffered = true; |
| 114 | } else if (( arg == "--json") || (arg == "--protocol=json")){ |
| 115 | protocol = json; |
| 116 | } else if (( arg == "--protocol=binary")){ |
| 117 | protocol = binary; |
| 118 | } else if (arg.startsWith("--host=")) { |
| 119 | ClientOnlyOption(arg); |
| 120 | host = arg.substr(arg.indexOf("=") + 1); |
| 121 | } else if (arg.startsWith("--port=")) { |
| 122 | var tmp = Std.parseInt(arg.substr(arg.indexOf("=")+1)); |
| 123 | if( tmp != null) |
| 124 | port = tmp; |
| 125 | else |
| 126 | throw "Invalid port number "+arg; |
| 127 | } else if (arg == "-n") { |
| 128 | ClientOnlyOption(arg); |
| 129 | step = 2; |
| 130 | } else if (arg == "-t") { |
| 131 | ClientOnlyOption(arg); |
| 132 | step = 3; |
| 133 | } else if (arg == "-u") { |
| 134 | ClientOnlyOption(arg); |
| 135 | step = 4; |
| 136 | } else { |
| 137 | throw "Unexpected argument "+arg; |
| 138 | } |
| 139 | |
| 140 | case 2: // num iterations |
| 141 | step = 1; |
| 142 | var tmp = Std.parseInt(arg); |
| 143 | if( tmp != null) |
| 144 | numIterations = tmp; |
| 145 | else |
| 146 | throw "Invalid numeric value "+arg; |
| 147 | |
| 148 | case 3: // num threads |
| 149 | step = 1; |
| 150 | var tmp = Std.parseInt(arg); |
| 151 | if( tmp != null) |
| 152 | numThreads = tmp; |
| 153 | else |
| 154 | throw "Invalid numeric value "+arg; |
| 155 | |
| 156 | case 4: // url |
| 157 | step = 1; |
| 158 | host = arg; |
| 159 | |
| 160 | default: |
| 161 | throw "Unexpected state"; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | if ( framed && buffered) |
| 166 | { |
| 167 | trace("WN: framed supersedes buffered transport"); |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | #end |
| 174 | |
| 175 | |
| 176 | private function ClientOnlyOption( arg : String) { |
| 177 | if( server) { |
| 178 | throw "Unexpected argument in client mode: "+arg; |
| 179 | } |
| 180 | } |
| 181 | } |