示例#1
0
    def test_load_data_threaded_perf(self):
        # loadData - Not using threads
        non_threaded_time = 0
        for i in range(10000):
            base = PFData(
                ('LW.out.press.00000.pfb'
                 ))  # Little Washita pressure PFB with 2x2x1 grid size
            base.loadHeader()

            start = time.time_ns()
            base.loadData()
            non_threaded_time += time.time_ns() - start

        # loadDataThreaded - Using 4 threads
        num_threads = 4
        threaded_time = 0
        for i in range(10000):
            test = PFData(
                ('LW.out.press.00000.pfb'
                 ))  # Little Washita pressure PFB with 2x2x1 grid size
            test.loadHeader()

            start = time.time_ns()
            test.loadPQR(
            )  # loadPQR() must be called before loadDataThreaded()
            test.loadDataThreaded(num_threads)
            threaded_time += time.time_ns() - start

        base.close()
        test.close()

        # loadDataThreaded() should have less total time spent than loadData()
        self.assertTrue(
            threaded_time < non_threaded_time,
            f'Using {num_threads} threads has degraded the performance of loadDataThreaded()'
        )

        # Display performance increase in percent change
        pct_change = 100 * abs(threaded_time -
                               non_threaded_time) / non_threaded_time
        print(
            f'{pct_change:.2f}% performance increase when using LoadDataThreaded() with {num_threads} threads'
        )
示例#2
0
    def test_load_data_threaded(self):
        base = PFData(('press.init.pfb'))
        base.loadHeader()
        base.loadData()

        # 1 thread
        test1 = PFData(('press.init.pfb'))
        test1.loadHeader()
        test1.loadPQR()
        test1.loadDataThreaded(1)
        self.assertEqual(PFData.differenceType_none,
                         base.compare(test1)[0], "base and test1 are the same")

        # 8 threads
        test8 = PFData(('press.init.pfb'))
        test8.loadHeader()
        test8.loadPQR()
        test8.loadDataThreaded(8)
        self.assertEqual(PFData.differenceType_none,
                         base.compare(test8)[0], "base and test8 are the same")

        # 40 threads (more than the number of subgrids)
        test40 = PFData(('press.init.pfb'))
        test40.loadHeader()
        test40.loadPQR()
        test40.loadDataThreaded(40)
        self.assertEqual(PFData.differenceType_none,
                         base.compare(test40)[0],
                         "base and test40 are the same")

        base.close()
        test1.close()
        test8.close()
        test40.close()
示例#3
0
    def test_read_write_data(self):
        test = PFData(('press.init.pfb'))
        retval = test.loadHeader()
        self.assertEqual(0, retval, 'should load header of file that exists')
        retval = test.loadPQR()
        self.assertEqual(0, retval, 'should load PQR of file that exists')
        retval = test.loadData()
        self.assertEqual(0, retval, 'should load data from valid file')
        retval = test.writeFile(('press.init.pfb.tmp'))
        self.assertEqual(0, retval,
                         'should write data from previously loaded file')

        data2 = PFData(('press.init.pfb.tmp'))
        data2.loadHeader()
        data2.loadData()
        data2.loadPQR()
        self.assertIsNone(
            np.testing.assert_array_equal(
                test.viewDataArray(), data2.viewDataArray(),
                'should read back same values we wrote'))
        in_file_hash = calculate_sha1_hash(('press.init.pfb'))
        out_file_hash = calculate_sha1_hash(('press.init.pfb.tmp'))

        # This assertion (that the files are identical) is failing in Python and in C++
        # because the original test input file was written by a tool that incorrectly set the value
        self.assertNotEqual(
            in_file_hash, out_file_hash,
            'sha1 hash of input and output files should not match')

        same, byte_diff = byte_compare_files(('press.init.pfb'),
                                             ('press.init.pfb.tmp'))

        self.assertFalse(
            same, 'press.init.pfb should differ from version just written')
        self.assertEqual(92, byte_diff, 'first byte difference at byte 92')

        test.close()
        data2.close()
        os.remove(('press.init.pfb.tmp'))