3 <title>ArrayBuffer External Memory test</title>
7 function print(message, color)
9 var paragraph = document.createElement("div");
10 paragraph.appendChild(document.createTextNode(message));
11 paragraph.style.fontFamily = "monospace";
13 paragraph.style.color = color;
14 log.appendChild(paragraph);
19 print("PASS: " + msg, "green");
24 print("FAIL: " + msg, "red");
31 function externalMemory() {
32 return getV8Statistics().amount_of_external_allocated_memory;
35 function collectGarbage() {
36 for (var i = 0; i < 10; i++) gc();
39 function allocationsThatIncreaseExternalMemory() {
40 function test(expression) {
41 var before = externalMemory();
42 (function () { eval(expression); }) ();
43 var now = externalMemory();
44 if (now < before + MB - noise) {
45 fail(expression + " did not increase the amount of external memory (" +
46 before + ", " + now + ").");
48 pass(expression + " increased the amount of external memory.");
51 var after = externalMemory();
52 if (after > now + noise) {
53 fail("Garbage collection after " + expression +
54 " did not return the amount of external memory to the initial value (" +
55 now + ", " + after + ").");
57 pass("Garbage collection after " + expression +
58 " returned the amount of external memory to the initial value.");
62 test("(new ArrayBuffer(MB))");
63 test("(new Float32Array(MB))");
64 test("(new Float64Array(MB))");
65 test("(new Int8Array(MB))");
66 test("(new Int16Array(MB))");
67 test("(new Int32Array(MB))");
68 test("(new Uint8Array(MB))");
69 test("(new Uint16Array(MB))");
70 test("(new Uint32Array(MB))");
71 var largeJSArray = [];
72 for (var i = 0; i < MB; i++) largeJSArray.push(i);
73 test("(new Float32Array(largeJSArray))");
74 test("(new Float64Array(largeJSArray))");
75 test("(new Int8Array(largeJSArray))");
76 test("(new Int16Array(largeJSArray))");
77 test("(new Int32Array(largeJSArray))");
78 test("(new Uint8Array(largeJSArray))");
79 test("(new Uint16Array(largeJSArray))");
80 test("(new Uint32Array(largeJSArray))");
81 var int8Array = new Int8Array(MB);
82 test("(new Float32Array(int8Array))");
83 test("(new Float64Array(int8Array))");
84 test("(new Int8Array(int8Array))");
85 test("(new Int16Array(int8Array))");
86 test("(new Int32Array(int8Array))");
87 test("(new Uint8Array(int8Array))");
88 test("(new Uint16Array(int8Array))");
89 test("(new Uint32Array(int8Array))");
93 function allocationsThatDoNotChangeExternalMemory() {
94 function test(expression) {
95 var before = externalMemory();
96 (function () { eval(expression); }) ();
97 var now = externalMemory();
98 if (now > before + noise) {
99 fail(expression + " increased the amount of external memory (" + before + ", " + now + ").");
101 pass(expression + " did not increase the amount of external memory.");
104 var after = externalMemory();
105 if (after < now - noise) {
106 fail("Garbage collection after " + expression + " decreased the amount of external memory (" +
107 now + ", " + after + ").");
109 pass("Garbage collection after " + expression +
110 " did not decrease the amount of external memory.");
113 var arrayBuffer = new ArrayBuffer(MB);
114 test("(new Float32Array(arrayBuffer))");
115 test("(new Float64Array(arrayBuffer))");
116 test("(new Int8Array(arrayBuffer))");
117 test("(new Int16Array(arrayBuffer))");
118 test("(new Int32Array(arrayBuffer))");
119 test("(new Uint8Array(arrayBuffer))");
120 test("(new Uint16Array(arrayBuffer))");
121 test("(new Uint32Array(arrayBuffer))");
122 var int8Array = new Int8Array(MB);
123 test("(new Float32Array(int8Array.buffer))");
124 test("(new Float64Array(int8Array.buffer))");
125 test("(new Int8Array(int8Array.buffer))");
126 test("(new Int16Array(int8Array.buffer))");
127 test("(new Int32Array(int8Array.buffer))");
128 test("(new Uint8Array(int8Array.buffer))");
129 test("(new Uint16Array(int8Array.buffer))");
130 test("(new Uint32Array(int8Array.buffer))");
134 function transfersThatDecreaseExternalMemory() {
136 "function externalMemory() {\n" +
137 " return getV8Statistics().amount_of_external_allocated_memory;\n" +
140 "var MB = KB * KB;\n" +
141 "var noise = KB;\n" +
142 "self.onmessage = function(e) {\n" +
143 " var before = externalMemory();\n" +
145 " var after = externalMemory();\n" +
146 " if (after > before + MB - noise) {\n" +
147 " self.postMessage('PASS: Amount of external memory increased.');\n" +
149 " self.postMessage('FAIL: Amount of external memory did not increase.');\n" +
153 var blob = new Blob([workerSource]);
154 var worker = new Worker(window.webkitURL.createObjectURL(blob));
155 worker.onmessage = function (e) {
156 print("message from worker: " + e.data, "blue");
158 function test(expression)
160 var buffer = eval(expression);
162 var before = externalMemory();
163 worker.postMessage(buffer, [buffer]);
164 var now = externalMemory();
165 if (now > before - MB + noise) {
166 fail("Transfer of " + expression + " did not decrease the amount of external memory (" +
167 before + ", " + now + ").");
169 pass("Transfer of " + expression + " decreased the amount of external memory.");
172 var after = externalMemory();
173 if (after < now - noise) {
174 fail("Garbage collection after transfer of " + expression +
175 " decreased the amount of external memory (" + now + ", " + after + ").");
177 pass("Garbage collection after transfer of " + expression +
178 " did not decrease the amount of external memory.");
181 fail("Transfer of " + name + ": could not webkitPostMessage: " + e);
186 test("(new ArrayBuffer(MB))");
187 test("(new Float32Array(MB)).buffer");
188 test("(new Float64Array(MB)).buffer");
189 test("(new Int8Array(MB)).buffer");
190 test("(new Int16Array(MB)).buffer");
191 test("(new Int32Array(MB)).buffer");
192 test("(new Uint8Array(MB)).buffer");
193 test("(new Uint16Array(MB)).buffer");
194 test("(new Uint32Array(MB)).buffer");
199 log = document.getElementById("log1");
200 if (typeof gc == "undefined" || typeof getV8Statistics == "undefined") {
201 print("Run chrome browser with --js-flags='--expose_gc --track_gc_object_stats'", "red");
203 allocationsThatIncreaseExternalMemory();
205 allocationsThatDoNotChangeExternalMemory();
207 log = document.getElementById("log2");
208 transfersThatDecreaseExternalMemory();
215 <body onload="runAll()">
216 <p>This test checks that allocation and deallocation of typed arrays correctly
217 adjusts the amount of external memory in V8.</p>
218 <div id='log1'></div>
219 <p>This test checks that transfer of an array buffer to worker decreases amount of
220 external memory in the main V8 isolate.</p>
221 <div id='log2'></div>