Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Purposefully dodgy Fibonacci implementation, simply to generate CPU load on the application server for testing. This is meant to be bad code intended to be as CPU-hungry as possible. Suggestions for making it worse are welcome! What it does can be controlled using two URL GET parameters: * count=x, which specifies the number of Fibonacci numbers…
method IF_HTTP_EXTENSION~HANDLE_REQUEST.
* Purposefully dodgy Fibonacci implementation, simply to generate CPU load for testing.
* This is meant to be bad code intended to be as CPU-hungry as possible.
* What it does can be controlled using two URL parameters:
* count=x, which specifies the number of Fibonacci numbers output.
* cache=true, which causes the ICM to cache the result for 5 minutes and thus reduce the load on ABAP.
* Any questions, contact @sufw
data:
verb type string,
fibfragment type string,
response_content type string,
howMany type i,
val1 type string,
val2 type string,
sumof1and2 type string,
totalCounter type i,
cacheFlag type string.
* Sensible? default for the length of the Fib sequence.
* Note that due to the algorithm, this actually produces x+1 numbers.
howMany = 19.
* enable other handlers
if_http_extension~flow_rc = if_http_extension=>co_flow_ok_others_opt.
* set not stateful
server->set_session_stateful( stateful = server->co_disabled ).
verb = server->request->get_header_field( name = '~request_method' ).
if verb ne 'GET'.
server->response->set_status( code = '405' reason = 'Method Not Allowed' ).
exit.
endif.
* Find whether a specific length was requested, and override default if it was.
data: countParam type i.
countParam = 0 + server->request->GET_FORM_FIELD( name = 'count' ).
if countParam gt 0.
howMany = countParam - 1.
endif.
* Check whether caching in the ICM was requested
cacheFlag = server->request->GET_FORM_FIELD( name = 'cache' ).
if cacheFlag eq 'true'.
server->response->server_cache_expire_rel( expires_rel = '300').
endif.
* Prep.
data: innerCounter type i.
innerCounter = 1.
response_content = '1'.
* Do some actual work.
do howMany times.
* Be stupid here and start from the beginning each time to consume max. CPU.
* To be even more daft, do many string <--> num conversions
val1 = '1'.
val2 = '1'.
do innerCounter times.
sumof1and2 = '' + val1 + val2.
val1 = '' + val2.
val2 = '' + sumof1and2.
totalCounter = '' + totalCounter + 1.
enddo.
fibfragment = '' + sumof1and2.
* Add to the response payload
concatenate
response_content
CL_ABAP_CHAR_UTILITIES=>NEWLINE
fibfragment
into response_content.
innerCounter = innerCounter + 1.
enddo.
data totalCounterStr type string.
totalCounterStr = '' + totalCounter.
concatenate
response_content
CL_ABAP_CHAR_UTILITIES=>NEWLINE
CL_ABAP_CHAR_UTILITIES=>NEWLINE
`Looped a total of ` totalCounterStr `times.`
into response_content.
* Ensure we are good HTTP citizens and provide a content type.
server->response->set_header_field(
name = 'Content-Type'
value = 'text/plain; charset=utf-8' ).
* ...and finally set the response.
server->response->set_cdata( response_content ).
endmethod.
@qmacro

This comment has been minimized.

Copy link

@qmacro qmacro commented Jul 24, 2013

Nice to see native ICF methods used. Rare but pleasant sight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment