17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

359<br />

{{profile_ex,'-run/0-fun-1-',0},1},<br />

{{profile_ex,'-run/0-fun-0-',0},1}]}<br />

6> cprof:stop().<br />

5380<br />

7><br />

I really like this tool because it is so simple <strong>and</strong> easy to use. At code annotation #1 cprof<br />

is started. No special compilation, you need do noth<strong>in</strong>g <strong>in</strong> the code be<strong>in</strong>g counted to enable<br />

it. This makes it a very useful tool. Many times you will just be <strong>in</strong>terested <strong>in</strong> the counts of a<br />

specific module <strong>in</strong>stead of the system as a whole. Cprof allows us to do this if we just call<br />

start with the specific name of the module to be profiled.<br />

2> cprof:start(profile_ex).<br />

10<br />

This is advisable for further limit<strong>in</strong>g the impact of the analysis. Either way the next step<br />

we take at code annotation #2 is to pause cprof caus<strong>in</strong>g it to stop count<strong>in</strong>g what ever it is<br />

that we were measur<strong>in</strong>g which <strong>in</strong> this case was the call to profile_ex:run/1. Code annotation<br />

#3 is the call to analysis which dumps the output to the term<strong>in</strong>al as fairly readable erlang<br />

term. We can see from our term that the results are much as expected. We see the full<br />

1000 calls to both looper <strong>and</strong> funner. Notice though that the <strong>in</strong>teger_to_list/1 calls don’t<br />

show up. The reason for that is that because cprof uses breakpo<strong>in</strong>ts to collect counts only<br />

beam code can be evaluated. BIFs are built right <strong>in</strong>to the emulator, are not beam code, <strong>and</strong><br />

therefore can’t be counted. cprof is a simple, easy way to f<strong>in</strong>d out what functions are be<strong>in</strong>g<br />

called <strong>and</strong> how many times, noth<strong>in</strong>g more noth<strong>in</strong>g less.<br />

There are other related tools to look at, such as cover for code coverage <strong>and</strong> the<br />

<strong>in</strong>strument module for do<strong>in</strong>g memory usage analysis but <strong>in</strong> general their usage for do<strong>in</strong>g<br />

performance tun<strong>in</strong>g is less common. With these two tools <strong>in</strong> your arsenal you have a really<br />

nice foot<strong>in</strong>g for do<strong>in</strong>g some very powerful performance analysis on your systems. This will<br />

allow you to optimize the way your processes, functions, types <strong>and</strong> operators are used. This<br />

allows you to measure which performance related refactor<strong>in</strong>gs were positive <strong>and</strong> which were<br />

not. Know<strong>in</strong>g what works <strong>and</strong> what does not out of a list of options for implementation is the<br />

best practical tool you have for shav<strong>in</strong>g off microseconds, milliseconds, <strong>and</strong> depend<strong>in</strong>g on the<br />

situation even m<strong>in</strong>utes or hours off execution times so that you can reach your performance<br />

goals.<br />

We now have tools <strong>in</strong> our arsenal that allow for performance profil<strong>in</strong>g to be done. The<br />

other part of step three<br />

“Profile code <strong>and</strong> refactor bottlenecks”<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!