Labels

Sunday, May 3, 2009

Programming Erlang - Exercise 8.11.2 Benchmark Statistics

Programming Erlang - Chapter 8 Problems: BENCHMARK results
My contribution to the discussion for Programming Erlang - Chapter 8 Problems
( http://forums.pragprog.com/forums/27/topics/311 ).

Not very long ago, having some spare time, I decided to read the intriguing Joe Armstrong’s book and, after a long time, I started to do some programming completing book’s exercises. As Joe said, programming is in fact experimental discipline and without trying to do some programming one can’t understand the potential of some programming language. Why I choose Erlang I really don’t know. I stumbled on the Pragmatic Bookshelf website and I was attracted to the apparently elegant programming language and decided to give it a try. Getting more involved I completed the Chapter 8 Exercises. The result was MM#1 program ( “c8p2m_t1” ), my first version of the solution.
As I’m a kind of picky guy I started with the precise definition of a ring abstraction; according to my understanding a ring is an abstraction which is created by linking the N parallel processes so that each process is aware ONLY of its successor. Additionally, each ring process should have the quite precisely known and well defined functionality, fully controlled and created by a programmer. I have the opinion that it is not mandatory to create a ring, as the abstraction, from N IDENTICAL parallel processes (like in my first version of the code – MM#1) but for me it was the easiest way to solve the Problem 2. Following Joe’s advice to write a blog about test results, I was looking for the other published solutions, of other people who were following Joe’s advice and wanted to share their experience. That is how I spotted discussion in the Pragmatic Bookshelf website and started to analyze and compare posted solutions with my own solution to the problem, presented here as MM#1 (“c8p2m_t1”).

First solution (by Christopher Atkins) does not correctly ping the message around the ring (“Caller” in the loop/1 seems to be always Erlang shell Pid and ping doesn’t circle around the created ring of N spawned processes).

The second solution (by Nico Verwer) correctly ping a message around the ring, but the ring is not created in compliance with my definition of a ring abstraction. N-1 identical parallel processes [represented by the proc()] are linked to the control function main() [almost identical to the proc()in functionality], running as a part of the Erlang Shell, representing the ring start process in the Nico’s code. The choice of parallel processors linked in a ring is a question of freedom but, there is one important difference in Nico’s choice for the control process, violating my definition of a ring abstraction, and, according to my opinion, that represents a valid objection that Nico’s code did NOT create a valid ring abstraction. To be more specific, the Erlang Shell is a process, running in parallel with other spawned processes, but, with significantly more hidden functionality than other N-1 parallel processes. The main() function in the shell is just a small part of that complex process, enveloping quite a few system processes [ i() provides more information in that respect]. In that respect the Erlang Shell is NOT controlled by the programmer and from my point of view, can NOT be a part of the ring abstraction, which should be completely created by the programmer.
On the other side, Nico’s code gave me the idea that I can create the second version of the code MM#2 ( “c8p2m_t2” ) where I created one asymmetrical process, which has the control role in the ring.

I decided to test the efficiency of three different solutions ( Nico’s code: NV, MM#1 and MM#2) under the same controlled conditions.
Environment:
AMD Turion(tm) 64 , 1590 MHz Mobile Technology ML-28, 2GB Memory
MS XP Home Edition SP-3 with standard convoluted MS mixture of OS services with disabled internet connection, antivirus and other startup programs and Erlang (BEAM) emulator version 5.6.5.

Each experiment is using the same number of ping messages, doubling the ring size and dividing in half the number of circles in each measurement (except in the last case because of limitations regarding the max number of parallel processes).



The representative metrics is the measured average ping message elapsed time!
The graphical results are represented in this chart:


Discussion of results:
The code MM#2 is showing the best results, almost 10% better than the next contender NV. The MM#1 is showing the worst results, 20% worse than NV. If we analyze the code then we can see that the main difference between the MM#2 and NV on one side, and the MM#1 on the other side, is the fact that the algorithms in the first group (MM#2 and NV) don’t perform same number of arithmetic operations (increasing the counter representing the current number of executed pings and corresponding tests to decide when to stop pinging) as the MM#1 algorithm. In the former case, this number is the number of circles where in the later case, it is the total number of pings; a difference between the MM#2 and NV, and 10% greater efficiency of the MM#2 algorithm is probably due to the difference between created control process in MM#2 case vs. Erlang shell hidden processing and its hidden overhead (the control process in the NV case). These results are increasing my preference toward the precise definition of a ring abstraction. On the other side, algorithm in the MM#1 has its advantage in its flexibility to implement a change in the behavior of the ring abstraction. Each ring process in MM#1 is and can be the control process, what is not the case in MM#2 and NV algorithms. If, for example, we want to change the program to stop after arbitrary number of ping messages, it’s easy to do that in the MM#1 case. It would be a challenge to implement that change in behavior in the MM#2 and NV, because the algorithm in these cases does not depend on a ring size, what is on the other side the main reason why these algorithms are “more efficient”. As the “efficiency” can be improved other ways (creation of the ring abstraction over independent nodes …..), and we still measure the efficiency with the average transaction response time, the clear advantage of independent “intelligent” components of a ring abstraction in MM#1 becomes attractive advantage.

No comments:

Post a Comment