toolkit/components/osfile/tests/xpcshell/test_duration.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 let {OS} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
michael@0 2 let {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {});
michael@0 3
michael@0 4 /**
michael@0 5 * Test optional duration reporting that can be used for telemetry.
michael@0 6 */
michael@0 7 add_task(function* duration() {
michael@0 8 Services.prefs.setBoolPref("toolkit.osfile.log", true);
michael@0 9 // Options structure passed to a OS.File copy method.
michael@0 10 let copyOptions = {
michael@0 11 // This field should be overridden with the actual duration
michael@0 12 // measurement.
michael@0 13 outExecutionDuration: null
michael@0 14 };
michael@0 15 let currentDir = yield OS.File.getCurrentDirectory();
michael@0 16 let pathSource = OS.Path.join(currentDir, "test_duration.js");
michael@0 17 let copyFile = pathSource + ".bak";
michael@0 18 function testOptions(options, name) {
michael@0 19 do_print("Checking outExecutionDuration for operation: " + name);
michael@0 20 do_print(name + ": Gathered method duration time: " +
michael@0 21 options.outExecutionDuration + "ms");
michael@0 22 // Making sure that duration was updated.
michael@0 23 do_check_eq(typeof options.outExecutionDuration, "number");
michael@0 24 do_check_true(options.outExecutionDuration >= 0);
michael@0 25 };
michael@0 26 // Testing duration of OS.File.copy.
michael@0 27 yield OS.File.copy(pathSource, copyFile, copyOptions);
michael@0 28 testOptions(copyOptions, "OS.File.copy");
michael@0 29 yield OS.File.remove(copyFile);
michael@0 30
michael@0 31 // Trying an operation where options are cloned.
michael@0 32 let pathDest = OS.Path.join(OS.Constants.Path.tmpDir,
michael@0 33 "osfile async test read writeAtomic.tmp");
michael@0 34 let tmpPath = pathDest + ".tmp";
michael@0 35 let readOptions = {
michael@0 36 outExecutionDuration: null
michael@0 37 };
michael@0 38 let contents = yield OS.File.read(pathSource, undefined, readOptions);
michael@0 39 testOptions(readOptions, "OS.File.read");
michael@0 40 // Options structure passed to a OS.File writeAtomic method.
michael@0 41 let writeAtomicOptions = {
michael@0 42 // This field should be first initialized with the actual
michael@0 43 // duration measurement then progressively incremented.
michael@0 44 outExecutionDuration: null,
michael@0 45 tmpPath: tmpPath
michael@0 46 };
michael@0 47 yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
michael@0 48 testOptions(writeAtomicOptions, "OS.File.writeAtomic");
michael@0 49 yield OS.File.remove(pathDest);
michael@0 50
michael@0 51 do_print("Ensuring that we can use outExecutionDuration to accumulate durations");
michael@0 52
michael@0 53 let ARBITRARY_BASE_DURATION = 5;
michael@0 54 copyOptions = {
michael@0 55 // This field should now be incremented with the actual duration
michael@0 56 // measurement.
michael@0 57 outExecutionDuration: ARBITRARY_BASE_DURATION
michael@0 58 };
michael@0 59 let backupDuration = ARBITRARY_BASE_DURATION;
michael@0 60 // Testing duration of OS.File.copy.
michael@0 61 yield OS.File.copy(pathSource, copyFile, copyOptions);
michael@0 62
michael@0 63 do_check_true(copyOptions.outExecutionDuration >= backupDuration);
michael@0 64
michael@0 65 backupDuration = copyOptions.outExecutionDuration;
michael@0 66 yield OS.File.remove(copyFile, copyOptions);
michael@0 67 do_check_true(copyOptions.outExecutionDuration >= backupDuration);
michael@0 68
michael@0 69 // Trying an operation where options are cloned.
michael@0 70 // Options structure passed to a OS.File writeAtomic method.
michael@0 71 writeAtomicOptions = {
michael@0 72 // This field should be overridden with the actual duration
michael@0 73 // measurement.
michael@0 74 outExecutionDuration: copyOptions.outExecutionDuration,
michael@0 75 tmpPath: tmpPath
michael@0 76 };
michael@0 77 backupDuration = writeAtomicOptions.outExecutionDuration;
michael@0 78
michael@0 79 yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
michael@0 80 do_check_true(copyOptions.outExecutionDuration >= backupDuration);
michael@0 81 OS.File.remove(pathDest);
michael@0 82
michael@0 83 // Testing an operation that doesn't take arguments at all
michael@0 84 let file = yield OS.File.open(pathSource);
michael@0 85 yield file.stat();
michael@0 86 yield file.close();
michael@0 87 });
michael@0 88
michael@0 89 function run_test() {
michael@0 90 run_next_test();
michael@0 91 }

mercurial