[pypy-commit] cffi static-callback-embedding: Pseudo-tests that print some performance numbers for calling an embedded

arigo pypy.commits at gmail.com
Sat Jan 2 09:33:22 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: static-callback-embedding
Changeset: r2516:63c49bc07ecc
Date: 2016-01-02 15:33 +0100
http://bitbucket.org/cffi/cffi/changeset/63c49bc07ecc/

Log:	Pseudo-tests that print some performance numbers for calling an
	embedded "extern Python" function in a loop, with or without
	threads.

diff --git a/testing/embedding/perf-test.c b/testing/embedding/perf-test.c
new file mode 100644
--- /dev/null
+++ b/testing/embedding/perf-test.c
@@ -0,0 +1,86 @@
+#include <stdio.h>
+#include <assert.h>
+#include <sys/time.h>
+#ifdef PTEST_USE_THREAD
+# include <pthread.h>
+# include <semaphore.h>
+static sem_t done;
+#endif
+
+
+extern int add1(int, int);
+
+
+static double time_delta(struct timeval *stop, struct timeval *start)
+{
+    return (stop->tv_sec - start->tv_sec) +
+        1e-6 * (stop->tv_usec - start->tv_usec);
+}
+
+static double measure(void)
+{
+    long long i, iterations;
+    int result;
+    struct timeval start, stop;
+    double elapsed;
+
+    add1(0, 0);   /* prepare off-line */
+
+    i = 0;
+    iterations = 1000;
+    result = gettimeofday(&start, NULL);
+    assert(result == 0);
+
+    while (1) {
+        for (; i < iterations; i++) {
+            add1(((int)i) & 0xaaaaaa, ((int)i) & 0x555555);
+        }
+        result = gettimeofday(&stop, NULL);
+        assert(result == 0);
+
+        elapsed = time_delta(&stop, &start);
+        assert(elapsed >= 0.0);
+        if (elapsed > 2.5)
+            break;
+        iterations = iterations * 3 / 2;
+    }
+
+    return elapsed / (double)iterations;
+}
+
+static void *start_routine(void *arg)
+{
+    double t = measure();
+    printf("time per call: %.3g\n", t);
+
+#ifdef PTEST_USE_THREAD
+    int status = sem_post(&done);
+    assert(status == 0);
+#endif
+
+    return arg;
+}
+
+
+int main(void)
+{
+#ifndef PTEST_USE_THREAD
+    start_routine(0);
+#else
+    pthread_t th;
+    int i, status = sem_init(&done, 0, 0);
+    assert(status == 0);
+
+    add1(0, 0);   /* this is the main thread */
+
+    for (i = 0; i < PTEST_USE_THREAD; i++) {
+        status = pthread_create(&th, NULL, start_routine, NULL);
+        assert(status == 0);
+    }
+    for (i = 0; i < PTEST_USE_THREAD; i++) {
+        status = sem_wait(&done);
+        assert(status == 0);
+    }
+#endif
+    return 0;
+}
diff --git a/testing/embedding/perf.py b/testing/embedding/perf.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/perf.py
@@ -0,0 +1,18 @@
+import cffi
+
+ffi = cffi.FFI()
+
+ffi.cdef("""
+    extern "Python" int add1(int, int);
+""", dllexport=True)
+
+ffi.embedding_init_code(r"""
+    @ffi.def_extern()
+    def add1(x, y):
+        return x + y
+""")
+
+ffi.set_source("_perf_cffi", """
+""")
+
+ffi.compile(verbose=True)
diff --git a/testing/embedding/test_performance.py b/testing/embedding/test_performance.py
new file mode 100644
--- /dev/null
+++ b/testing/embedding/test_performance.py
@@ -0,0 +1,47 @@
+from testing.embedding.test_basic import EmbeddingTests
+
+
+class TestPerformance(EmbeddingTests):
+    def test_perf_single_threaded(self):
+        self.prepare_module('perf')
+        self.compile('perf-test', ['_perf_cffi'], ['-O2'])
+        output = self.execute('perf-test')
+        print '='*79
+        print output.rstrip()
+        print '='*79
+
+    def test_perf_in_1_thread(self):
+        self.prepare_module('perf')
+        self.compile('perf-test', ['_perf_cffi'],
+                     ['-pthread', '-O2', '-DPTEST_USE_THREAD=1'])
+        output = self.execute('perf-test')
+        print '='*79
+        print output.rstrip()
+        print '='*79
+
+    def test_perf_in_2_threads(self):
+        self.prepare_module('perf')
+        self.compile('perf-test', ['_perf_cffi'],
+                     ['-pthread', '-O2', '-DPTEST_USE_THREAD=2'])
+        output = self.execute('perf-test')
+        print '='*79
+        print output.rstrip()
+        print '='*79
+
+    def test_perf_in_4_threads(self):
+        self.prepare_module('perf')
+        self.compile('perf-test', ['_perf_cffi'],
+                     ['-pthread', '-O2', '-DPTEST_USE_THREAD=4'])
+        output = self.execute('perf-test')
+        print '='*79
+        print output.rstrip()
+        print '='*79
+
+    def test_perf_in_8_threads(self):
+        self.prepare_module('perf')
+        self.compile('perf-test', ['_perf_cffi'],
+                     ['-pthread', '-O2', '-DPTEST_USE_THREAD=8'])
+        output = self.execute('perf-test')
+        print '='*79
+        print output.rstrip()
+        print '='*79


More information about the pypy-commit mailing list