Saving and Reloading a Measurement 
Imagine that you have a set of tests for which you want to measure coverage. Some of them take longer than a typical unit test (perhaps an ECATT integration test). Others must be started with a different user, and some might even need to be executed manually.
A typical solution for this scenario is to schedule two batch jobs for example weekly. The first job runs every Monday to start and persist a measurement. The second job runs every Friday to retrieve and stop the measurement. After the measurement has been stopped, you can calculate the coverage results.
Between the start and stop of the measurement, the Coverage API records all automatic or manual tests by the users specified in the measurement.
The Coverage API works in this scenario almost like the Coverage Analyzer (transaction SCOV), in that all activity by the specified users is recorded, not just the relevant tests.
Note
Be sure in this scenario not to import or otherwise change the code under measurement while the measurement is running.
If measured code is changed, then the system automatically deletes the raw data of affected programs. This is necessary: The raw data no longer corresponds to the source code.
Any results that you calculate in this situation are unreliable and not reproducible.
Here is sample code for saving a measurement and then later retrieving and stopping the measurement.
Syntax
REPORT START_MEASUREMENT_AND_SAVE.
* This report starts a measurement and saves it.
* A second report, perhaps started interactively,
* stops the measurement and calculates coverage
* results.
DATA: factory TYPE REF TO if_scv_factory,
measurement TYPE REF TO if_scv_measurement,
users TYPE if_scv_measurement=>users,
repository TYPE REF TO if_scv_repository,
measurement_id TYPE cva_uuid,
id_records TYPE my_measurements.
* Create a measurement
factory = cl_scv_coverage_api=>get_factory( ).
APPEND 'AUNIT_USER' TO users. "Coverage will be recorded
APPEND 'ECATT_USER' TO users. "for these users.
measurement = factory->create_measurement(
i_users = users ).
* Start the measurement
measurement->start( ).
* Save the measurement and store the measurement ID
* for use when stopping the measurement.
repository = cl_scv_coverage_api=>get_repository( ).
measurement_id = repository->save_measurement( measurement ).
* You need to save the UUID for reloading the measurement
id_records-id = measurement_id.
id_records-date = sy-datum.
INSERT INTO z_table_of_measurements VALUES id_records.
While the measurement is running (status started), the Coverage API records all of the tests and other activity of users AUNIT_USER and ECATT_USER (in this example).
To end the measurement and calculate and report results, you could run a second program that might look like this sample code.
Syntax
REPORT STOP_MEASUREMENT_AND_REPORT.
DATA: measurement TYPE REF TO if_scv_measurement,
repository TYPE REF TO if_scv_repository,
result TYPE REF TO if_scv_result,
measurement_id TYPE cva_uuid,
id_records TYPE my_measurements.
* Get the UUID of the active measurement
SELECT SINGLE * FROM my_table_of_measurements INTO id_records WHERE...
measurement_id = id_records-id.
* Get the repository
repository = cl_scv_coverage_api=>get_repository( ).
* Retrieve the measurement and stop it
measurement = repository->load_measurement( measurement_id ).
measurement->stop( ).
* Calculate and report results....
result = measurement->build_package_result( "MY_PACKAGE" ).
...
For more information on saving and retrieving standalone measurements from the repository of the Coverage API, see Step 6: Saving Code Coverage Results.
For more information about saving and reloading measurements in measurement series, see Step 5: Saving Measurements and Results in a Measurement Series.
For more information on using results to report coverage statistics and on displaying results, see Step 3: Calculating Code Coverage Results, Step 4: Traversing the Hierarchy of Results, and Step 5: Displaying the Code Coverage Results.