Add a benchmark for string transcoding.
authorfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 2 Mar 2016 22:30:49 +0000 (22:30 +0000)
committerfpizlo@apple.com <fpizlo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 2 Mar 2016 22:30:49 +0000 (22:30 +0000)
Rubber stamped by Saam Barati.

I wrote some code like this while working on
https://github.com/WebAssembly/design/pull/573. I thought I'd add it as a benchmark since
it stresses things that we may not have good bench coverage for.

* js/regress/script-tests/string-transcoding.js: Added.
(decodeUTF8):
(encodeUTF8):
(arraysEqual):
(arrayToString):
(setHeader):
(print):
(tryArray):
(doSteps):
* js/regress/string-transcoding-expected.txt: Added.
* js/regress/string-transcoding.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@197465 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/js/regress/script-tests/string-transcoding.js [new file with mode: 0644]
LayoutTests/js/regress/string-transcoding-expected.txt [new file with mode: 0644]
LayoutTests/js/regress/string-transcoding.html [new file with mode: 0644]

index 7127927..4fd7593 100644 (file)
@@ -1,3 +1,25 @@
+2016-03-02  Filip Pizlo  <fpizlo@apple.com>
+
+        Add a benchmark for string transcoding.
+
+        Rubber stamped by Saam Barati.
+
+        I wrote some code like this while working on
+        https://github.com/WebAssembly/design/pull/573. I thought I'd add it as a benchmark since
+        it stresses things that we may not have good bench coverage for.
+
+        * js/regress/script-tests/string-transcoding.js: Added.
+        (decodeUTF8):
+        (encodeUTF8):
+        (arraysEqual):
+        (arrayToString):
+        (setHeader):
+        (print):
+        (tryArray):
+        (doSteps):
+        * js/regress/string-transcoding-expected.txt: Added.
+        * js/regress/string-transcoding.html: Added.
+
 2016-03-02  Dave Hyatt  <hyatt@apple.com>
 
         Add support for the "first" value of the hanging-punctuation property.
diff --git a/LayoutTests/js/regress/script-tests/string-transcoding.js b/LayoutTests/js/regress/script-tests/string-transcoding.js
new file mode 100644 (file)
index 0000000..6a51b6f
--- /dev/null
@@ -0,0 +1,81 @@
+function decodeUTF8(array) {
+    var string = "";
+    for (var i = 0; i < array.length; ++i)
+        string += String.fromCharCode(array[i]);
+    return decodeURIComponent(escape(string));
+}
+
+function encodeUTF8(string) {
+    string = unescape(encodeURIComponent(string));
+
+    var array = new Uint8Array(string.length);
+    for (var i = 0; i < array.length; ++i)
+        array[i] = string.charCodeAt(i);
+    return array;
+}
+
+function arraysEqual(a, b) {
+    if (a.length != b.length)
+        return false;
+    for (var i = 0; i < a.length; ++i) {
+        if (a[i] != b[i])
+            return false;
+    }
+    return true;
+}
+
+function arrayToString(array) {
+    return "[" + Array.prototype.join.call(array, ", ") + "]";
+}
+
+function setHeader(s) {
+}
+
+function print(s) {
+    document.getElementById("console").innerHTML += "<br/>" + s;
+}
+
+function tryArray(array) {
+    try {
+        var string = decodeUTF8(array);
+        try {
+            var array2 = encodeUTF8(string);
+            if (!arraysEqual(array, array2)) {
+                print("Round trip failed: " + arrayToString(array) + " turned into " + arrayToString(array2));
+                return;
+            }
+        } catch (e) {
+            print("Threw exception in encode for: " + arrayToString(array));
+            return;
+        }
+    } catch (e) {
+        return;
+    }
+}
+
+var array = new Uint8Array(5);
+
+function doSteps(numSteps) {
+    while (numSteps--) {
+        tryArray(array);
+
+        var done = false;
+        array[0]++;
+        for (var i = 0; i < array.length; ++i) {
+            if (array[i])
+                break;
+            if (i + 1 == array.length) {
+                done = true;
+                break;
+            }
+            array[i + 1]++;
+        }
+
+        if (done)
+            return false;
+    }
+
+    return true;
+}
+
+doSteps(5000);
diff --git a/LayoutTests/js/regress/string-transcoding-expected.txt b/LayoutTests/js/regress/string-transcoding-expected.txt
new file mode 100644 (file)
index 0000000..7997137
--- /dev/null
@@ -0,0 +1,10 @@
+JSRegress/string-transcoding
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS no exception thrown
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/js/regress/string-transcoding.html b/LayoutTests/js/regress/string-transcoding.html
new file mode 100644 (file)
index 0000000..c3b130f
--- /dev/null
@@ -0,0 +1,12 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src="../../resources/js-test-pre.js"></script>
+</head>
+<body>
+<script src="../../resources/regress-pre.js"></script>
+<script src="script-tests/string-transcoding.js"></script>
+<script src="../../resources/regress-post.js"></script>
+<script src="../../resources/js-test-post.js"></script>
+</body>
+</html>