static TransportProvider getProvider() { String providerStr = System.getProperty("fhb.http.provider", "apache3").toUpperCase(); for (TransportProvider provider : TransportProvider.values()) { if (provider.name().equals(providerStr)) return provider; } // Default is APACHE3 return APACHE3; }
private String createDefinition(Object runConfigNode) throws Exception { Element benchDefNode = (Element) xp.evaluate("fd:benchmarkDefinition", runConfigNode, XPathConstants.NODE); if (benchDefNode == null) { return null; } String definingClassName = xp.evaluate("fd:driverConfig/@name", runConfigNode); // Get the cycleTime annotation Element driverConfigNode = (Element) xp.evaluate("fd:driverConfig", runConfigNode, XPathConstants.NODE); String requestLagTime = getRequestLagTime(driverConfigNode); /** * Load the template from file Template file is small, but is there a better way to read this? */ String line = null; BufferedReader br = null; InputStream is = null; try { ClassLoader cl = this.getClass().getClassLoader(); is = cl.getResourceAsStream("driver_template"); br = new BufferedReader(new InputStreamReader(is)); } catch (Exception ex) { // what to do? ex.printStackTrace(); throw new ConfigurationException(ex); } StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { if (!line.equals("\n")) { sb.append(line).append("\n"); } } String template = sb.toString(); is.close(); // Obtain the provider. TransportProvider provider = TransportProvider.getProvider(); /** * Get the operation token out of the template. Use this to create multiple * operations/functions that will replace the token in the java file */ String opTemplate = template.substring((template.indexOf("#operation") + 10), template.indexOf("operation#")); StringBuilder operations = new StringBuilder(); Element operationNode = null; int i = 1; while ((operationNode = (Element) xp.evaluate( ("fd:driverConfig/fd:operation[" + i + "]"), runConfigNode, XPathConstants.NODE)) != null) { /* * There are many different options here. First, the operation * is either POST or GET. In either case, the subst element * indicates whether or not random strings are present in the * payload data (for backward compatibility, it is assumed * that random data is present). * * For POST, we can read the data from a file, and we can * encode the data as form-encoded or binary (octet-stream) */ boolean isPost = false; boolean isBinary = false; boolean isFile = false; boolean doSubst = true; String requestLagTimeOverride = getRequestLagTime(operationNode); String operationName = xp.evaluate("fd:name", operationNode); String url = xp.evaluate("fd:url", operationNode); String max90th = xp.evaluate("fd:max90th", operationNode); String kbps = xp.evaluate("fd:kbps", operationNode); String accept = xp.evaluate("fd:accept", operationNode); String requestString = ""; Element requestNode = (Element) xp.evaluate("fd:get", operationNode, XPathConstants.NODE); if (requestNode == null) { // Can't have both post & get either, but if both are there, // will assume you meant GET. requestNode = (Element) xp.evaluate("fd:post", operationNode, XPathConstants.NODE); if (requestNode != null) { isPost = true; if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "file"))) { isFile = true; } if ("true".equalsIgnoreCase(requestNode.getAttributeNS(null, "binary"))) { isBinary = true; } } else { throw new ConfigurationException("<operation> " + "must have a either a get/post"); } // Can't have both post & get either, but if there, // will assume you meant GET. } if ("false".equalsIgnoreCase(requestNode.getAttributeNS(null, "subst"))) { doSubst = false; } if (isBinary && doSubst) { throw new ConfigurationException( "<operation> " + "cannot be both binary and perform substitution"); } requestString = requestNode.getNodeValue(); if (requestString == null) { CDATASection cDataNode = (CDATASection) requestNode.getFirstChild(); if (cDataNode != null) { requestString = cDataNode.getNodeValue(); } } if (requestString == null) { requestString = ""; } if (requestLagTimeOverride == null) { requestLagTimeOverride = ""; } if (operationName == null) { throw new ConfigurationException("<operation> must have a <name> "); } if (url == null) { throw new ConfigurationException("<operation> must have a <url>"); } RunInfoDefinition rid; if (isPost) { if (isFile) { rid = new RunInfoPostFileDefinition(); } else { rid = new RunInfoPostDataDefinition(); } } else { rid = new RunInfoGetDefinition(); } rid.init(doSubst, isBinary, url, requestString, kbps, accept); // Create the benchmark Operation annotation StringBuilder bmop = new StringBuilder("@BenchmarkOperation(name = \"").append(operationName); bmop.append("\", percentileLimits={ ") .append(max90th) .append(", ") .append(max90th) .append(", ") .append(max90th) .append("}, "); if (provider == TransportProvider.SUN && url.startsWith("https")) bmop.append("timing = com.sun.faban.driver.Timing.MANUAL"); else bmop.append("timing = com.sun.faban.driver.Timing.AUTO"); bmop.append(")"); String opTemplateClone = new String(opTemplate); // replace tokens with actual content opTemplateClone = opTemplateClone.replaceAll("@RequestLagTime@", requestLagTimeOverride); opTemplateClone = opTemplateClone.replaceAll("@Operations@", bmop.toString()); opTemplateClone = opTemplateClone.replaceAll("@operationName@", operationName); opTemplateClone = opTemplateClone.replaceAll("@url@", rid.getURL(i)); opTemplateClone = opTemplateClone.replaceAll("@postRequest@", rid.getPostRequest(i)); opTemplateClone = opTemplateClone.replaceAll("@Statics@", rid.getStatics(i)); opTemplateClone = opTemplateClone.replaceAll("@doKbps@", rid.getKbps(i)); opTemplateClone = opTemplateClone.replaceAll("@doHeaders@", rid.getHeaders(i)); opTemplateClone = opTemplateClone.replaceAll("@doTiming@", rid.doTiming(i, provider)); operations.append(opTemplateClone); i++; } String benchmarkDef = getBenchmarkDefinition(benchDefNode); // replace tokens in template template = template.replaceFirst("#operation(.*\\n*)*operation#", operations.toString()); template = template.replaceFirst("@RequestLagTime@", requestLagTime); template = template.replaceFirst("@BenchmarkDefinition@", benchmarkDef); template = template.replaceAll("@DriverClassName@", definingClassName); template = template.replaceFirst("@ProviderClass@", provider.providerClass); template = template.replaceFirst("@BenchmarkDriverName@", definingClassName); String tmpDir = System.getProperty("faban.tmpdir"); if (tmpDir == null) { tmpDir = System.getProperty("java.io.tmpdir"); } if (!tmpDir.endsWith(File.separator)) { tmpDir = tmpDir + File.separator; } String className = new StringBuilder(tmpDir).append(definingClassName).append(".java").toString(); try { // convert class name to filename? PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(className))); pw.print(template); pw.flush(); } catch (Exception ex) { ex.printStackTrace(); throw new ConfigurationException(ex); } String classpath = System.getProperty("java.class.path"); String arg[] = new String[] {"-classpath", classpath, className}; int errorCode = com.sun.tools.javac.Main.compile(arg); if (errorCode != 0) { throw new ConfigurationException( "unable to compile generated driver file. " + "check output for errors"); } return definingClassName; }