aboutsummaryrefslogtreecommitdiff
path: root/jerry-main
diff options
context:
space:
mode:
authorAkos Kiss <akiss@inf.u-szeged.hu>2017-11-16 12:36:58 +0100
committerDániel Bátyai <dbatyai@inf.u-szeged.hu>2017-11-16 12:36:58 +0100
commita0db3ee5b302a6574b229cc807869afe7107c872 (patch)
treeced7c1f0a1ef278a717164e3eb8a026184ef6d66 /jerry-main
parent7692aa9d660a3ecf4d4772bdb8b426ac85b6a98d (diff)
Ensure that the test version of the command line tool is stable for benchmarking (#2076)
Some benchmark suites contain test cases that have nonreproducible behaviour. This is mostly caused by relying on "now" when dealing with dates or timestamps, instead of using a fixed moment. (A notorious example is the crypto-aes.js test case of the sunspider bechmark suite, where the heap memory consumption can vary between 34K-41K heap because of using `(new Date()).getTime()`.) This commit renames the jerry-minimal command line tool to jerry-test (to better reflect its purpose) and adds extra code, which intercepts some calls to libc (`gettimeofday`, `rand`) and pins their results to some fixed values. This makes the tool useless in a general case but ensures stable results when benchmarking -- for which it is mostly used. As a side effect, the commit also changes jerry-libc by making all libc functions weak symbols to allow their override from application code. JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
Diffstat (limited to 'jerry-main')
-rw-r--r--jerry-main/CMakeLists.txt6
-rw-r--r--jerry-main/benchmarking.c63
-rw-r--r--jerry-main/main-unix-test.c (renamed from jerry-main/main-unix-minimal.c)0
3 files changed, 66 insertions, 3 deletions
diff --git a/jerry-main/CMakeLists.txt b/jerry-main/CMakeLists.txt
index bffe1f63..f7dcfc75 100644
--- a/jerry-main/CMakeLists.txt
+++ b/jerry-main/CMakeLists.txt
@@ -64,9 +64,9 @@ if(JERRY_CMDLINE)
target_link_libraries("jerry" jerry-ext jerry-port-default)
endif()
-if(JERRY_CMDLINE_MINIMAL)
- jerry_create_executable("jerry-minimal" "main-unix-minimal.c")
- target_link_libraries("jerry-minimal" jerry-port-default-minimal)
+if(JERRY_CMDLINE_TEST)
+ jerry_create_executable("jerry-test" "main-unix-test.c" "benchmarking.c")
+ target_link_libraries("jerry-test" jerry-port-default-minimal)
endif()
if(JERRY_CMDLINE_SNAPSHOT)
diff --git a/jerry-main/benchmarking.c b/jerry-main/benchmarking.c
new file mode 100644
index 00000000..0e8e151a
--- /dev/null
+++ b/jerry-main/benchmarking.c
@@ -0,0 +1,63 @@
+/* Copyright JS Foundation and other contributors, http://js.foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * This source contains libc overrides for the sake of stable benchmarking. If
+ * building a binary for the purpose of benchmarking, the object compiled from
+ * this source is to be injected into the list of objects-to-be-linked before
+ * the list of libraries-to-be-linked to ensure that the linker picks up these
+ * implementations.
+ */
+
+ #ifdef __GNUC__
+/*
+ * Note:
+ * This is nasty and dangerous. However, we only need the timeval structure
+ * from sys/time.h. Unfortunately, the same header also declares
+ * gettimeofday, which has different declarations on different platforms
+ * (e.g., macOS, Linux). So, instead of #ifdef'ing for platforms, we simply
+ * tweak the header to declare another function. Don't try this at home.
+ */
+#define gettimeofday __prevent_conflicting_gettimeofday_declarations__
+#include <sys/time.h>
+#undef gettimeofday
+
+int gettimeofday (struct timeval *, void *);
+
+/**
+ * Useless but stable gettimeofday implementation. Returns Epoch. Ensures that
+ * test cases relying on "now" yield stable results.
+ */
+int gettimeofday (struct timeval *tv,
+ void *tz)
+{
+ (void) tz;
+ tv->tv_sec = 0;
+ tv->tv_usec = 0;
+ return 0;
+} /* gettimeofday */
+#endif /* __GNUC__ */
+
+
+int rand (void);
+
+/**
+ * Useless but stable rand implementation. Returns 4. Ensures that test cases
+ * relying on randomness yield stable results.
+ */
+int rand (void)
+{
+ return 4; /* Chosen by fair dice roll. Guaranteed to be random. */
+} /* rand */
diff --git a/jerry-main/main-unix-minimal.c b/jerry-main/main-unix-test.c
index 1fe10fdc..1fe10fdc 100644
--- a/jerry-main/main-unix-minimal.c
+++ b/jerry-main/main-unix-test.c