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
|
var { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
/**
* Test optional duration reporting that can be used for telemetry.
*/
add_task(async function duration() {
const availableDurations = [
"outSerializationDuration",
"outExecutionDuration",
];
Services.prefs.setBoolPref("toolkit.osfile.log", true);
// Options structure passed to a OS.File copy method.
let copyOptions = {
// These fields should be overwritten with the actual duration
// measurements.
outSerializationDuration: null,
outExecutionDuration: null,
};
let currentDir = await OS.File.getCurrentDirectory();
let pathSource = OS.Path.join(currentDir, "test_duration.js");
let copyFile = pathSource + ".bak";
function testOptions(options, name, durations = availableDurations) {
for (let duration of durations) {
info(`Checking ${duration} for operation: ${name}`);
info(`${name}: Gathered method duration time: ${options[duration]} ms`);
// Making sure that duration was updated.
Assert.equal(typeof options[duration], "number");
Assert.ok(options[duration] >= 0);
}
}
function testOptionIncrements(
options,
name,
backupDuration,
durations = availableDurations
) {
for (let duration of durations) {
info(`Checking ${duration} increment for operation: ${name}`);
info(`${name}: Gathered method duration time: ${options[duration]} ms`);
info(`${name}: Previous duration: ${backupDuration[duration]} ms`);
// Making sure that duration was incremented.
Assert.ok(options[duration] >= backupDuration[duration]);
}
}
// Testing duration of OS.File.copy.
await OS.File.copy(pathSource, copyFile, copyOptions);
testOptions(copyOptions, "OS.File.copy");
await OS.File.remove(copyFile);
// Trying an operation where options are cloned.
let pathDest = OS.Path.join(
OS.Constants.Path.tmpDir,
"osfile async test read writeAtomic.tmp"
);
let tmpPath = pathDest + ".tmp";
let readOptions = {
// We do not check for |outSerializationDuration| since |Scheduler.post|
// may not be called whenever |read| is called.
outExecutionDuration: null,
};
let contents = await OS.File.read(pathSource, undefined, readOptions);
testOptions(readOptions, "OS.File.read", ["outExecutionDuration"]);
// Options structure passed to a OS.File writeAtomic method.
let writeAtomicOptions = {
// This field should be first initialized with the actual
// duration measurement then progressively incremented.
outExecutionDuration: null,
tmpPath,
};
// Note that |contents| cannot be reused after this call since it is detached.
await OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
testOptions(writeAtomicOptions, "OS.File.writeAtomic", [
"outExecutionDuration",
]);
await OS.File.remove(pathDest);
info(
`Ensuring that we can use ${availableDurations.join(
", "
)} to accumulate durations`
);
let ARBITRARY_BASE_DURATION = 5;
copyOptions = {
// This field should now be incremented with the actual duration
// measurement.
outSerializationDuration: ARBITRARY_BASE_DURATION,
outExecutionDuration: ARBITRARY_BASE_DURATION,
};
// We need to copy the object, since having a reference would make this pointless.
let backupDuration = Object.assign({}, copyOptions);
// Testing duration of OS.File.copy.
await OS.File.copy(pathSource, copyFile, copyOptions);
testOptionIncrements(copyOptions, "copy", backupDuration);
backupDuration = Object.assign({}, copyOptions);
await OS.File.remove(copyFile, copyOptions);
testOptionIncrements(copyOptions, "remove", backupDuration);
// Trying an operation where options are cloned.
// Options structure passed to a OS.File writeAtomic method.
writeAtomicOptions = {
// We do not check for |outSerializationDuration| since |Scheduler.post|
// may not be called whenever |writeAtomic| is called.
outExecutionDuration: ARBITRARY_BASE_DURATION,
};
writeAtomicOptions.tmpPath = tmpPath;
backupDuration = Object.assign({}, writeAtomicOptions);
contents = await OS.File.read(pathSource, undefined, readOptions);
await OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
testOptionIncrements(
writeAtomicOptions,
"writeAtomicOptions",
backupDuration,
["outExecutionDuration"]
);
OS.File.remove(pathDest);
// Testing an operation that doesn't take arguments at all
let file = await OS.File.open(pathSource);
await file.stat();
await file.close();
});
|