## Calculating Fibonacci numbers with CPI

One of my first university project with functional programming was to figure out how to calculate Fibonacci numbers. It is simple algorithms that recurse thru the values. It can be implemented pretty simple in a number of languages as seen on this blog.

The function is as follows.

```function F(n)
if n = 0
return 0
if n = 1
return 1
else
return F(n-1) + F(n-2)```

It is a calculation intensive process because of all the recursion happening. If for instance you want to calculate F(15) you will need to calculate F(14) and F(13), they would then require F(13) and F(12). So you will end up with a lof of function calls.

It is a good use case to use the ProcessDirect adapter and see how it performs. It is not something that you should be running on your production system because it can take up a lot of resources.

To start the process a simple HTTP to Process Direct is added to the process.

The parameter to the call is then a header value called n and this is the only allowed header.

1) We set some values and find a number for the process. We set the Application id to N as described in the blog.

2) There is a select for the value of the n and if n = 1  or n = 0, a fixed value will be returned in the result header

3) For other functions I have created a simple function for creating an XML with values n-1 and n-2 is.

```def Message processData(Message message) {
//Body
def body = message.getBody();

def nString = map.get("n");
def n = Integer.parseInt(nString);

message.setBody("<r><n>"+(n-1)+"</n><n>"+(n-2)+"</n></r>");
return message;
}```

4) There is a split on /r/n

5) For each of the two request, it will set n as header and then call process direct. The result will be placed in a XML.

6) Both messages will be gathered and XPath Sum(//result) will set the value result

There are some ways to improve it like most notably put all calculation in one groovy script. It was interesting to see how this performs.

To call it using the following command

```curl -X POST \
https://****iflmap.hcisbp.eu1.hana.ondemand.com/http/fibonacci \
-H 'n: 22'```

## Takeaways

So this is not just for fun. It can provide some insights into how many processes can be performed.

It was interesting to know if how the split works and if the parallel processing is performing better.

If I Calculated F(10) it took 2.5 seconds without parallel processing and 300 seconds when parallel processing was enabled. It will here need to have open processes for all events at the same time and will need to keep the flow open for a longer duration.  With the other, it could just calculate each from time to time. Otherwise, it just needs to calculate one by one. So there is probably a limit to how many concurrent CPI processes you can have open.

When I look at CPU and memory usage I don’t see much difference in them with the flows.

I have also calculated F(20) without parallel processing it does take 360 seconds and it is possible to see a spike in the CPU usage for the duration.

Let me know what you find.