[Lldb-commits] [lldb] r136664 - in /lldb/trunk/test: benchmarks/example/Makefile benchmarks/example/TestRepeatedExprs.py benchmarks/example/main.c benchmarks/example/main.cpp lldbbench.py

Johnny Chen johnny.chen at apple.com
Mon Aug 1 17:43:09 PDT 2011


Author: johnny
Date: Mon Aug  1 19:43:09 2011
New Revision: 136664

URL: http://llvm.org/viewvc/llvm-project?rev=136664&view=rev
Log:
Add a Stopwatch utility class to lldbench.py module and initialize an instance of
Stopwatch (self.swatch) within the BenchBase's setUp() instance method to be available
to all the child classes.

Use self.swatch to measure elapsed time in TestRepeatedExprs.py, which needs to be
modified later on to actually measure repeated expression evaluations within the
context of lldb as well as gdb.

Added:
    lldb/trunk/test/benchmarks/example/main.cpp
      - copied, changed from r136649, lldb/trunk/test/benchmarks/example/main.c
Removed:
    lldb/trunk/test/benchmarks/example/main.c
Modified:
    lldb/trunk/test/benchmarks/example/Makefile
    lldb/trunk/test/benchmarks/example/TestRepeatedExprs.py
    lldb/trunk/test/lldbbench.py

Modified: lldb/trunk/test/benchmarks/example/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/benchmarks/example/Makefile?rev=136664&r1=136663&r2=136664&view=diff
==============================================================================
--- lldb/trunk/test/benchmarks/example/Makefile (original)
+++ lldb/trunk/test/benchmarks/example/Makefile Mon Aug  1 19:43:09 2011
@@ -1,5 +1,5 @@
 LEVEL = ../../make
 
-C_SOURCES := main.c
+CXX_SOURCES := main.cpp
 
 include $(LEVEL)/Makefile.rules

Modified: lldb/trunk/test/benchmarks/example/TestRepeatedExprs.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/benchmarks/example/TestRepeatedExprs.py?rev=136664&r1=136663&r2=136664&view=diff
==============================================================================
--- lldb/trunk/test/benchmarks/example/TestRepeatedExprs.py (original)
+++ lldb/trunk/test/benchmarks/example/TestRepeatedExprs.py Mon Aug  1 19:43:09 2011
@@ -23,8 +23,11 @@
         self.run_gdb_repeated_exprs()
 
     def run_lldb_repeated_exprs(self):
-        print "running "+self.testMethodName
-        print "benchmarks result for "+self.testMethodName
+        for i in range(1000):
+            with self.swatch:
+                print "running "+self.testMethodName
+                print "benchmarks result for "+self.testMethodName
+        print "stopwatch:", str(self.swatch)
 
     def run_gdb_repeated_exprs(self):
         print "running "+self.testMethodName

Removed: lldb/trunk/test/benchmarks/example/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/benchmarks/example/main.c?rev=136663&view=auto
==============================================================================
--- lldb/trunk/test/benchmarks/example/main.c (original)
+++ lldb/trunk/test/benchmarks/example/main.c (removed)
@@ -1,6 +0,0 @@
-#include <stdio.h>
-
-int main(int argc, char const *argv[]) {
-    printf("Hello world.\n");
-    return 0;
-}

Copied: lldb/trunk/test/benchmarks/example/main.cpp (from r136649, lldb/trunk/test/benchmarks/example/main.c)
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/benchmarks/example/main.cpp?p2=lldb/trunk/test/benchmarks/example/main.cpp&p1=lldb/trunk/test/benchmarks/example/main.c&r1=136649&r2=136664&rev=136664&view=diff
==============================================================================
--- lldb/trunk/test/benchmarks/example/main.c (original)
+++ lldb/trunk/test/benchmarks/example/main.cpp Mon Aug  1 19:43:09 2011
@@ -1,6 +1,43 @@
 #include <stdio.h>
 
