Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Test performance export+import to|from buffer vs JSON serialization
REPORT ztest_srlz_json_vs_buffer.
TYPES:
BEGIN OF ts_result,
tag TYPE char30,
start TYPE i,
end TYPE i,
diff TYPE i,
END OF ts_result,
tt_result TYPE STANDARD TABLE OF ts_result WITH EMPTY KEY.
PARAMETERS p_size TYPE i DEFAULT 10000.
PARAMETERS p_count TYPE i DEFAULT 100.
CLASS zcl_c8a005_tab_json DEFINITION.
PUBLIC SECTION.
METHODS constructor .
METHODS get_json_string
IMPORTING
!it_tab_data TYPE any
EXPORTING
!ev_json_str TYPE string .
METHODS get_from_json2tab
IMPORTING
!iv_tabname TYPE tabname
!iv_json_str TYPE string
EXPORTING
!et_data TYPE STANDARD TABLE.
PROTECTED SECTION.
PRIVATE SECTION.
CONSTANTS: BEGIN OF mc
, json_open TYPE string VALUE '{"JSON":'
, json_close TYPE string VALUE '}'
, END OF mc.
METHODS create_tab_n_line
IMPORTING
!iv_tabname TYPE tabname
EXPORTING
!ert_ref TYPE REF TO data
!ers_ref TYPE REF TO data
.
METHODS get_tab_n_line_type
IMPORTING
!iv_tabname TYPE tabname
EXPORTING
!eo_line_type TYPE REF TO cl_abap_structdescr
!eo_tab_type TYPE REF TO cl_abap_tabledescr.
ENDCLASS.
CLASS zcl_c8a005_tab_json IMPLEMENTATION.
METHOD constructor.
ENDMETHOD. "constructor
METHOD create_tab_n_line.
* IMPORTING
* !iv_tabname TYPE tabname
* EXPORTING
* !ert_ref TYPE REF TO data
* !ers_ref TYPE REF TO data
* .
DATA lv_tabname TYPE tabname.
DATA lo_line_type TYPE REF TO cl_abap_structdescr.
DATA lo_tab_type TYPE REF TO cl_abap_tabledescr.
DATA lt_tab_ref TYPE REF TO data.
DATA ls_line_ref TYPE REF TO data.
lv_tabname = iv_tabname.
TRY.
me->get_tab_n_line_type(
EXPORTING
iv_tabname = lv_tabname " Имя таблицы
IMPORTING
eo_line_type = lo_line_type " Runtime Type Services
eo_tab_type = lo_tab_type " Runtime Type Services
).
" CATCH zcx_nm0002_templ. " Exceptions while template
CREATE DATA lt_tab_ref TYPE HANDLE lo_tab_type.
CREATE DATA ls_line_ref TYPE HANDLE lo_line_type.
ert_ref ?= lt_tab_ref.
ers_ref ?= ls_line_ref.
CATCH cx_root.
MESSAGE x000(cl).
ENDTRY.
ENDMETHOD. "create_tab_n_line
METHOD get_from_json2tab.
DATA lv_json_str TYPE string.
DATA lr_db_content TYPE REF TO data.
DATA lr_reader TYPE REF TO if_sxml_reader.
DATA lo_tab_type TYPE REF TO cl_abap_tabledescr.
FIELD-SYMBOLS <fs_dyn_tab> TYPE STANDARD TABLE.
IF et_data IS NOT SUPPLIED.
TRY .
create_tab_n_line(
EXPORTING
iv_tabname = iv_tabname " Имя таблицы
IMPORTING
ert_ref = lr_db_content
).
CATCH cx_root.
MESSAGE e000(cl).
RETURN.
ENDTRY.
ASSIGN lr_db_content->* TO <fs_dyn_tab> .
ELSE.
ASSIGN et_data TO <fs_dyn_tab>.
ENDIF.
lv_json_str = mc-json_open && iv_json_str && mc-json_close.
TRY.
CLEAR <fs_dyn_tab>.
CALL TRANSFORMATION id
SOURCE XML lv_json_str
RESULT json = <fs_dyn_tab>.
CATCH cx_root.
MESSAGE e000(cl).
ENDTRY.
ENDMETHOD. "get_from_json2tab
METHOD get_json_string.
* IMPORTING it_tab_data TYPE any
* EXPORTING ev_json_str TYPE string.
DATA lo_writer TYPE REF TO cl_sxml_string_writer.
DATA lv_xstr_json TYPE xstring.
" DATA lv_str_json TYPE string.
DATA lv_reponse2pend TYPE string.
lo_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE json = it_tab_data
RESULT XML lo_writer.
lv_xstr_json = lo_writer->get_output( ).
TRY.
ev_json_str = cl_abap_codepage=>convert_from(
source = lv_xstr_json
* codepage = `UTF-8`
* endian =
* replacement = '#'
* ignore_cerr = ABAP_FALSE
).
CATCH cx_parameter_invalid_range. " Parameter with Invalid Range
CATCH cx_sy_codepage_converter_init. " System Exception for Code Page Converter Initialization
CATCH cx_sy_conversion_codepage. " System Exception Converting Character Set
CATCH cx_parameter_invalid_type. " Parameter with Invalid Type
ENDTRY.
"REPLACE FIRST OCCURRENCE OF '{"JSON":[' IN ev_json_str WITH '{"request":['.
" REPLACE ALL OCCURRENCES OF '{"JSON":' IN lv_str_json WITH ''.
REPLACE ALL OCCURRENCES OF mc-json_open IN ev_json_str WITH ''.
ev_json_str = shift_right( val = ev_json_str places = 1 ).
" ev_json_str = lv_str_json.
ENDMETHOD. "get_json_string
METHOD get_tab_n_line_type.
* METHODS get_tab_n_line_type
* IMPORTING
* !iv_tabname TYPE tabname
* EXPORTING
* !eo_line_type TYPE REF TO cl_abap_structdescr
* !eo_tab_type TYPE REF TO cl_abap_tabledescr.
DATA lo_struct TYPE REF TO cl_abap_structdescr.
DATA lt_comp TYPE cl_abap_structdescr=>component_table.
DATA lv_msg TYPE string.
DATA lo_typedescr TYPE REF TO cl_abap_typedescr.
lo_struct ?= cl_abap_typedescr=>describe_by_name( iv_tabname ).
lt_comp = lo_struct->get_components( ).
TRY .
eo_line_type = cl_abap_structdescr=>create(
p_components = lt_comp
* p_strict = true
).
eo_tab_type = cl_abap_tabledescr=>create(
p_line_type = eo_line_type
* p_table_kind = tablekind_std
* p_unique = abap_false
* p_key =
* p_key_kind = keydefkind_default
).
CATCH cx_sy_table_creation. " Exception when Creating a Table Type
MESSAGE e001(cl) WITH iv_tabname INTO lv_msg.
ENDTRY.
ENDMETHOD. "get_tab_n_line_type
ENDCLASS.
START-OF-SELECTION.
PERFORM main.
FORM main.
DATA lt_result TYPE tt_result.
DATA ls_result TYPE ts_result.
DATA lt_data TYPE STANDARD TABLE OF mara WITH DEFAULT KEY.
DATA lt_data2 TYPE STANDARD TABLE OF mara WITH DEFAULT KEY.
DATA lo_tab_json TYPE REF TO zcl_c8a005_tab_json.
DATA lv_json_str TYPE string.
DATA lv_xstr TYPE xstring.
DO p_size TIMES.
APPEND VALUE #( matnr = sy-index ) TO lt_data.
ENDDO.
DO.
CLEAR ls_result.
GET RUN TIME FIELD ls_result-start.
CASE sy-index.
WHEN 1.
ls_result-tag = 'EXPORT/IMPORT'.
DO p_count TIMES.
EXPORT data = lt_data TO DATA BUFFER lv_xstr.
IMPORT data = lt_data2 FROM DATA BUFFER lv_xstr.
ENDDO.
WHEN 2.
ls_result-tag = 'EXPORT/IMPORT with ZIP'.
DO p_count TIMES.
EXPORT data = lt_data TO DATA BUFFER lv_xstr COMPRESSION ON.
IMPORT data = lt_data2 FROM DATA BUFFER lv_xstr.
ENDDO.
WHEN 3.
ls_result-tag = 'JSON'.
CREATE OBJECT lo_tab_json.
DO p_count TIMES.
lo_tab_json->get_json_string( EXPORTING it_tab_data = lt_data
IMPORTING ev_json_str = lv_json_str ).
lo_tab_json->get_from_json2tab(
EXPORTING
iv_tabname = 'MARA'
iv_json_str = lv_json_str
IMPORTING
et_data = lt_data2
).
ENDDO.
WHEN OTHERS.
EXIT.
ENDCASE.
GET RUN TIME FIELD ls_result-end.
ls_result-diff = ls_result-end - ls_result-start.
APPEND ls_result TO lt_result.
ENDDO.
SORT lt_result BY diff.
LOOP AT lt_result INTO ls_result.
WRITE: ls_result-tag, ls_result-diff, ls_result-start, ls_result-end.
NEW-LINE.
ENDLOOP.
ENDFORM.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment