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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_duration.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +let {OS} = Components.utils.import("resource://gre/modules/osfile.jsm", {});
     1.5 +let {Services} = Components.utils.import("resource://gre/modules/Services.jsm", {});
     1.6 +
     1.7 +/**
     1.8 + * Test optional duration reporting that can be used for telemetry.
     1.9 + */
    1.10 +add_task(function* duration() {
    1.11 +  Services.prefs.setBoolPref("toolkit.osfile.log", true);
    1.12 +  // Options structure passed to a OS.File copy method.
    1.13 +  let copyOptions = {
    1.14 +    // This field should be overridden with the actual duration
    1.15 +    // measurement.
    1.16 +    outExecutionDuration: null
    1.17 +  };
    1.18 +  let currentDir = yield OS.File.getCurrentDirectory();
    1.19 +  let pathSource = OS.Path.join(currentDir, "test_duration.js");
    1.20 +  let copyFile = pathSource + ".bak";
    1.21 +  function testOptions(options, name) {
    1.22 +    do_print("Checking outExecutionDuration for operation: " + name);
    1.23 +    do_print(name + ": Gathered method duration time: " +
    1.24 +      options.outExecutionDuration + "ms");
    1.25 +    // Making sure that duration was updated.
    1.26 +    do_check_eq(typeof options.outExecutionDuration, "number");
    1.27 +    do_check_true(options.outExecutionDuration >= 0);
    1.28 +  };
    1.29 +  // Testing duration of OS.File.copy.
    1.30 +  yield OS.File.copy(pathSource, copyFile, copyOptions);
    1.31 +  testOptions(copyOptions, "OS.File.copy");
    1.32 +  yield OS.File.remove(copyFile);
    1.33 +
    1.34 +  // Trying an operation where options are cloned.
    1.35 +  let pathDest = OS.Path.join(OS.Constants.Path.tmpDir,
    1.36 +    "osfile async test read writeAtomic.tmp");
    1.37 +  let tmpPath = pathDest + ".tmp";
    1.38 +  let readOptions = {
    1.39 +    outExecutionDuration: null
    1.40 +  };
    1.41 +  let contents = yield OS.File.read(pathSource, undefined, readOptions);
    1.42 +  testOptions(readOptions, "OS.File.read");
    1.43 +  // Options structure passed to a OS.File writeAtomic method.
    1.44 +  let writeAtomicOptions = {
    1.45 +    // This field should be first initialized with the actual
    1.46 +    // duration measurement then progressively incremented.
    1.47 +    outExecutionDuration: null,
    1.48 +    tmpPath: tmpPath
    1.49 +  };
    1.50 +  yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
    1.51 +  testOptions(writeAtomicOptions, "OS.File.writeAtomic");
    1.52 +  yield OS.File.remove(pathDest);
    1.53 +
    1.54 +  do_print("Ensuring that we can use outExecutionDuration to accumulate durations");
    1.55 +
    1.56 +  let ARBITRARY_BASE_DURATION = 5;
    1.57 +  copyOptions = {
    1.58 +    // This field should now be incremented with the actual duration
    1.59 +    // measurement.
    1.60 +    outExecutionDuration: ARBITRARY_BASE_DURATION
    1.61 +  };
    1.62 +  let backupDuration = ARBITRARY_BASE_DURATION;
    1.63 +  // Testing duration of OS.File.copy.
    1.64 +  yield OS.File.copy(pathSource, copyFile, copyOptions);
    1.65 +
    1.66 +  do_check_true(copyOptions.outExecutionDuration >= backupDuration);
    1.67 +
    1.68 +  backupDuration = copyOptions.outExecutionDuration;
    1.69 +  yield OS.File.remove(copyFile, copyOptions);
    1.70 +  do_check_true(copyOptions.outExecutionDuration >= backupDuration);
    1.71 +
    1.72 +  // Trying an operation where options are cloned.
    1.73 +  // Options structure passed to a OS.File writeAtomic method.
    1.74 +  writeAtomicOptions = {
    1.75 +    // This field should be overridden with the actual duration
    1.76 +    // measurement.
    1.77 +    outExecutionDuration: copyOptions.outExecutionDuration,
    1.78 +    tmpPath: tmpPath
    1.79 +  };
    1.80 +  backupDuration = writeAtomicOptions.outExecutionDuration;
    1.81 +
    1.82 +  yield OS.File.writeAtomic(pathDest, contents, writeAtomicOptions);
    1.83 +  do_check_true(copyOptions.outExecutionDuration >= backupDuration);
    1.84 +  OS.File.remove(pathDest);
    1.85 +
    1.86 +  // Testing an operation that doesn't take arguments at all
    1.87 +  let file = yield OS.File.open(pathSource);
    1.88 +  yield file.stat();
    1.89 +  yield file.close();
    1.90 +});
    1.91 +
    1.92 +function run_test() {
    1.93 +  run_next_test();
    1.94 +}

mercurial