OSDN Git Service

Add a simple performance test for IndirectRefTable.
authorJeff Brown <jeffbrown@google.com>
Thu, 27 Oct 2011 01:41:12 +0000 (18:41 -0700)
committerJeff Brown <jeffbrown@google.com>
Thu, 27 Oct 2011 20:03:41 +0000 (13:03 -0700)
Change-Id: Ib75354508fff1921d8d73eba5e3a3905a362b1e1

vm/test/TestIndirectRefTable.cpp

index 1e462e7..4f9b30b 100644 (file)
 #include "Dalvik.h"
 
 #include <stdlib.h>
+#include <sys/time.h>
 
 #ifndef NDEBUG
 
 #define DBUG_MSG    LOGI
 
+class Stopwatch {
+public:
+    Stopwatch() {
+        reset();
+    }
+
+    void reset() {
+        start_ = now();
+    }
+
+    float elapsedSeconds() {
+        return (now() - start_) * 0.000001f;
+    }
+
+private:
+    u8 start_;
+
+    static u8 now() {
+#ifdef HAVE_POSIX_CLOCKS
+        struct timespec tm;
+        clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tm);
+        return tm.tv_sec * 1000000LL + tm.tv_nsec / 1000;
+#else
+        struct timeval tv;
+        gettimeofday(&tv, NULL);
+        return tv.tv_sec * 1000000LL + tv.tv_usec;
+#endif
+    }
+};
+
 /*
  * Basic add/get/delete tests in an unsegmented table.
  */
@@ -310,6 +341,66 @@ bail:
     return result;
 }
 
+static bool performanceTest()
+{
+    static const int kTableMax = 100;
+    IndirectRefTable irt;
+    IndirectRef manyRefs[kTableMax];
+    ClassObject* clazz = dvmFindClass("Ljava/lang/Object;", NULL);
+    Object* obj0 = dvmAllocObject(clazz, ALLOC_DONT_TRACK);
+    const u4 cookie = IRT_FIRST_SEGMENT;
+    const int kLoops = 100000;
+    Stopwatch stopwatch;
+
+    DBUG_MSG("+++ START performance\n");
+
+    if (!irt.init(kTableMax, kTableMax, kIndirectKindGlobal)) {
+        return false;
+    }
+
+    stopwatch.reset();
+    for (int loop = 0; loop < kLoops; loop++) {
+        for (int i = 0; i < kTableMax; i++) {
+            manyRefs[i] = irt.add(cookie, obj0);
+        }
+        for (int i = 0; i < kTableMax; i++) {
+            irt.remove(cookie, manyRefs[i]);
+        }
+    }
+    DBUG_MSG("Add/remove %d objects FIFO order, %d iterations, %0.3fms / iteration",
+            kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000 / kLoops);
+
+    stopwatch.reset();
+    for (int loop = 0; loop < kLoops; loop++) {
+        for (int i = 0; i < kTableMax; i++) {
+            manyRefs[i] = irt.add(cookie, obj0);
+        }
+        for (int i = kTableMax; i-- > 0; ) {
+            irt.remove(cookie, manyRefs[i]);
+        }
+    }
+    DBUG_MSG("Add/remove %d objects LIFO order, %d iterations, %0.3fms / iteration",
+            kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000  / kLoops);
+
+    for (int i = 0; i < kTableMax; i++) {
+        manyRefs[i] = irt.add(cookie, obj0);
+    }
+    stopwatch.reset();
+    for (int loop = 0; loop < kLoops; loop++) {
+        for (int i = 0; i < kTableMax; i++) {
+            irt.get(manyRefs[i]);
+        }
+    }
+    DBUG_MSG("Get %d objects, %d iterations, %0.3fms / iteration",
+            kTableMax, kLoops, stopwatch.elapsedSeconds() * 1000  / kLoops);
+    for (int i = kTableMax; i-- > 0; ) {
+        irt.remove(cookie, manyRefs[i]);
+    }
+
+    irt.destroy();
+    return true;
+}
+
 /*
  * Some quick tests.
  */
@@ -320,6 +411,11 @@ bool dvmTestIndirectRefTable()
         return false;
     }
 
+    if (!performanceTest()) {
+        LOGE("IRT performance test failed");
+        return false;
+    }
+
     return true;
 }