Added latency & memory usage test 61/183661/6
authorMonika Zielinska <m.zielinska3@samsung.com>
Mon, 9 Jul 2018 13:24:05 +0000 (15:24 +0200)
committerMonika Zielinska <m.zielinska3@samsung.com>
Wed, 25 Jul 2018 07:07:17 +0000 (09:07 +0200)
Change-Id: I2daf0306bce9468b397fa78f2f5a8437c5bd8389

Makefile.am
src/stest_memory.c [new file with mode: 0644]

index b2e5f02..12b53f3 100644 (file)
@@ -116,18 +116,20 @@ libdbuspolicy_tests_SOURCES = src/test_runner.c
 runnerdir = ${libdir}/dbus-tests/runner/
 
 alonetestdir = ${libdir}/dbus-tests/test-suites/libdbuspolicy-tests/
-alonetest_PROGRAMS = dbus_daemon stest_ownership stest_method_call stest_signal stest_cynara
+alonetest_PROGRAMS = dbus_daemon stest_ownership stest_method_call stest_signal stest_cynara stest_memory
 
 dbus_daemon_SOURCES = src/dbus_daemon.c
 stest_ownership_SOURCES = src/stest_ownership.c src/stest_common.c
 stest_method_call_SOURCES = src/stest_method_call.c src/stest_common.c
 stest_signal_SOURCES = src/stest_signal.c src/stest_common.c
 stest_cynara_SOURCES = src/stest_cynara.c src/stest_common.c
+stest_memory_SOURCES = src/stest_memory.c
 
 stest_ownership_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS)
 stest_method_call_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS)
 stest_signal_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS)
 stest_cynara_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS)
+stest_memory_LDADD = src/libinternalfortests.a -lexpat -lstdc++ $(CYNARA_LIBS)
 
 all-tests:: $(alonetest_PROGRAMS) $(runner_PROGRAMS)
 endif
diff --git a/src/stest_memory.c b/src/stest_memory.c
new file mode 100644 (file)
index 0000000..e20fe24
--- /dev/null
@@ -0,0 +1,90 @@
+/* Author: Monika ZieliƄska */
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+#include <dbuspolicy1/libdbuspolicy1.h>
+
+#define STATUS_FILE_PATH "/proc/self/statm"
+#define LINE_LEN 64
+
+static int parse_line(char *line, char type)
+{
+       assert(line != NULL);
+       assert(type == 'r' || type == 't');
+
+       if (type == 'r') //RSS
+               while (*line >= '0' && *line <= '9')
+                       line++;
+       return atoi(line);
+}
+
+static int get_usage(int *total, int *rss)
+{
+       FILE *fp;
+       char line[LINE_LEN], *p;
+
+       assert(total != NULL);
+       assert(rss != NULL);
+
+       fp = fopen(STATUS_FILE_PATH, "r");
+       if (fp == NULL)
+               return -1;
+
+       if ((p = fgets(line, sizeof(line), fp)) != NULL) {
+               *total = parse_line(line, 't');
+               *rss = parse_line(line, 'r');
+               fclose(fp);
+               return 0;
+       }
+
+       fclose(fp);
+       return -1;
+}
+
+static void get_time_since(struct timespec *begin, struct timespec *res)
+{
+       struct timespec now;
+
+       assert(begin != NULL);
+       assert(res != NULL);
+
+       clock_gettime(CLOCK_MONOTONIC, &now);
+       res->tv_sec = now.tv_sec - begin->tv_sec;
+       res->tv_nsec = now.tv_nsec - begin->tv_nsec;
+
+       if (res->tv_nsec < 0) {
+               res->tv_nsec += 1000000000;
+               res->tv_sec -= 1;
+       }
+}
+
+int main(void)
+{
+       int total_before, rss_before, total_after, rss_after, res, page;
+       void *policy;
+       struct timespec begin, elapsed;
+
+       res = get_usage(&total_before, &rss_before);
+       assert(res != -1);
+       clock_gettime(CLOCK_MONOTONIC, &begin);
+
+       policy = dbuspolicy1_init("/sys/fs/kdbus/0-system/bus");
+       assert(policy != NULL);
+
+       get_time_since(&begin, &elapsed);
+       res = get_usage(&total_after, &rss_after);
+       assert(res != -1);
+
+       page = getpagesize();
+
+       printf("Total memory change: %d B\n", (total_after - total_before) * page);
+       printf("RSS change: %d B\n", (rss_after - rss_before) * page);
+       printf("Time: %lld.%.9ld s\n", (long long)elapsed.tv_sec, elapsed.tv_nsec);
+
+       dbuspolicy1_free(policy);
+       return 0;
+}