+class Point {
+public:
+    int x;
+    int y;
+    Point(int a, int b):
+        x(a),
+        y(b)
+    {}
+};
+
+class Data {
+public:
+    int id;
+    Point point;
+    Data(int i):
+        id(i),
+        point(0, 0)
+    {}
+};
+
 int main(int argc, char const *argv[]) {
-    printf("Hello world.\n");
+    Data *data[1000];
+    Data **ptr = data;
+    for (int i = 0; i < 1000; ++i) {
+        ptr[i] = new Data(i);
+        ptr[i]->point.x = i;
+        ptr[i]->point.y = i+1;
+    }
+
+    printf("Finished populating data.\n");
+    for (int i = 0; i < 1000; ++i) {
+        bool dump = argc > 1; // Set breakpoint here.
+                              // Evaluate a couple of expressions (2*1000 = 2000 exprs):
+                              // expr ptr[i]->point.x
+                              // expr ptr[i]->point.y
+        if (dump) {
+            printf("data[%d] = %d (%d, %d)\n", i, ptr[i]->id, ptr[i]->point.x, ptr[i]->point.y);
+        }
+    }
     return 0;
 }

Modified: lldb/trunk/test/lldbbench.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbbench.py?rev=136664&r1=136663&r2=136664&view=diff
==============================================================================
--- lldb/trunk/test/lldbbench.py (original)
+++ lldb/trunk/test/lldbbench.py Mon Aug  1 19:43:09 2011
@@ -1,8 +1,100 @@
-import lldbtest
+import time
 from lldbtest import benchmarks_test
+from lldbtest import Base
 
-class BenchBase(lldbtest.Base):
+class Stopwatch(object):
+    """Stopwatch provides a simple utility to start/stop your stopwatch multiple
+    times.  Each start/stop is equal to a lap, with its elapsed time accumulated
+    while measurment is in progress.
+
+    When you're ready to start from scratch for another round of measurements,
+    be sure to call the reset() method.
+
+    For example,
+
+    sw = Stopwatch()
+    for i in range(1000):
+        with sw:
+            # Do some length operations...
+            ...
+    # Get the average time.
+    avg_time = sw.avg()
+
+    # Reset the stopwatch as we are about to perform other kind of operations.
+    sw.reset()
+    ...
+    """
+
+    #############################################################
+    #
+    # Context manager interfaces to support the 'with' statement.
+    #
+    #############################################################
+
+    def __enter__(self):
+        """
+        Context management protocol on entry to the body of the with statement.
+        """
+        return self.start()
+
+    def __exit__(self, type, value, tb):
+        """
+        Context management protocol on exit from the body of the with statement.
+        """
+        self.stop()
+
+    def reset(self):
+        self.__laps__ = 0
+        self.__total_elapsed__ = 0.0
+        self.__start__ = None
+        self.__stop__ = None
+        self.__elapsed__ = 0.0
+
+    def __init__(self):
+        self.reset()
+
+    def start(self):
+        if self.__start__ is None:
+            self.__start__ = time.time()
+        else:
+            raise Exception("start() already called, did you forget to stop() first?")
+        # Return self to facilitate the context manager __enter__ protocol.
+        return self
+
+    def stop(self):
+        if self.__start__ is not None:
+            self.__stop__ = time.time()
+            elapsed = self.__stop__ - self.__start__
+            self.__total_elapsed__ += elapsed
+            self.__laps__ += 1
+            self.__start__ = None # Reset __start__ to be None again.
+        else:
+            raise Exception("stop() called without first start()?")
+
+    def laps(self):
+        """Gets the number of laps. One lap is equal to a start/stop action."""
+        return self.__laps__
+
+    def avg(self):
+        """Equal to total elapsed time divided by the number of laps."""
+        return self.__total_elapsed__ / self.__laps__
+
+    def __str__(self):
+        return "Avg: %f (Laps: %d, Total Elapsed Time: %d)" % (self.avg(),
+                                                               self.__laps__,
+                                                               self.__total_elapsed__)
+
+class BenchBase(Base):
     """
     Abstract base class for benchmark tests.
     """
+    def setUp(self):
+        """Fixture for unittest test case setup."""
+        Base.setUp(self)
+        self.swatch = Stopwatch()
+
+    def tearDown(self):
+        """Fixture for unittest test case teardown."""
+        Base.tearDown(self)
+        del self.swatch
 





More information about the lldb-commits mailing list