Esempio n. 1
0
def test_benchmark():
  b = util.Benchmark()
  assert b.solver == None
  assert b.timeout == 1e9
  assert b.log_filename == 'benchmark-log.yaml'
  def on_nl_file(nl_file):
    b.solver_options['answer'] = 42
  b = util.Benchmark(solver='testsolver', timeout=11, log='test.log', on_nl_file=on_nl_file)
  assert b.solver == 'testsolver'
  assert b.timeout == 11
  assert b.log_filename == 'test.log'
  assert not os.path.exists('test.log')
  assert b.on_nl_file == on_nl_file
  with temp_ampl_file() as ampl_file:
    with tempfile.NamedTemporaryFile() as log_file:
      with util.Benchmark(solver=mock_solver, solver_options={'answer': 42},
                          log=log_file.name) as b:
        assert log_file.read() == ''
        b.run(ampl_file.name)
      log = yaml.load(log_file.read())
      assert len(log) == 1
      entry = log[0]
      assert entry['model'] == ampl_file.name
      assert entry['sha'] == util.sha1_file(ampl_file.name)
      assert entry['solver'] == mock_solver
      assert entry['solver_options'] == {'answer': 42}
      assert entry['start']
      assert float(entry['time']) > 0
      assert not entry['timeout']
      assert type(entry['obj']) is float
      assert entry['solve_result']
      assert entry['solve_message']
      assert entry['output'].endswith("'-AMPL', 'answer=42']\n")
Esempio n. 2
0
def test_benchmark():
    b = util.Benchmark()
    assert b.solver == None
    assert b.timeout == 1e9
    assert b.log_filename == 'benchmark-log.yaml'

    def on_nl_file(nl_file):
        b.solver_options['answer'] = 42

    b = util.Benchmark(solver='testsolver',
                       timeout=11,
                       log='test.log',
                       on_nl_file=on_nl_file)
    assert b.solver == 'testsolver'
    assert b.timeout == 11
    assert b.log_filename == 'test.log'
    assert not os.path.exists('test.log')
    assert b.on_nl_file == on_nl_file
    with temp_ampl_file() as ampl_file:
        with tempfile.NamedTemporaryFile() as log_file:
            with util.Benchmark(solver=mock_solver,
                                solver_options={'answer': 42},
                                log=log_file.name) as b:
                assert log_file.read() == ''
                b.run(ampl_file.name)
            log = yaml.load(log_file.read())
            assert len(log) == 1
            entry = log[0]
            assert entry['model'] == ampl_file.name
            assert entry['sha'] == util.sha1_file(ampl_file.name)
            assert entry['solver'] == mock_solver
            assert entry['solver_options'] == {'answer': 42}
            assert entry['start']
            assert float(entry['time']) > 0
            assert not entry['timeout']
            assert type(entry['obj']) is float
            assert entry['solve_result']
            assert entry['solve_message']
            assert entry['output'].endswith("'-AMPL', 'answer=42']\n")
Esempio n. 3
0
def test_sha1_file():
    with tempfile.NamedTemporaryFile() as f:
        f.write('some useful content')
        f.flush()
        assert (util.sha1_file(
            f.name) == '4f7a376f6110cb8aad4f02e319b52f7325d63a83')
Esempio n. 4
0
def test_sha1_file():
  with tempfile.NamedTemporaryFile() as f:
    f.write('some useful content')
    f.flush()
    assert(util.sha1_file(f.name) == '4f7a376f6110cb8aad4f02e319b52f7325d63a83')