CSC/ECE 517 Fall 2007/wiki1b 5 b4: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:


==Example: Profiler Aspect==
==Example: Profiler Aspect==
One common use of AOP is profiling.  Profiling refers to the collection of data about the dynamic behavior of a system.  It is done to determine a breakdown of where time and memory are being consumed and make optimizations based on the results.  In this example, we create an aspect that will determine the number of (milli)seconds spent in a single method.  It is kept very simple to clearly illustrate the functionality of AspectR; a full-featured profiler would need to account for non-serialized and nested method invocations, and would need to aggregate the results, for example.
One common use of AOP is profiling.  Profiling refers to the collection of data about the dynamic behavior of a system.  It is done to determine a breakdown of where time and memory are being consumed and make optimizations based on the results.  In the example below, we create an aspect that will determine the number of (milli)seconds spent in a single method.  It is kept very simple to clearly illustrate the functionality of AspectR; a full-featured profiler would need to account for non-serialized and nested method invocations, aggregate the results, etc.
 
The Profiler class contains two methods: profiler_enter and profiler_exit.  The before advice, profiler_enter, records the time right before the method is invoked.  The after advice, profiler_exit, records the time just after the method exits and prints out the duration of the method invocation.


<pre>
<pre>

Revision as of 01:46, 1 October 2007

Hold for Robin Yehle Wiki Assignment 1b AspectR is a very useful Ruby module, but it is not easy to find documentation on it that is appropriate for students taking this class. Find, or construct, documentation that explains what it does without presuming previous knowledge of AspectJ, that describes many or all methods of the module and how they work. Also find or produce an easy-to-understand example that does not involve logging.

Example: Profiler Aspect

One common use of AOP is profiling. Profiling refers to the collection of data about the dynamic behavior of a system. It is done to determine a breakdown of where time and memory are being consumed and make optimizations based on the results. In the example below, we create an aspect that will determine the number of (milli)seconds spent in a single method. It is kept very simple to clearly illustrate the functionality of AspectR; a full-featured profiler would need to account for non-serialized and nested method invocations, aggregate the results, etc.

The Profiler class contains two methods: profiler_enter and profiler_exit. The before advice, profiler_enter, records the time right before the method is invoked. The after advice, profiler_exit, records the time just after the method exits and prints out the duration of the method invocation.

require 'aspectr'
include AspectR
class Profiler < Aspect
   
  def profiler_enter(method, object, exitstatus, *args) 
    @enterTime = Time.now
    puts "#{@enterTime.strftime('%Y-%m-%d %X')} #{self.class}##{method}: #{args.inspect}" 
   end
   
  def profiler_exit(method, object, exitstatus, *args) 
    @exitTime = Time.now
    print "#{@exitTime.strftime('%Y-%m-%d %X')} #{self.class}##{method}: exited " 
    if exitstatus.kind_of?(Array) 
      print "normally returning #{exitstatus[0].inspect} " 
    elsif exitstatus == true 
      print "with exception '#{$!}' " 
    else 
      print "normally " 
    end 
    puts "after #{@exitTime.to_f - @enterTime.to_f} seconds"
  end
end