def testReduceOnly(self): with os_util.TempDir() as temp_dir: FLAGS.local_output_dir = temp_dir # Run a normal batch sim to generate local worker outputs. with testing_fakes.PatchAllFakes(binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=GoodWorker, client_class=TestSimClient): client = TestSimClient(sim_name=self.SIM_NAME, num_workers=self.NUM_WORKERS) client.Run() self.assertAlmostEqual(10 * _SIM_TIME, client.total_sim_time) # Run a reduce-only client with patches that will make it crash horribly # if it does anything other than reducing. FLAGS.reduce_only = True with testing_fakes.PatchAllFakes(binary_map=None, binaries=None, worker_factory=None, client_class=TestSimClient): reducer_client = TestSimClient(sim_name=self.SIM_NAME, num_workers=self.NUM_WORKERS) reducer_client.Run() self.assertAlmostEqual(10 * _SIM_TIME, client.total_sim_time)
def testNonzeroSimReturnCode(self): class FakeBinaries(testing_fakes.FakeBinaries): _NUM_SAMPLES = 100 def Controller(self, args): with open(self._ParseArg(args, '--all_params'), 'r') as config_file: config = json.load(config_file) self._iec_case = config['sim']['iec_sim']['load_case'] # Simulate being terminated with SIGINT. return -2 def GroundEstimator(self, args): with open(self._ParseArg(args, '--all_params'), 'r') as config_file: config = json.load(config_file) self._iec_case = config['sim']['iec_sim']['load_case'] # Simulate being terminated with SIGINT. return -2 def Simulator(self, args): if (self._iec_case == sim_types. kIecCaseExtremeCoherentGustWithDirectionChange): return -1 else: return 0 def PcapToHdf5(self, args): log_file_name = self._ParseArg(args, '--output_file') MakeFakeLogFile(log_file_name, self._NUM_SAMPLES) return 0 patch = testing_fakes.PatchAllFakes( binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=batch_sim_worker.IecCasesSimWorker, client_class=batch_sim_client.IecCasesSimClient) with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.cases = ['1.1', '1.3', '1.4b'] gflags.FLAGS.output_dir = temp_dir gflags.FLAGS.local_output_dir = temp_dir client = batch_sim_client.IecCasesSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() filenames = [ os.path.join(temp_dir, '%d.json' % i) for i in range(3) ] for filename in filenames: self.assertTrue(os.path.isfile(filename)) sim_successes = [] for filename in filenames: with open(filename, 'r') as f: config = json.load(f) sim_successes.append(config['sim_successful']) self.assertEqual([True, True, False], sim_successes)
def testClientAndWorker(self): FLAGS.wind_speeds = numpy.linspace(3.0, 15.0, 10) patch = testing_fakes.PatchAllFakes( binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=batch_sim_worker.PowerCurveSimWorker, client_class=batch_sim_client.PowerCurveSimClient) with os_util.TempDir() as temp_dir: with patch, test_util.DisableWarnings(): gflags.FLAGS.output_dir = temp_dir client = batch_sim_client.PowerCurveSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() # Check that a data file containing the power curve was created. data_file = h5py.File(os.path.join(temp_dir, 'data.h5')) wind_speeds = data_file['wind_speed'][:] powers = data_file['crosswind_power'][:] sim_successes = data_file['sim_success'][:] for w, p, s in zip(wind_speeds, powers, sim_successes): self.assertAlmostEqual(1000.0 * w**3, p, delta=1e-8) self.assertEqual(s, w < 4.0 or w > 6.0) # Check that all the plots and HTML files were created. self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'mean_flap_deflections.svg'))) self.assertTrue( os.path.isfile( os.path.join( temp_dir, 'standard_deviation_flap_deflections.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'faults_vs_wind_speed.png'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'power_curve_by_throttle.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'power_and_tension_curve.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'angles_vs_wind_speed.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'angle_errors_vs_wind_speed.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'radius_vs_wind_speed.svg'))) self.assertTrue( os.path.isfile( os.path.join(temp_dir, 'curvature_errors_vs_wind_speed.svg'))) self.assertTrue( os.path.isfile(os.path.join(temp_dir, 'index.html')))
def testClientAndWorker(self): patch = testing_fakes.PatchAllFakes( binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=crosswind_sweeps_worker.CrosswindSweepsSimWorker, client_class=crosswind_sweeps_client.CrosswindSweepsSimClient) # FLAGS are not automatically reset between tests. with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.output_dir = temp_dir gflags.FLAGS.wind_speeds = [5.0] client = crosswind_sweeps_client.CrosswindSweepsSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'style.css'))) self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'index.html'))) with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.output_dir = temp_dir gflags.FLAGS.custom_sweep = ( '[{"name": "Mass Scale",' '"path": ["sim", "wing_sim", "mass_prop_uncertainties",' '"mass_scale"],' '"distribution": {"lower_bound": 0.9, "upper_bound": 1.1,' '"type": "uniform"},' '"values": [0.9, 1.0, 1.1]}]') gflags.FLAGS.only_custom_sweep = True client = crosswind_sweeps_client.CrosswindSweepsSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'style.css'))) self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'index.html'))) # Reset these flags to default before next test. gflags.FLAGS.custom_sweep = '[]' gflags.FLAGS.only_custom_sweep = False with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.output_dir = temp_dir gflags.FLAGS.monte_carlo = True gflags.FLAGS.monte_carlo_rows = 2 gflags.FLAGS.monte_carlo_cols = 2 gflags.FLAGS.wind_speeds = [5.0] client = crosswind_sweeps_client.CrosswindSweepsSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'style.css'))) self.assertTrue(os.path.isfile(os.path.join(temp_dir, 'index.html')))
def testSuccess(self): patch = testing_fakes.PatchAllFakes(binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=GoodWorker, client_class=TestSimClient) with patch: client = TestSimClient(sim_name=self.SIM_NAME, num_workers=self.NUM_WORKERS) client.Run() self.assertAlmostEqual(10 * _SIM_TIME, client.total_sim_time)
def testError(self): patch = testing_fakes.PatchAllFakes(binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=BadWorker, client_class=TestSimClient) with patch, mock.patch( 'makani.lib.python.batch_sim.client.logging') as mock_log: client = TestSimClient(sim_name=self.SIM_NAME, num_workers=self.NUM_WORKERS) client.Run() self.assertTrue(mock_log.error.called) self.assertEqual(0.0, client.total_sim_time) self.assertEqual({}, client._gcompute.metadata_by_instance_name)
def testMaxJobsPerWorker(self): for max_jobs, expected_num_workers in zip([1, 2, 3, 4, 5], [10, 5, 4, 3, 2]): FLAGS.max_jobs_per_worker = max_jobs patch = testing_fakes.PatchAllFakes(binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=GoodWorker, client_class=TestSimClient) with patch: client = TestSimClient(sim_name=self.SIM_NAME) client.Run() self.assertAlmostEqual(10 * _SIM_TIME, client.total_sim_time) self.assertEqual(expected_num_workers, client._num_workers)
def testNoDeleteWorkersOnError(self): patch = testing_fakes.PatchAllFakes(binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=BadWorker, client_class=TestSimClient) with patch, mock.patch( 'makani.lib.python.batch_sim.client.logging') as mock_log: with test_util.FlagValueSaver(): FLAGS.delete_workers_on_error = False client = TestSimClient(sim_name=self.SIM_NAME, num_workers=self.NUM_WORKERS) client.Run() self.assertTrue(mock_log.error.called) self.assertEqual(0.0, client.total_sim_time) self.assertEqual(self.NUM_WORKERS, len(client._gcompute.metadata_by_instance_name))
def testClientAndWorker(self): class FakeBinaries(testing_fakes.FakeBinaries): _NUM_SAMPLES = 100 def Controller(self, args): return 0 def GroundEstimator(self, args): return 0 def Simulator(self, args): return 0 def PcapToHdf5(self, args): log_file_name = self._ParseArg(args, '--output_file') MakeFakeLogFile(log_file_name, self._NUM_SAMPLES) return 0 patch = testing_fakes.PatchAllFakes( binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=batch_sim_worker.IecCasesSimWorker, client_class=batch_sim_client.IecCasesSimClient) with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.output_dir = temp_dir client = batch_sim_client.IecCasesSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() # Check that all the plots and HTML files were created. image_files = [ f for f in os.listdir(temp_dir) if re.match(r'.+\.png', f) ] self.assertEqual(len(batch_sim_client.IEC_CASES), len(image_files)) self.assertTrue( os.path.isfile(os.path.join(temp_dir, 'index.html')))
def testClientAndWorker(self): FLAGS.wind_speeds = numpy.linspace(3.0, 15.0, 3) FLAGS.num_amplitudes = 2 FLAGS.show_events = False patch = testing_fakes.PatchAllFakes( binary_map=self.BINARY_MAP, binaries=FakeBinaries(), worker_factory=batch_sim_worker.HoverDisturbancesSimWorker, client_class=batch_sim_client.HoverDisturbancesSimClient) with os_util.TempDir() as temp_dir: with patch: gflags.FLAGS.output_dir = temp_dir client = batch_sim_client.HoverDisturbancesSimClient( num_workers=self.NUM_WORKERS, sim_name=self.SIM_NAME) client.Run() self.assertTrue( os.path.isfile(os.path.join(temp_dir, 'style.css'))) self.assertTrue( os.path.isfile(os.path.join(temp_dir, 'index.html')))