Skip to content

Instantly share code, notes, and snippets.

@victorizbitskiy
Last active November 13, 2022 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorizbitskiy/3805caaa6a72a717569deee791e00dfc to your computer and use it in GitHub Desktop.
Save victorizbitskiy/3805caaa6a72a717569deee791e00dfc to your computer and use it in GitHub Desktop.
ABAP APPEND TO INNER TABLE SPEADTEST
*&---------------------------------------------------------------------*
*& Report ZSPEADTEST_APPEND
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZSPEADTEST_APPEND.
CLASS lcl_append_opera_spead_test DEFINITION FINAL.
PUBLIC SECTION.
TYPES:
BEGIN OF ty_accumulator,
full_name TYPE string,
birthday TYPE d,
age TYPE i,
END OF ty_accumulator,
ty_t_accumulator TYPE STANDARD TABLE OF ty_accumulator WITH EMPTY KEY.
TYPES:
BEGIN OF ty_result,
name TYPE c LENGTH 30,
value TYPE timestampl,
place TYPE i,
END OF ty_result,
ty_t_result TYPE STANDARD TABLE OF ty_result WITH EMPTY KEY.
DATA mv_test_runs_times TYPE i.
DATA mv_operations_times TYPE i.
METHODS:
constructor IMPORTING iv_test_runs_times TYPE i
iv_operations_times TYPE i,
run,
get_result RETURNING VALUE(rt_result) TYPE ty_t_result.
PRIVATE SECTION.
DATA mt_run_result TYPE ty_t_result.
DATA mt_runs_result TYPE ty_t_result.
METHODS:
append,
append_with_clear_before,
append_value,
append_initial_line,
base,
place_assignment.
ENDCLASS.
CLASS lcl_opera_spead_test_res_view DEFINITION FINAL.
PUBLIC SECTION.
TYPES:
BEGIN OF ty_common_result,
name TYPE c LENGTH 30,
cnt_place_1 TYPE i,
cnt_place_2 TYPE i,
cnt_place_3 TYPE i,
cnt_place_4 TYPE i,
cnt_place_5 TYPE i,
time TYPE timestampl,
END OF ty_common_result,
ty_t_common_result TYPE STANDARD TABLE OF ty_common_result WITH EMPTY KEY.
METHODS:
constructor IMPORTING io_opera_spead_test TYPE REF TO lcl_append_opera_spead_test,
display.
PRIVATE SECTION.
DATA mo_opera_spead_test TYPE REF TO lcl_append_opera_spead_test.
DATA mt_common_result TYPE ty_t_common_result.
METHODS:
prepare,
collect_result IMPORTING is_result TYPE lcl_append_opera_spead_test=>ty_result.
ENDCLASS.
CLASS lcl_report DEFINITION FINAL.
PUBLIC SECTION.
CLASS-METHODS:
end_of_selection.
ENDCLASS.
CLASS lcl_append_opera_spead_test IMPLEMENTATION.
METHOD constructor.
mv_test_runs_times = iv_test_runs_times.
mv_operations_times = iv_operations_times.
ENDMETHOD.
METHOD run.
DO mv_test_runs_times TIMES.
CLEAR mt_run_result.
append( ).
append_with_clear_before( ).
append_initial_line( ).
append_value( ).
base( ).
place_assignment( ).
ENDDO.
ENDMETHOD.
METHOD get_result.
rt_result = mt_runs_result.
ENDMETHOD.
METHOD append.
DATA lv_tsmp_before TYPE timestampl.
DATA lv_tsmp_after TYPE timestampl.
DATA lv_tsmp_result TYPE timestampl.
DATA lt_acc TYPE STANDARD TABLE OF ty_accumulator.
DATA ls_acc LIKE LINE OF lt_acc.
GET TIME STAMP FIELD lv_tsmp_before.
DO mv_operations_times TIMES.
ls_acc-full_name = |John Doe|.
ls_acc-birthday = sy-datum.
ls_acc-age = 30.
APPEND ls_acc TO lt_acc.
ENDDO.
GET TIME STAMP FIELD lv_tsmp_after.
lv_tsmp_result = cl_abap_tstmp=>subtract( tstmp1 = lv_tsmp_after
tstmp2 = lv_tsmp_before ).
APPEND VALUE ty_result( name = 'APPEND' value = lv_tsmp_result ) TO mt_run_result.
ENDMETHOD.
METHOD append_with_clear_before.
DATA lv_tsmp_before TYPE timestampl.
DATA lv_tsmp_after TYPE timestampl.
DATA lv_tsmp_result TYPE timestampl.
DATA lt_acc TYPE STANDARD TABLE OF ty_accumulator.
DATA ls_acc LIKE LINE OF lt_acc.
GET TIME STAMP FIELD lv_tsmp_before.
DO mv_operations_times TIMES.
CLEAR ls_acc.
ls_acc-full_name = |John Doe|.
ls_acc-birthday = sy-datum.
ls_acc-age = 30.
APPEND ls_acc TO lt_acc.
ENDDO.
GET TIME STAMP FIELD lv_tsmp_after.
lv_tsmp_result = cl_abap_tstmp=>subtract( tstmp1 = lv_tsmp_after
tstmp2 = lv_tsmp_before ).
APPEND VALUE ty_result( name = 'APPEND_WITH_CLEAR_BEFORE' value = lv_tsmp_result ) TO mt_run_result.
ENDMETHOD.
METHOD append_value.
DATA lv_tsmp_before TYPE timestampl.
DATA lv_tsmp_after TYPE timestampl.
DATA lv_tsmp_result TYPE timestampl.
DATA lt_acc TYPE STANDARD TABLE OF ty_accumulator.
GET TIME STAMP FIELD lv_tsmp_before.
DO mv_operations_times TIMES.
APPEND VALUE ty_accumulator( full_name = |John Doe|
birthday = sy-datum
age = 30 ) TO lt_acc.
ENDDO.
GET TIME STAMP FIELD lv_tsmp_after.
lv_tsmp_result = cl_abap_tstmp=>subtract( tstmp1 = lv_tsmp_after
tstmp2 = lv_tsmp_before ).
APPEND VALUE ty_result( name = 'APPEND_VALUE' value = lv_tsmp_result ) TO mt_run_result.
ENDMETHOD.
METHOD append_initial_line.
DATA lv_tsmp_before TYPE timestampl.
DATA lv_tsmp_after TYPE timestampl.
DATA lv_tsmp_result TYPE timestampl.
DATA lt_acc TYPE STANDARD TABLE OF ty_accumulator.
GET TIME STAMP FIELD lv_tsmp_before.
DO mv_operations_times TIMES.
APPEND INITIAL LINE TO lt_acc ASSIGNING FIELD-SYMBOL(<lfs_acc>).
<lfs_acc>-full_name = |John Doe|.
<lfs_acc>-birthday = sy-datum.
<lfs_acc>-age = 30.
ENDDO.
GET TIME STAMP FIELD lv_tsmp_after.
lv_tsmp_result = cl_abap_tstmp=>subtract( tstmp1 = lv_tsmp_after
tstmp2 = lv_tsmp_before ).
APPEND VALUE ty_result( name = 'APPEND_INITIAL_LINE' value = lv_tsmp_result ) TO mt_run_result.
ENDMETHOD.
METHOD base.
DATA lv_tsmp_before TYPE timestampl.
DATA lv_tsmp_after TYPE timestampl.
DATA lv_tsmp_result TYPE timestampl.
DATA lt_acc TYPE STANDARD TABLE OF ty_accumulator.
GET TIME STAMP FIELD lv_tsmp_before.
DO mv_operations_times TIMES.
lt_acc = VALUE ty_t_accumulator( BASE lt_acc ( full_name = |John Doe|
birthday = sy-datum
age = 30 ) ).
ENDDO.
GET TIME STAMP FIELD lv_tsmp_after.
lv_tsmp_result = cl_abap_tstmp=>subtract( tstmp1 = lv_tsmp_after
tstmp2 = lv_tsmp_before ).
APPEND VALUE ty_result( name = 'BASE' value = lv_tsmp_result ) TO mt_run_result.
ENDMETHOD.
METHOD place_assignment.
DATA lv_cnt TYPE i.
" Определяем места в текущем прогоне
SORT mt_run_result BY value ASCENDING.
LOOP AT mt_run_result ASSIGNING FIELD-SYMBOL(<lfs_run_result>).
lv_cnt = lv_cnt + 1.
APPEND VALUE ty_result( name = <lfs_run_result>-name
value = <lfs_run_result>-value
place = lv_cnt ) TO mt_runs_result.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
CLASS lcl_opera_spead_test_res_view IMPLEMENTATION.
METHOD constructor.
mo_opera_spead_test = io_opera_spead_test.
ENDMETHOD.
METHOD display.
DATA lo_salv_table TYPE REF TO cl_salv_table.
prepare( ).
TRY.
cl_salv_table=>factory( IMPORTING r_salv_table = lo_salv_table
CHANGING t_table = mt_common_result ).
DATA(lo_salv_functions) = lo_salv_table->get_functions( ).
lo_salv_functions->set_all( abap_true ).
DATA(lo_salv_columns_table) = lo_salv_table->get_columns( ).
lo_salv_columns_table->set_optimize( abap_true ).
DATA(lo_column) = lo_salv_columns_table->get_column( 'NAME' ).
lo_column->set_long_text( |Operation name| ).
lo_column = lo_salv_columns_table->get_column( 'CNT_PLACE_1' ).
lo_column->set_long_text( |First places| ).
lo_column = lo_salv_columns_table->get_column( 'CNT_PLACE_2' ).
lo_column->set_long_text( |Second places| ).
lo_column = lo_salv_columns_table->get_column( 'CNT_PLACE_3' ).
lo_column->set_long_text( |Thirth places| ).
lo_column = lo_salv_columns_table->get_column( 'CNT_PLACE_4' ).
lo_column->set_long_text( |Fourth places| ).
lo_column = lo_salv_columns_table->get_column( 'CNT_PLACE_5' ).
lo_column->set_long_text( |Fifth places| ).
lo_column = lo_salv_columns_table->get_column( 'TIME' ).
lo_column->set_short_text( |AverTime| ).
lo_column->set_medium_text( |Average time| ).
lo_column->set_long_text( |Average time| ).
lo_salv_table->display( ).
CATCH cx_salv_msg cx_salv_not_found INTO DATA(lx_e).
WRITE lx_e->get_text( ).
ENDTRY.
ENDMETHOD.
METHOD prepare.
DATA(lt_runs_result) = mo_opera_spead_test->get_result( ).
LOOP AT lt_runs_result ASSIGNING FIELD-SYMBOL(<lfs_runs_result>).
collect_result( <lfs_runs_result> ).
ENDLOOP.
LOOP AT mt_common_result ASSIGNING FIELD-SYMBOL(<lfs_common_result>).
<lfs_common_result>-time = <lfs_common_result>-time / mo_opera_spead_test->mv_test_runs_times.
ENDLOOP.
SORT mt_common_result BY cnt_place_1 DESCENDING.
ENDMETHOD.
METHOD collect_result.
FIELD-SYMBOLS <lfs_common_result> LIKE LINE OF mt_common_result.
FIELD-SYMBOLS <lfs_place_cnt_val> TYPE any.
UNASSIGN <lfs_common_result>.
ASSIGN mt_common_result[ name = is_result-name ] TO <lfs_common_result>.
IF <lfs_common_result> IS NOT ASSIGNED.
APPEND VALUE #( name = is_result-name ) TO mt_common_result ASSIGNING <lfs_common_result>.
ENDIF.
ASSIGN COMPONENT |{ 'CNT_PLACE_' }{ is_result-place }| OF STRUCTURE <lfs_common_result> TO <lfs_place_cnt_val>.
<lfs_place_cnt_val> = <lfs_place_cnt_val> + 1.
ASSIGN COMPONENT 'TIME' OF STRUCTURE <lfs_common_result> TO <lfs_place_cnt_val>.
<lfs_place_cnt_val> = <lfs_place_cnt_val> + is_result-value.
ENDMETHOD.
ENDCLASS.
CLASS lcl_report IMPLEMENTATION.
METHOD end_of_selection.
DATA(lv_test_runs_times) = 1000.
DATA(lv_operations_times) = 1000.
DATA(lo_append_opera_spead_test) = NEW lcl_append_opera_spead_test( iv_test_runs_times = lv_test_runs_times
iv_operations_times = lv_operations_times ).
lo_append_opera_spead_test->run( ).
DATA(lo_opera_spead_test_res_view) = NEW lcl_opera_spead_test_res_view( lo_append_opera_spead_test ).
lo_opera_spead_test_res_view->display( ).
ENDMETHOD.
ENDCLASS.
END-OF-SELECTION.
lcl_report=>end_of_selection( ).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment