/** * Create test plan. * * @param string $size size of the test plan. * @param string $proxy proxy url * @param string $port (optional) port to be used for proxy server. * @param bool $enable only enable it. */ public function create_test_plan($size, $proxy = "localhost:9090", $port = '', $enable = false) { global $CFG, $DB; // Check if site is initialised for performance testing. //TODO: // Drop old testplan data dir, ensuring we have fresh data everytime we run the tool. util::drop_dir(util::get_tool_dir(), true); // Initialise BrowserMobProxy. $browsermobproxy = new browsermobproxyclient($proxy); // Use the proxy server url now. $proxyurl = $browsermobproxy->create_connection($port); echo "Proxy server running at: " . $proxyurl . PHP_EOL; // Create behat.yml. util::create_test_feature($size, array(), $proxyurl); util::set_option('size', $size); // Run test plan. if (!$enable) { $cmd = "vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml '; echo "Run " . PHP_EOL . ' - ' . $cmd . PHP_EOL . PHP_EOL; } else { $this->run_plan_features(); } // Close BrowserMobProxy connection. $browsermobproxy->close_connection(); }
/** * Get user input, about what request is of interest, if set in testplan.json then ignore. * * @param string $capturelabel capture label. * @return array request which is saved. */ private function confirm_requests_to_capture($capturelabel) { // Save har data in the temp file. $useridentifiedrequestsdatafile = util::get_har_file_path($capturelabel); $hardata = browsermobproxyclient::get_har($capturelabel); file_put_contents($useridentifiedrequestsdatafile, $hardata); $requests = util::get_info_from_har($capturelabel); // Get which request is being used. $request = $this->is_request_in_config($capturelabel, $requests); // Get query params to be used. if (!empty($request)) { // Update config and return. util::save_final_har_data($capturelabel, $request); return $request; } // This is only possible with interactive terminal, so ensure we have one before proceeding. $posixexists = function_exists('posix_isatty'); // Make sure this step is only used with interactive terminal (if detected). if ($posixexists && !@posix_isatty(STDOUT)) { $msg = "You have some unfilled requests in testplan.json\n" . "Please run following command for interactive terminal to enter value\n" . " vendor/bin/behat --config " . util::get_tool_dir() . DIRECTORY_SEPARATOR . 'behat.yml '; util::performance_exception($msg); } // Else ask user for the actual request to use. fwrite(STDOUT, "\n"); if (count($requests) > 1) { foreach ($requests as $index => $request) { fwrite(STDOUT, $index . ". - " . $request['method'] . " - " . $request['path'] . PHP_EOL); } fwrite(STDOUT, "Which http request should associate with \"{$capturelabel}\": "); $requestindex = trim(fread(STDIN, 1024)); // Ensure correct selection is done. while (!is_number($requestindex) || $requestindex <= 0 || $requestindex > count($requests)) { fwrite(STDOUT, "Wrong request number for step {$capturelabel}, try again: "); $requestindex = trim(fread(STDIN, 1024)); } } else { $requestindex = 0; } fwrite(STDOUT, "\\Enter query values to substitute for \"" . $requests[$requestindex]['path'] . "\"\n "); $requests[$requestindex]['query'] = $this->get_substitute_value_from_user($requests[$requestindex]['query']); // Save final request. util::save_final_har_data($capturelabel, $requests[$requestindex]); return $requests[$requestindex]; }
/** * After suite event. * * @param SuiteEvent $event * @AfterSuite */ public static function after_suite(SuiteEvent $event) { $browsermobproxy = new browsermobproxyclient(util::get_option('proxyurl')); $browsermobproxy->close_connection(); echo PHP_EOL . "Test plan has been generated under:" . PHP_EOL; echo " - " . util::get_final_testplan_path() . PHP_EOL; }