Skip to content

Commit 7ed6f16

Browse files
authored
merge async-fs code (WebKit#135)
Copy code from async-fs and avoid using Math.random.
1 parent c5af577 commit 7ed6f16

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

generators/sync-file-system.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@ function computeIsLittleEndian() {
3434
}
3535

3636
const isLittleEndian = computeIsLittleEndian();
37+
let globalCounter = 0;
3738

38-
function randomFileContents(bytes = ((Math.random() * 128) >>> 0) + 2056) {
39-
let result = new ArrayBuffer(bytes);
39+
function randomFileContents() {
40+
const numBytes = (globalCounter % 128) + 2056;
41+
globalCounter++;
42+
let result = new ArrayBuffer(numBytes);
4043
let view = new Uint8Array(result);
41-
for (let i = 0; i < bytes; ++i)
42-
view[i] = (Math.random() * 255) >>> 0;
44+
for (let i = 0; i < numBytes; ++i)
45+
view[i] = (i + globalCounter) % 255;
4346
return new DataView(result);
4447
}
4548

49+
4650
class File {
4751
constructor(dataView, permissions) {
4852
this._data = dataView;
@@ -145,27 +149,30 @@ class Directory {
145149
function setupDirectory() {
146150
const fs = new Directory;
147151
let dirs = [fs];
152+
let counter = 0;
148153
for (let dir of dirs) {
149-
for (let i = 0; i < 8; ++i) {
150-
if (dirs.length < 250 && Math.random() >= 0.3) {
154+
for (let i = 0; i < 10; ++i) {
155+
if (dirs.length < 400 && (counter % 3) <= 1) {
151156
dirs.push(dir.addDirectory(`dir-${i}`));
152157
}
158+
counter++;
153159
}
154160
}
155161

156162
for (let dir of dirs) {
157163
for (let i = 0; i < 5; ++i) {
158-
if (Math.random() >= 0.6) {
164+
if ((counter % 3) === 0) {
159165
dir.addFile(`file-${i}`, new File(randomFileContents()));
160166
}
167+
counter++;
161168
}
162169
}
163170

164171
return fs;
165172
}
166173

167174
class Benchmark {
168-
EXPECTED_FILE_COUNT = 411;
175+
EXPECTED_FILE_COUNT = 666;
169176

170177
totalFileCount = 0;
171178
lastFileHash = undefined;

0 commit comments

Comments
 (0)