public static function __post_test_run()
 {
     pts_module::remove_file('is_running');
     $screenshots = self::get_screenshots();
     foreach ($screenshots as $screenshot) {
         // Compress the PNGs a bit
         pts_image::compress_png_image($screenshot, 9);
     }
     return $screenshots;
 }
 protected static function parse_iqc_result(&$test_profile, $parse_xml_file, $log_file, $pts_test_arguments, $extra_arguments)
 {
     $results_parser_xml = new pts_parse_results_nye_XmlReader($parse_xml_file);
     $result_match_test_arguments = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/MatchToTestArguments');
     $result_iqc_source_file = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/SourceImage');
     $result_iqc_image_x = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/ImageX');
     $result_iqc_image_y = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/ImageY');
     $result_iqc_image_width = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/ImageWidth');
     $result_iqc_image_height = $results_parser_xml->getXMLArrayValues('PhoronixTestSuite/ImageParser/ImageHeight');
     $test_result = false;
     if (!extension_loaded('gd')) {
         // Needs GD library to work
         return false;
     }
     for ($i = 0; $i < count($result_iqc_source_file); $i++) {
         if (!empty($result_match_test_arguments[$i]) && strpos($pts_test_arguments, $result_match_test_arguments[$i]) === false) {
             // This is not the ResultsParser XML section to use as the MatchToTestArguments does not match the PTS test arguments
             continue;
         }
         if (is_file($test_profile->get_install_dir() . $result_iqc_source_file[$i])) {
             $iqc_source_file = $test_profile->get_install_dir() . $result_iqc_source_file[$i];
         } else {
             // No image file found
             continue;
         }
         $img = pts_image::image_file_to_gd($iqc_source_file);
         if ($img == false) {
             return;
         }
         $img_sliced = imagecreatetruecolor($result_iqc_image_width[$i], $result_iqc_image_height[$i]);
         imagecopyresampled($img_sliced, $img, 0, 0, $result_iqc_image_x[$i], $result_iqc_image_y[$i], $result_iqc_image_width[$i], $result_iqc_image_height[$i], $result_iqc_image_width[$i], $result_iqc_image_height[$i]);
         $test_result = $test_profile->get_install_dir() . 'iqc.png';
         imagepng($img_sliced, $test_result);
         if ($test_result != false) {
             break;
         }
     }
     return $test_result;
 }
 public static function color_pixel_delta(&$base_img, &$compare_img, &$x, &$y)
 {
     $color = false;
     // for now we set to true when it should change, but ultimately should return a color
     $check_points = array(array($x + 1, $y), array($x - 1, $y), array($x, $y + 1), array($x, $y - 1));
     foreach ($check_points as $point_r) {
         if (pts_image::rgb_int_diff(pts_image::rgb_gd_color_at($base_img, $point_r[0], $point_r[1]), pts_image::rgb_gd_color_at($compare_img, $point_r[0], $point_r[1])) > 12) {
             $color = array($point_r[0], $point_r[1]);
             break;
         }
     }
     return $color;
 }
 public static function run($r)
 {
     if (pts_openbenchmarking_client::user_name() == false) {
         echo PHP_EOL . 'You must first be logged into an OpenBenchmarking.org account.' . PHP_EOL;
         echo PHP_EOL . 'Create An Account: http://openbenchmarking.org/';
         echo PHP_EOL . 'Log-In Command: phoronix-test-suite openbenchmarking-setup' . PHP_EOL . PHP_EOL;
         return false;
     }
     ini_set('memory_limit', '2048M');
     foreach (pts_types::identifiers_to_test_profile_objects($r, false, true) as $test_profile) {
         $qualified_identifier = $test_profile->get_identifier();
         // First make sure the test profile is already in the OpenBenchmarking.org database...
         $json = pts_openbenchmarking::make_openbenchmarking_request('is_test_profile', array('i' => $qualified_identifier));
         $json = json_decode($json, true);
         if (!isset($json['openbenchmarking']['test']['valid']) || $json['openbenchmarking']['test']['valid'] != 'TRUE') {
             echo PHP_EOL . $qualified_identifier . ' must first be uploaded to OpenBenchmarking.org.' . PHP_EOL;
             //	break;
         }
         // Set some other things...
         pts_client::pts_set_environment_variable('FORCE_TIMES_TO_RUN', 1);
         pts_client::pts_set_environment_variable('TEST_RESULTS_NAME', $test_profile->get_title() . ' Testing ' . date('Y-m-d'));
         pts_client::pts_set_environment_variable('TEST_RESULTS_IDENTIFIER', 'Sample Run');
         pts_client::pts_set_environment_variable('TEST_RESULTS_DESCRIPTION', 1);
         pts_openbenchmarking_client::override_client_setting('AutoUploadResults', true);
         pts_openbenchmarking_client::override_client_setting('UploadSystemLogsByDefault', true);
         // Take screenshots
         pts_client::pts_set_environment_variable('SCREENSHOT_INTERVAL', 9);
         pts_module_manager::attach_module('timed_screenshot');
         $force_ss = true;
         $reference_ss_file = pts_module_manager::module_call('timed_screenshot', 'take_screenshot', $force_ss);
         sleep(2);
         $apitrace = pts_file_io::glob('/usr/local/lib/*/apitrace/wrappers/glxtrace.so');
         if (!empty($apitrace) && pts_client::executable_in_path('apitrace')) {
             $apitrace = array_shift($apitrace);
             putenv('LD_PRELOAD=' . $apitrace);
         } else {
             $apitrace = false;
         }
         // So for any compiling tasks they will try to use the most aggressive instructions possible
         putenv('CFLAGS=-march=native -O3');
         putenv('CXXFLAGS=-march=native -O3');
         pts_test_installer::standard_install($qualified_identifier, true);
         $run_manager = new pts_test_run_manager(false, 2);
         $run_manager->standard_run($qualified_identifier);
         if ($apitrace) {
             putenv('LD_PRELOAD=');
         }
         if ($reference_ss_file) {
             $reference_ss = pts_image::image_file_to_gd($reference_ss_file);
             unlink($reference_ss_file);
             $screenshots_gd = array();
             $screenshots = pts_module_manager::module_call('timed_screenshot', 'get_screenshots');
             var_dump($screenshots);
             foreach ($screenshots as $ss_file) {
                 $screenshots_gd[$ss_file] = pts_image::image_file_to_gd($ss_file);
                 if ($screenshots_gd[$ss_file] == false) {
                     continue;
                 }
                 $ss_delta = pts_image::gd_image_delta_composite($reference_ss, $screenshots_gd[$ss_file], true);
                 if (count($ss_delta) < floor(imagesx($reference_ss) * 0.5600000000000001) || filesize($ss_file) > 2097152) {
                     // If less than 56% of the pixels are changing on X, then likely not much to show off... (CLI only likely)
                     // Or if filesize of image is beyond 2MB
                     //echo 'dropping' . $ss_file . PHP_EOL;
                     unset($screenshots_gd[$ss_file]);
                     pts_file_io::unlink($ss_file);
                 }
             }
             $ss_files = array_keys($screenshots_gd);
             shuffle($ss_files);
             // Don't upload more than 4MB worth of screenshots
             while (pts_file_io::array_filesize($ss_files) > 1048576 * 2) {
                 $f = array_pop($ss_files);
                 unlink($f);
             }
             if (count($ss_files) > 0) {
                 $c = 1;
                 foreach ($ss_files as $i => $file) {
                     $new_file = dirname($file) . '/screenshot-' . $c . '.png';
                     rename($file, $new_file);
                     $ss_files[$i] = $new_file;
                     $c++;
                 }
                 $ss_zip_file = PTS_OPENBENCHMARKING_SCRATCH_PATH . 'screenshots-' . $test_profile->get_identifier_base_name() . '-' . $test_profile->get_test_profile_version() . '.zip';
                 $zip_created = pts_compression::zip_archive_create($ss_zip_file, $ss_files);
                 if ($zip_created) {
                     echo count($ss_files) . ' screenshots captured for use.';
                     //'tp_sha1' => sha1_file($zip_file),
                     //'tp_zip' => base64_encode(file_get_contents($zip_file)),
                 }
                 foreach ($ss_files as $file) {
                     //	pts_file_io::unlink($file);
                 }
             }
         }
         $test_binary = self::locate_test_profile_lead_binary($test_profile);
         $shared_library_dependencies = array();
         $instruction_usage = array();
         $gl_calls = null;
         if (is_executable($test_binary)) {
             if ($apitrace) {
                 // Find the trace...
                 $test_binary_dir = dirname($test_binary);
                 $trace_file = glob($test_binary_dir . '/*.trace');
                 if ($trace_file) {
                     echo 'Analyzing GL traces';
                     $trace_file = array_shift($trace_file);
                     $gl_usage = self::analyze_apitrace_trace_glpop($trace_file);
                     if (!empty($gl_usage)) {
                         $gl_calls = implode(',', $gl_usage);
                     }
                 }
             }
             $ldd = trim(shell_exec('ldd ' . $test_binary));
             foreach (explode(PHP_EOL, $ldd) as $line) {
                 $line = explode(' => ', $line);
                 if (count($line) == 2) {
                     $shared_library_dependencies[] = trim(basename($line[0]));
                 }
             }
             echo PHP_EOL . 'SHARED LIBRARY DEPENDENCIES: ' . PHP_EOL;
             print_r($shared_library_dependencies);
             foreach (array('core-avx-i', 'bdver2') as $march) {
                 // So for any compiling tasks they will try to use the most aggressive instructions possible
                 putenv('CFLAGS=-march=' . $march . ' -O3');
                 putenv('CXXFLAGS=-march=' . $march . ' -O3');
                 pts_test_installer::standard_install($qualified_identifier, true);
                 $instruction_usage[$march] = self::analyze_binary_instruction_usage($test_binary);
                 if ($instruction_usage[$march] == null) {
                     unset($instruction_usage[$march]);
                 }
             }
             if (!empty($instruction_usage) && count(array_unique($instruction_usage)) == 1) {
                 $generic = array_pop($instruction_usage);
                 $instruction_usage = array('generic' => $generic);
             }
             var_dump($instruction_usage);
         } else {
             echo PHP_EOL . $test_binary;
             echo PHP_EOL . 'Test binary could not be found.' . PHP_EOL;
             //		return false;
         }
     }
     sleep(10);
     var_dump($shared_library_dependencies);
     var_dump($instruction_usage);
     var_dump($gl_calls);
     $server_response = pts_openbenchmarking::make_openbenchmarking_request('upload_test_meta', array('i' => $test_profile->get_identifier(), 'screenshots_zip' => $ss_zip_conts = base64_encode(file_get_contents($ss_zip_file)), 'screenshots_zip_sha1' => sha1($ss_zip_conts), 'ldd_libraries' => implode(',', $shared_library_dependencies), 'opengl_calls' => $gl_calls, 'instruction_set_usage' => base64_encode(json_encode($instruction_usage))));
     var_dump($server_response);
     $json = json_decode($server_response, true);
     pts_file_io::unlink($ss_zip_file);
 }
 public static function run($args)
 {
     $result = $args[0];
     $result_file = new pts_result_file($result);
     $result_file_identifiers = $result_file->get_system_identifiers();
     if (count($result_file_identifiers) < 2) {
         echo PHP_EOL . 'There are not multiple test runs in this result file.' . PHP_EOL;
         return false;
     }
     $base_identifier = pts_user_io::prompt_text_menu('Select the base test run', $result_file_identifiers);
     $base_select = new pts_result_merge_select($result, $base_identifier);
     $compare_identifier = pts_user_io::prompt_text_menu('Select the test run to compare', $result_file_identifiers);
     $compare_select = new pts_result_merge_select($result, $compare_identifier);
     do {
         $extract_to = 'iqc-analyze-' . rand(100, 999);
     } while (is_dir(PTS_SAVE_RESULTS_PATH . $extract_to));
     $extract_result = pts_merge::merge_test_results($base_select, $compare_select);
     pts_client::save_test_result($extract_to . '/composite.xml', $extract_result);
     $compare_file = new pts_result_file($extract_to);
     $result_file_writer = new pts_result_file_writer('Image Delta');
     foreach ($compare_file->get_result_objects() as $result_object) {
         if ($result_object->test_profile->get_display_format() != 'IMAGE_COMPARISON') {
             continue;
         }
         $base_result = null;
         $compare_result = null;
         foreach ($result_object->test_result_buffer->get_buffer_items() as $buffer_item) {
             if ($buffer_item->get_result_identifier() == $base_identifier && $base_result == null) {
                 $base_result = $buffer_item->get_result_value();
             } else {
                 if ($buffer_item->get_result_identifier() == $compare_identifier && $compare_result == null) {
                     $compare_result = $buffer_item->get_result_value();
                 }
             }
             if ($compare_result != null && $base_result != null) {
                 break;
             }
         }
         if ($compare_result == null || $base_result == null) {
             continue;
         }
         $base_img = imagecreatefromstring(base64_decode($base_result));
         $compare_img = imagecreatefromstring(base64_decode($compare_result));
         $delta_img = imagecreatefromstring(base64_decode($compare_result));
         $img_width = imagesx($base_img);
         $img_height = imagesy($base_img);
         $img_changed = false;
         for ($x = 0; $x < $img_width; $x++) {
             for ($y = 0; $y < $img_height; $y++) {
                 $base_image_color = pts_image::rgb_gd_color_at($base_img, $x, $y);
                 $compare_image_color = pts_image::rgb_gd_color_at($compare_img, $x, $y);
                 if ($base_image_color == $compare_image_color || pts_image::rgb_int_diff($base_image_color, $compare_image_color) < 9) {
                     if ($cords = pts_image::color_pixel_delta($base_img, $compare_img, $x, $y)) {
                         $pixel_rgb = pts_image::rgb_gd_color_at($delta_img, $cords[0], $cords[1]);
                         $color_invert = imagecolorresolve($delta_img, 255 - $pixel_rgb[0], 255 - $pixel_rgb[1], 255 - $pixel_rgb[2]);
                         imagesetpixel($delta_img, $x, $y, $color_invert);
                         $img_changed = true;
                     }
                 }
             }
         }
         if ($img_changed) {
             imagepng($delta_img, PTS_SAVE_RESULTS_PATH . $extract_to . '/scratch.png');
             $result_value = base64_encode(file_get_contents(PTS_SAVE_RESULTS_PATH . $extract_to . '/scratch.png', FILE_BINARY));
             pts_file_io::unlink(PTS_SAVE_RESULTS_PATH . $extract_to . '/scratch.png');
             $result_file_writer->add_result_from_result_object_with_value_string($result_object, $result_value);
         }
     }
     pts_client::save_result_file($result_file_writer, $extract_to);
     pts_client::display_web_page(PTS_SAVE_RESULTS_PATH . $extract_to . '/composite.xml');
 }