1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<html>
<head>
<title>ArrayBuffer External Memory test</title>
<script>
var log;
function print(message, color)
{
var paragraph = document.createElement("div");
paragraph.appendChild(document.createTextNode(message));
paragraph.style.fontFamily = "monospace";
if (color)
paragraph.style.color = color;
log.appendChild(paragraph);
}
function pass(msg)
{
print("PASS: " + msg, "green");
}
function fail(msg)
{
print("FAIL: " + msg, "red");
}
var KB = 1024;
var MB = KB * KB;
var noise = KB;
function externalMemory() {
return getV8Statistics().amount_of_external_allocated_memory;
}
function collectGarbage() {
for (var i = 0; i < 10; i++) gc();
}
function allocationsThatIncreaseExternalMemory() {
function test(expression) {
var before = externalMemory();
(function () { eval(expression); }) ();
var now = externalMemory();
if (now < before + MB - noise) {
fail(expression + " did not increase the amount of external memory (" +
before + ", " + now + ").");
} else {
pass(expression + " increased the amount of external memory.");
}
collectGarbage();
var after = externalMemory();
if (after > now + noise) {
fail("Garbage collection after " + expression +
" did not return the amount of external memory to the initial value (" +
now + ", " + after + ").");
} else {
pass("Garbage collection after " + expression +
" returned the amount of external memory to the initial value.");
}
}
test("(new ArrayBuffer(MB))");
test("(new Float32Array(MB))");
test("(new Float64Array(MB))");
test("(new Int8Array(MB))");
test("(new Int16Array(MB))");
test("(new Int32Array(MB))");
test("(new Uint8Array(MB))");
test("(new Uint16Array(MB))");
test("(new Uint32Array(MB))");
var largeJSArray = [];
for (var i = 0; i < MB; i++) largeJSArray.push(i);
test("(new Float32Array(largeJSArray))");
test("(new Float64Array(largeJSArray))");
test("(new Int8Array(largeJSArray))");
test("(new Int16Array(largeJSArray))");
test("(new Int32Array(largeJSArray))");
test("(new Uint8Array(largeJSArray))");
test("(new Uint16Array(largeJSArray))");
test("(new Uint32Array(largeJSArray))");
var int8Array = new Int8Array(MB);
test("(new Float32Array(int8Array))");
test("(new Float64Array(int8Array))");
test("(new Int8Array(int8Array))");
test("(new Int16Array(int8Array))");
test("(new Int32Array(int8Array))");
test("(new Uint8Array(int8Array))");
test("(new Uint16Array(int8Array))");
test("(new Uint32Array(int8Array))");
}
function allocationsThatDoNotChangeExternalMemory() {
function test(expression) {
var before = externalMemory();
(function () { eval(expression); }) ();
var now = externalMemory();
if (now > before + noise) {
fail(expression + " increased the amount of external memory (" + before + ", " + now + ").");
} else {
pass(expression + " did not increase the amount of external memory.");
}
collectGarbage();
var after = externalMemory();
if (after < now - noise) {
fail("Garbage collection after " + expression + " decreased the amount of external memory (" +
now + ", " + after + ").");
} else {
pass("Garbage collection after " + expression +
" did not decrease the amount of external memory.");
}
}
var arrayBuffer = new ArrayBuffer(MB);
test("(new Float32Array(arrayBuffer))");
test("(new Float64Array(arrayBuffer))");
test("(new Int8Array(arrayBuffer))");
test("(new Int16Array(arrayBuffer))");
test("(new Int32Array(arrayBuffer))");
test("(new Uint8Array(arrayBuffer))");
test("(new Uint16Array(arrayBuffer))");
test("(new Uint32Array(arrayBuffer))");
var int8Array = new Int8Array(MB);
test("(new Float32Array(int8Array.buffer))");
test("(new Float64Array(int8Array.buffer))");
test("(new Int8Array(int8Array.buffer))");
test("(new Int16Array(int8Array.buffer))");
test("(new Int32Array(int8Array.buffer))");
test("(new Uint8Array(int8Array.buffer))");
test("(new Uint16Array(int8Array.buffer))");
test("(new Uint32Array(int8Array.buffer))");
}
function transfersThatDecreaseExternalMemory() {
var workerSource =
"function externalMemory() {\n" +
" return getV8Statistics().amount_of_external_allocated_memory;\n" +
"}\n" +
"var KB = 1024;\n" +
"var MB = KB * KB;\n" +
"var noise = KB;\n" +
"self.onmessage = function(e) {\n" +
" var before = externalMemory();\n" +
" e.data;\n" +
" var after = externalMemory();\n" +
" if (after > before + MB - noise) {\n" +
" self.postMessage('PASS: Amount of external memory increased.');\n" +
" } else {\n" +
" self.postMessage('FAIL: Amount of external memory did not increase.');\n" +
" }\n" +
"}\n";
var blob = new Blob([workerSource]);
var worker = new Worker(window.webkitURL.createObjectURL(blob));
worker.onmessage = function (e) {
print("message from worker: " + e.data, "blue");
}
function test(expression)
{
var buffer = eval(expression);
try {
var before = externalMemory();
worker.postMessage(buffer, [buffer]);
var now = externalMemory();
if (now > before - MB + noise) {
fail("Transfer of " + expression + " did not decrease the amount of external memory (" +
before + ", " + now + ").");
} else {
pass("Transfer of " + expression + " decreased the amount of external memory.");
}
collectGarbage();
var after = externalMemory();
if (after < now - noise) {
fail("Garbage collection after transfer of " + expression +
" decreased the amount of external memory (" + now + ", " + after + ").");
} else {
pass("Garbage collection after transfer of " + expression +
" did not decrease the amount of external memory.");
}
} catch (e) {
fail("Transfer of " + name + ": could not webkitPostMessage: " + e);
return false;
}
return true;
}
test("(new ArrayBuffer(MB))");
test("(new Float32Array(MB)).buffer");
test("(new Float64Array(MB)).buffer");
test("(new Int8Array(MB)).buffer");
test("(new Int16Array(MB)).buffer");
test("(new Int32Array(MB)).buffer");
test("(new Uint8Array(MB)).buffer");
test("(new Uint16Array(MB)).buffer");
test("(new Uint32Array(MB)).buffer");
}
function runAll() {
log = document.getElementById("log1");
if (typeof gc == "undefined" || typeof getV8Statistics == "undefined") {
print("Run chrome browser with --js-flags='--expose_gc --track_gc_object_stats'", "red");
} else {
allocationsThatIncreaseExternalMemory();
collectGarbage();
allocationsThatDoNotChangeExternalMemory();
collectGarbage();
log = document.getElementById("log2");
transfersThatDecreaseExternalMemory();
collectGarbage();
}
}
</script>
</head>
<body onload="runAll()">
<p>This test checks that allocation and deallocation of typed arrays correctly
adjusts the amount of external memory in V8.</p>
<div id='log1'></div>
<p>This test checks that transfer of an array buffer to worker decreases amount of
external memory in the main V8 isolate.</p>
<div id='log2'></div>
</body>
</html>
|