PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/phoronix-test-suite/pts-core/objects/pts_test_run_options.php

#
PHP | 402 lines | 300 code | 56 blank | 46 comment | 49 complexity | e701585651afb8ca2c594b79996745ac MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Phoronix Test Suite
  4. URLs: http://www.phoronix.com, http://www.phoronix-test-suite.com/
  5. Copyright (C) 2010, Phoronix Media
  6. Copyright (C) 2010, Michael Larabel
  7. This program 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 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class pts_test_run_options
  19. {
  20. public static function prompt_user_options(&$test_profile, $preset_selections = null)
  21. {
  22. $user_args = array();
  23. $text_args = array();
  24. // Rather than using AUTO_TEST_OPTION_SELECTIONS, pass it to the $preset_selections argument
  25. if(($cli_presets_env = pts_client::read_env('PRESET_OPTIONS')) != false)
  26. {
  27. // To specify test options externally from an environment variable
  28. // i.e. PRESET_OPTIONS='stream.run-type=Add' ./phoronix-test-suite benchmark stream
  29. $preset_selections = pts_client::parse_value_string_double_identifier($cli_presets_env);
  30. }
  31. $identifier = $test_profile->get_identifier_base_name();
  32. foreach($test_profile->get_test_option_objects() as $i => $o)
  33. {
  34. if($i == 0)
  35. {
  36. pts_client::$display->generic_heading($test_profile->get_title() . ' Test Configuration');
  37. }
  38. $option_identifier = $o->get_identifier();
  39. if($o->option_count() == 0)
  40. {
  41. // User inputs their option as there is nothing to select
  42. if(isset($preset_selections[$identifier][$option_identifier]))
  43. {
  44. $value = $preset_selections[$identifier][$option_identifier];
  45. }
  46. else
  47. {
  48. echo PHP_EOL . $o->get_name() . PHP_EOL;
  49. $value = pts_user_io::prompt_user_input('Enter Value');
  50. }
  51. array_push($text_args, array($o->format_option_display_from_input($value)));
  52. array_push($user_args, array($o->format_option_value_from_input($value)));
  53. }
  54. else
  55. {
  56. // Have the user select the desired option
  57. if(isset($preset_selections[$identifier][$option_identifier]))
  58. {
  59. $bench_choice = $preset_selections[$identifier][$option_identifier];
  60. }
  61. else
  62. {
  63. $option_names = $o->get_all_option_names_with_messages();
  64. if(count($option_names) > 1)
  65. {
  66. echo PHP_EOL . $o->get_name() . ':' . PHP_EOL;
  67. array_push($option_names, 'Test All Options');
  68. }
  69. $bench_choice = pts_user_io::prompt_text_menu('Enter Your Choice', $option_names, true, true);
  70. }
  71. $bench_choice = $o->parse_selection_choice_input($bench_choice);
  72. // Format the selected option(s)
  73. $option_args = array();
  74. $option_args_description = array();
  75. foreach($bench_choice as $c)
  76. {
  77. array_push($option_args, $o->format_option_value_from_select($c));
  78. array_push($option_args_description, $o->format_option_display_from_select($c));
  79. }
  80. array_push($text_args, $option_args_description);
  81. array_push($user_args, $option_args);
  82. }
  83. }
  84. $test_args = array();
  85. $test_args_description = array();
  86. self::compute_all_combinations($test_args, null, $user_args, 0);
  87. self::compute_all_combinations($test_args_description, null, $text_args, 0, ' - ');
  88. return array($test_args, $test_args_description);
  89. }
  90. public static function default_user_options(&$test_profile)
  91. {
  92. // Defaults mode for single test
  93. $all_args_real = array();
  94. $all_args_description = array();
  95. foreach($test_profile->get_test_option_objects() as $o)
  96. {
  97. $option_args = array();
  98. $option_args_description = array();
  99. $default_entry = $o->get_option_default();
  100. if($o->option_count() == 2)
  101. {
  102. foreach(array(0, 1) as $i)
  103. {
  104. array_push($option_args, $o->format_option_value_from_select($i));
  105. array_push($option_args_description, $o->format_option_display_from_select($i));
  106. }
  107. }
  108. else
  109. {
  110. array_push($option_args, $o->format_option_value_from_select($default_entry));
  111. array_push($option_args_description, $o->format_option_display_from_select($default_entry));
  112. }
  113. array_push($all_args_real, $option_args);
  114. array_push($all_args_description, $option_args_description);
  115. }
  116. $test_args = array();
  117. $test_args_description = array();
  118. self::compute_all_combinations($test_args, null, $all_args_real, 0);
  119. self::compute_all_combinations($test_args_description, null, $all_args_description, 0, ' - ');
  120. return array($test_args, $test_args_description);
  121. }
  122. public static function batch_user_options(&$test_profile)
  123. {
  124. // Batch mode for single test
  125. $batch_all_args_real = array();
  126. $batch_all_args_description = array();
  127. foreach($test_profile->get_test_option_objects() as $o)
  128. {
  129. $option_args = array();
  130. $option_args_description = array();
  131. $option_count = $o->option_count();
  132. for($i = 0; $i < $option_count; $i++)
  133. {
  134. array_push($option_args, $o->format_option_value_from_select($i));
  135. array_push($option_args_description, $o->format_option_display_from_select($i));
  136. }
  137. array_push($batch_all_args_real, $option_args);
  138. array_push($batch_all_args_description, $option_args_description);
  139. }
  140. $test_args = array();
  141. $test_args_description = array();
  142. self::compute_all_combinations($test_args, null, $batch_all_args_real, 0);
  143. self::compute_all_combinations($test_args_description, null, $batch_all_args_description, 0, ' - ');
  144. return array($test_args, $test_args_description);
  145. }
  146. protected static function compute_all_combinations(&$return_arr, $current_string, $options, $counter, $delimiter = ' ')
  147. {
  148. // In batch mode, find all possible combinations for test options
  149. if(count($options) <= $counter)
  150. {
  151. array_push($return_arr, trim($current_string));
  152. }
  153. else
  154. {
  155. foreach($options[$counter] as $single_option)
  156. {
  157. $new_current_string = $current_string;
  158. if(!empty($new_current_string))
  159. {
  160. $new_current_string .= $delimiter;
  161. }
  162. $new_current_string .= $single_option;
  163. self::compute_all_combinations($return_arr, $new_current_string, $options, $counter + 1, $delimiter);
  164. }
  165. }
  166. }
  167. public static function auto_process_test_option($test_identifier, $option_identifier, &$option_names, &$option_values, &$option_messages)
  168. {
  169. // Some test items have options that are dynamically built
  170. switch($option_identifier)
  171. {
  172. case 'auto-resolution':
  173. // Base options off available screen resolutions
  174. if(count($option_names) == 1 && count($option_values) == 1)
  175. {
  176. if(pts_flags::is_live_cd())
  177. {
  178. // Just use the stock resolution when operating from a LiveCD
  179. $available_video_modes = array(phodevi::read_property('gpu', 'screen-resolution'));
  180. }
  181. else
  182. {
  183. $available_video_modes = PTS_IS_CLIENT ? phodevi::read_property('gpu', 'available-modes') : null;
  184. }
  185. if(empty($available_video_modes))
  186. {
  187. // Use hard-coded defaults
  188. $available_video_modes = array(array(800, 600), array(1024, 768), array(1280, 1024), array(1280, 960),
  189. array(1400, 1050), array(1600, 900), array(1680, 1050), array(1600, 1200), array(1920, 1080), array(2560, 1600));
  190. }
  191. $format_name = $option_names[0];
  192. $format_value = $option_values[0];
  193. $option_names = array();
  194. $option_values = array();
  195. foreach($available_video_modes as $video_mode)
  196. {
  197. $this_name = str_replace('$VIDEO_WIDTH', $video_mode[0], $format_name);
  198. $this_name = str_replace('$VIDEO_HEIGHT', $video_mode[1], $this_name);
  199. $this_value = str_replace('$VIDEO_WIDTH', $video_mode[0], $format_value);
  200. $this_value = str_replace('$VIDEO_HEIGHT', $video_mode[1], $this_value);
  201. array_push($option_names, $this_name);
  202. array_push($option_values, $this_value);
  203. }
  204. }
  205. break;
  206. case 'auto-disk-partitions':
  207. case 'auto-disk-mount-points':
  208. // Base options off available disk partitions
  209. if(PTS_IS_CLIENT == false)
  210. {
  211. echo 'ERROR: This option is not supported in this configuration.';
  212. return;
  213. }
  214. /*if(phodevi::is_linux())
  215. {
  216. $all_devices = array_merge(pts_file_io::glob('/dev/hd*'), pts_file_io::glob('/dev/sd*'));
  217. }
  218. else if(phodevi::is_bsd())
  219. {
  220. $all_devices = array_merge(pts_file_io::glob('/dev/ad*'), pts_file_io::glob('/dev/ada*'));
  221. }
  222. else
  223. {
  224. $all_devices = array();
  225. }*/
  226. $all_devices = array_merge(pts_file_io::glob('/dev/hd*'), pts_file_io::glob('/dev/sd*'));
  227. foreach($all_devices as &$device)
  228. {
  229. if(!is_numeric(substr($device, -1)))
  230. {
  231. unset($device);
  232. }
  233. }
  234. $option_values = array();
  235. foreach($all_devices as $partition)
  236. {
  237. array_push($option_values, $partition);
  238. }
  239. if($option_identifier == 'auto-disk-mount-points')
  240. {
  241. $partitions_d = $option_values;
  242. $option_values = array();
  243. $option_names = array();
  244. $mounts = is_file('/proc/mounts') ? file_get_contents('/proc/mounts') : null;
  245. array_push($option_values, '');
  246. array_push($option_names, 'Default Test Directory');
  247. if(pts_flags::is_live_cd() == false)
  248. {
  249. foreach($partitions_d as $partition_d)
  250. {
  251. $mount_point = substr(($a = substr($mounts, strpos($mounts, $partition_d) + strlen($partition_d) + 1)), 0, strpos($a, ' '));
  252. if(is_dir($mount_point) && is_writable($mount_point) && $mount_point != '/boot')
  253. {
  254. array_push($option_values, $mount_point);
  255. array_push($option_names, $mount_point . ' [' . $partition_d . ']');
  256. }
  257. }
  258. }
  259. }
  260. else
  261. {
  262. $option_names = $option_values;
  263. }
  264. break;
  265. case 'auto-disks':
  266. // Base options off attached disks
  267. if(PTS_IS_CLIENT == false)
  268. {
  269. echo 'ERROR: This option is not supported in this configuration.';
  270. return;
  271. }
  272. $all_devices = array_merge(pts_file_io::glob('/dev/hd*'), pts_file_io::glob('/dev/sd*'));
  273. foreach($all_devices as &$device)
  274. {
  275. if(is_numeric(substr($device, -1)))
  276. {
  277. unset($device);
  278. }
  279. }
  280. $option_values = array();
  281. foreach($all_devices as $disk)
  282. {
  283. array_push($option_values, $disk);
  284. }
  285. $option_names = $option_values;
  286. break;
  287. case 'auto-removable-media':
  288. if(PTS_IS_CLIENT == false)
  289. {
  290. echo 'ERROR: This option is not supported in this configuration.';
  291. return;
  292. }
  293. foreach(array_merge(pts_file_io::glob('/media/*/'), pts_file_io::glob('/Volumes/*/')) as $media_check)
  294. {
  295. if(is_dir($media_check) && is_writable($media_check)) // add more checks later on
  296. {
  297. array_push($option_names, $media_check);
  298. array_push($option_values, $media_check);
  299. }
  300. }
  301. break;
  302. case 'auto-file-select':
  303. if(PTS_IS_CLIENT == false)
  304. {
  305. echo 'ERROR: This option is not supported in this configuration.';
  306. return;
  307. }
  308. $names = $option_names;
  309. $values = $option_values;
  310. $option_names = array();
  311. $option_values = array();
  312. for($i = 0; $i < count($names) && $i < count($values); $i++)
  313. {
  314. if(is_file($values[$i]))
  315. {
  316. array_push($option_names, $names[$i]);
  317. array_push($option_values, $values[$i]);
  318. }
  319. }
  320. break;
  321. case 'auto-directory-select':
  322. if(PTS_IS_CLIENT == false)
  323. {
  324. echo 'ERROR: This option is not supported in this configuration.';
  325. return;
  326. }
  327. $names = $option_names;
  328. $values = $option_values;
  329. $option_names = array();
  330. $option_values = array();
  331. for($i = 0; $i < count($names) && $i < count($values); $i++)
  332. {
  333. if(is_dir($values[$i]) && is_writable($removable_media[$i]))
  334. {
  335. array_push($option_names, $names[$i]);
  336. array_push($option_values, $values[$i]);
  337. }
  338. }
  339. break;
  340. }
  341. }
  342. }
  343. ?>