How to use hrTime method in devicefarmer-stf

Best JavaScript code snippet using devicefarmer-stf

hrtime.js

Source:hrtime.js Github

copy

Full Screen

1/*2 * CDDL HEADER START3 *4 * The contents of this file are subject to the terms of the5 * Common Development and Distribution License, Version 1.0 only6 * (the "License"). You may not use this file except in compliance7 * with the License.8 *9 * You can obtain a copy of the license at http://smartos.org/CDDL10 *11 * See the License for the specific language governing permissions12 * and limitations under the License.13 *14 * When distributing Covered Code, include this CDDL HEADER in each15 * file.16 *17 * If applicable, add the following below this CDDL HEADER, with the18 * fields enclosed by brackets "[]" replaced with your own identifying19 * information: Portions Copyright [yyyy] [name of copyright owner]20 *21 * CDDL HEADER END22 *23 * Copyright 2017 Joyent, Inc.24 *25 */26/*27 * hrtime.js provides helper functions to more easily work with hrtimes.28 *29 * It was originally created with the intent to provide the simplicity of using30 * Date.now(), but with the guarantees offered by a monotonic clock.31 *32 */33var util = require('util');34var assert = require('assert-plus');35module.exports.prettyHrtime = prettyHrtime;36module.exports.hrtimeDelta = hrtimeDelta;37module.exports.hrtimeDeltaPretty = hrtimeDeltaPretty;38module.exports.hrtimeComparator = hrtimeComparator;39module.exports.hrtimeToString = hrtimeToString;40module.exports.stringToHrtime = stringToHrtime;41module.exports.assertHrtime = assertHrtime;42/*43 * Convert an hrtime delta to a relative representation - returns strings like44 * "300.0s (5m)" (5 minutes), "0.523s (523ms)" (523 milliseconds), etc.45 *46 * Example:47 *48 * var then = process.hrtime();49 *50 * setTimeout(function () {51 * var now = process.hrtime();52 * var delta;53 * var s;54 *55 * // use hrtimeDelta to calculate the delta56 * delta = hrtimeDelta(now, then);57 * s = prettyHrtime(delta);58 * // => "5.0s (5s)"59 *60 * // pass the first time to the process.hrtime function to calculate the61 * // delta62 * delta = process.hrtime(then);63 * s = prettyHrtime(delta);64 * // => "5.0s (5s)"65 * }, 5 * 1000);66 *67 */68function prettyHrtime(delta) {69 var times;70 var names;71 var relative = '0ns';72 assertHrtime(delta, 'delta');73 times = [74 delta[0] / 60 / 60 / 24, // days75 delta[0] / 60 / 60, // hours76 delta[0] / 60, // minutes77 delta[0], // seconds78 delta[1] / 1e3 / 1e3, // ms79 delta[1] / 1e3, // us80 delta[1] // ns81 ];82 names = ['d', 'h', 'm', 's', 'ms', 'us', 'ns'];83 /*84 * All of the numbers in the `times` array will have (at least potentially)85 * decimal numbers with 2 exceptions:86 *87 * 1. seconds: The smallest unit of the first hrtime array spot.88 * 2. nanoseconds: The smallest unit of the second hrtime array spot.89 *90 * Since nanoseconds is the smallest unit available, there is no way for it91 * to have decimal numbers. However with seconds, we can manually add92 * milliseconds as a decimal number. This data will then get used below93 * when rounding the final value to 2 decimal places when formatting.94 *95 * For example, given an hrtime like [5, 123456789], the final result (in96 * the line below) will be `5.123`.97 */98 times[3] += (times[4] / 1e3);99 for (var i = 0; i < names.length; i++) {100 var t = times[i];101 if (Math.floor(t) > 0) {102 /*103 * `toFixed(2)` is used to ensure that the number seen has a104 * maximum of 2 decimal places. The result is then run through105 * `parseFloat()` to remove any insignifacnt zeroes. For example:106 *107 * Numbers with decimal places get reduced to 2 decimals only.108 * > a = 5.456109 * 5.456110 * > a.toFixed(2)111 * '5.46'112 * > parseFloat(a.toFixed(2))113 * 5.46114 *115 * Numbers without decimals will have the decimal point removed116 * completely.117 * > a = 5118 * 5119 * > a.toFixed(2)120 * '5.00'121 * > parseFloat(a.toFixed(2))122 * 5123 */124 relative = parseFloat(t.toFixed(2), 10) + names[i];125 break;126 }127 }128 return util.format('%ss (%s)', hrtimeToString(delta), relative);129}130/*131 * Calculate the difference of 2 hrtimes (subtracts hr2 from hr1)132 * and returns an array of seconds and nano seconds.133 *134 * hr1 must be larger than hr2135 */136function hrtimeDelta(hr1, hr2) {137 assertHrtime(hr1, 'hr1');138 assertHrtime(hr2, 'hr2');139 var s = hr1[0] - hr2[0];140 var ns = hr1[1] - hr2[1];141 var ret;142 if (ns < 0) {143 ns += 1e9;144 s -= 1;145 }146 ret = [s, ns];147 assertHrtime(ret, 'ret');148 return ret;149}150/*151 * Convenience wrapper for:152 *153 * prettyHrtime(hrtimeDelta(now, then));154 */155function hrtimeDeltaPretty(hr1, hr2) {156 assertHrtime(hr1, 'hr1');157 assertHrtime(hr2, 'hr2');158 return prettyHrtime(hrtimeDelta(hr1, hr2));159}160/*161 * Compare hrtime objects, cane be used directly with Array.prototype.sort162 */163function hrtimeComparator(hr1, hr2) {164 assertHrtime(hr1, 'hr1');165 assertHrtime(hr2, 'hr2');166 var s1 = hr1[0];167 var s2 = hr2[0];168 var ns1 = hr1[1];169 var ns2 = hr2[1];170 // first compare seconds171 if (s1 < s2)172 return -1;173 else if (s1 > s2)174 return 1;175 // next compare nano seconds176 if (ns1 < ns2)177 return -1;178 else if (ns1 > ns2)179 return 1;180 // hr times are the same181 return 0;182}183/*184 * Pretty print an hrtime as a string like "<secs>.<nanosecs>"185 */186function hrtimeToString(hrtime) {187 assertHrtime(hrtime, 'hrtime');188 var s = hrtime[0];189 var ns = hrtime[1].toString();190 while (ns.length < 9) {191 ns = '0' + ns;192 }193 return util.format('%d.%s', s, ns);194}195/*196 * Convert a string like "<secs>.<nanosecs>" to an hrtime array197 */198function stringToHrtime(s) {199 assert.string(s, 's');200 var hrtime = s.split('.').map(function (section) {201 return parseInt(section, 10);202 });203 assertHrtime(hrtime, 'hrtime');204 return hrtime;205}206/*207 * Assert that an object is an hrtime208 */209function assertHrtime(hrtime, s) {210 s = s || 'hrtime';211 assert.string(s, 's');212 assert.arrayOfNumber(hrtime, s);213 assert.equal(hrtime.length, 2, s);214 assert(hrtime[0] >= 0, 'secs >= 0');215 assert(hrtime[1] >= 0, 'nsecs >= 0');216 assert(hrtime[1] < 1e9, 'nsecs < 1e9');...

Full Screen

Full Screen

time.ts

Source:time.ts Github

copy

Full Screen

...51/**52 * Returns an hrtime calculated via performance component.53 * @param performanceNow54 */55export function hrTime(performanceNow?: number): api.HrTime {56 const timeOrigin = numberToHrtime(getTimeOrigin());57 const now = numberToHrtime(58 typeof performanceNow === 'number' ? performanceNow : performance.now()59 );60 let seconds = timeOrigin[0] + now[0];61 let nanos = timeOrigin[1] + now[1];62 // Nanoseconds63 if (nanos > SECOND_TO_NANOSECONDS) {64 nanos -= SECOND_TO_NANOSECONDS;65 seconds += 1;66 }67 return [seconds, nanos];68}69/**70 *71 * Converts a TimeInput to an HrTime, defaults to _hrtime().72 * @param time73 */74export function timeInputToHrTime(time: api.TimeInput): api.HrTime {75 // process.hrtime76 if (isTimeInputHrTime(time)) {77 return time as api.HrTime;78 } else if (typeof time === 'number') {79 // Must be a performance.now() if it's smaller than process start time.80 if (time < getTimeOrigin()) {81 return hrTime(time);82 } else {83 // epoch milliseconds or performance.timeOrigin84 return numberToHrtime(time);85 }86 } else if (time instanceof Date) {87 return numberToHrtime(time.getTime());88 } else {89 throw TypeError('Invalid input type');90 }91}92/**93 * Returns a duration of two hrTime.94 * @param startTime95 * @param endTime...

Full Screen

Full Screen

timer.test.js

Source:timer.test.js Github

copy

Full Screen

1import { timeout, waitFor, Timer } from '../src/timer'2describe('util: timer', () => {3 test('timeout (promise)', async () => {4 const result = await timeout(Promise.resolve('time not run out'), 100)5 expect(result).toEqual('time not run out')6 })7 test('timeout (async function)', async () => {8 const result = await timeout(async () => {9 await waitFor(10)10 return 'time not run out'11 }, 100)12 expect(result).toEqual('time not run out')13 })14 test('timeout (timeout in 100ms)', async () => {15 const call = timeout(waitFor(200), 100, 'timeout test 100ms')16 await expect(call).rejects.toThrow('timeout test 100ms')17 })18 test('timeout (async timeout in 100ms)', async () => {19 const call = timeout(async () => {20 await waitFor(500)21 }, 100, 'timeout test 100ms')22 await expect(call).rejects.toThrow('timeout test 100ms')23 })24 test('waitFor', async () => {25 const delay = 10026 const s = process.hrtime()27 await waitFor(delay)28 const t = process.hrtime(s)29 // Node.js makes no guarantees about the exact timing of when callbacks will fire30 // HTML5 specifies a minimum delay of 4ms for timeouts31 // although arbitrary, use this value to determine our lower limit32 expect((t[0] * 1e9 + t[1]) / 1e6).not.toBeLessThan(delay - 4)33 await waitFor()34 })35 describe('util: timer Timer', () => {36 beforeAll(() => {37 // jest.spyOn()38 })39 test('should construct Timer', () => {40 const timer = new Timer()41 expect(timer._times).toBeInstanceOf(Map)42 })43 test('should create new time record', () => {44 const timer = new Timer()45 timer.hrtime = jest.fn(() => 'hrtime')46 const time = timer.start('test', 'test Timer')47 expect(timer.hrtime).toBeCalledTimes(1)48 expect(time).toEqual({ description: 'test Timer', name: 'test', start: 'hrtime' })49 })50 test('should stop and remove time record', () => {51 const timer = new Timer()52 timer.hrtime = jest.fn(() => 'hrtime')53 timer.start('test', 'test Timer')54 const time = timer.end('test')55 expect(timer._times.size).toEqual(0)56 expect(timer.hrtime).toBeCalledTimes(2)57 expect(timer.hrtime).nthCalledWith(2, 'hrtime')58 expect(time).toEqual({ description: 'test Timer', name: 'test', duration: 'hrtime', start: 'hrtime' })59 })60 test('should be quiet if end with nonexistent time', () => {61 const timer = new Timer()62 const time = timer.end('test')63 expect(time).toBeUndefined()64 })65 test('should use bigint hrtime if supports', () => {66 const timer = new Timer()67 const hrtime = process.hrtime68 process.hrtime = {69 bigint: jest.fn(() => 'bigint hrtime')70 }71 const time = timer.hrtime()72 expect(time).toEqual('bigint hrtime')73 expect(process.hrtime.bigint).toBeCalledTimes(1)74 process.hrtime = hrtime75 })76 if (typeof BigInt !== 'undefined') {77 test('should calculate duration with bigint hrtime', () => {78 const timer = new Timer()79 const hrtime = process.hrtime80 process.hrtime = {81 bigint: jest.fn()82 .mockReturnValueOnce(BigInt(100000000))83 .mockReturnValueOnce(BigInt(213000000))84 }85 let time = timer.hrtime()86 time = timer.hrtime(time)87 expect(time).toEqual(BigInt(113))88 expect(process.hrtime.bigint).toBeCalledTimes(2)89 process.hrtime = hrtime90 })91 }92 test('should use hrtime if bigint it not supported', () => {93 const timer = new Timer()94 const hrtime = process.hrtime95 process.hrtime = jest.fn(() => 'hrtime')96 process.hrtime.bigint = undefined97 const time = timer.hrtime()98 expect(time).toEqual('hrtime')99 expect(process.hrtime).toBeCalledTimes(1)100 process.hrtime = hrtime101 })102 test('should calculate duration with hrtime', () => {103 const timer = new Timer()104 const hrtime = process.hrtime105 process.hrtime = jest.fn()106 .mockReturnValueOnce([1, 500000])107 .mockReturnValueOnce([2, 600000])108 process.hrtime.bigint = undefined109 let time = timer.hrtime()110 time = timer.hrtime(time)111 expect(time).toEqual(2000.6)112 expect(process.hrtime).toBeCalledTimes(2)113 expect(process.hrtime).nthCalledWith(1)114 expect(process.hrtime).nthCalledWith(2, [1, 500000])115 process.hrtime = hrtime116 })117 test('should clear all times', () => {118 const timer = new Timer()119 timer.hrtime = jest.fn(() => 'hrtime')120 timer.start('time-1', 'test time-1')121 timer.start('time-2', 'test time-2')122 timer.start('time-3', 'test time-3')123 expect(timer._times.size).toEqual(3)124 timer.clear()125 expect(timer._times.size).toEqual(0)126 })127 })...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf');2var device = new stf.Device();3device.hrTime();4var stf = require('devicefarmer-stf');5var device = new stf.Device();6device.hrTime(function(err, time) {7 console.log(time);8});9var stf = require('devicefarmer-stf');10var device = new stf.Device();11device.hrTime(function(err, time) {12 console.log(time);13}, 1);14var stf = require('devicefarmer-stf');15var device = new stf.Device();16device.hrTime(function(err, time) {17 console.log(time);18}, 1, 2);19var stf = require('devicefarmer-stf');20var device = new stf.Device();21device.hrTime(function(err, time) {22 console.log(time);23}, 1, 2, 3);24var stf = require('devicefarmer-stf');25var device = new stf.Device();26device.hrTime(function(err, time) {27 console.log(time);28}, 1, 2, 3, 4);29var stf = require('devicefarmer-stf');30var device = new stf.Device();31device.hrTime(function(err, time) {32 console.log(time);33}, 1, 2, 3, 4, 5);34var stf = require('devicefarmer-stf');35var device = new stf.Device();36device.hrTime(function(err, time) {37 console.log(time);38}, 1, 2, 3, 4, 5, 6);39var stf = require('devicefarmer-stf');40var device = new stf.Device();41device.hrTime(function(err, time) {42 console.log(time);43}, 1, 2, 3, 4, 5, 6, 7);44var stf = require('devicefarmer-stf');45var device = new stf.Device();46device.hrTime(function(err, time) {47 console.log(time);48}, 1, 2, 3, 4, 5, 6, 7,

Full Screen

Using AI Code Generation

copy

Full Screen

1var hrTime = require('devicefarmer-stf').hrTime;2var startTime = hrTime();3var endTime = hrTime();4var elapsed = endTime - startTime;5console.log('elapsed time: ' + elapsed + ' ns');6deviceAdded(device)7deviceRemoved(device)8deviceChanged(device)9deviceMessage(device, message)10deviceLog(device, log)11deviceLogcat(device, logcat)12deviceBugreport(device, bugreport)13deviceScreenshot(device, screenshot)14deviceVideo(device, video)15deviceMinicap(device, frame)16deviceMinitouch(device, event)17deviceMinitouchSwipe(device, event)18deviceMinitouchPinch(device, event)19deviceMinitouchRotate(device, event)20deviceMinitouchTap(device, event)21deviceMinitouchDown(device, event)22deviceMinitouchMove(device, event)23deviceMinitouchUp(device, event)24deviceMinitouchCancel(device, event)

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf');2var hrTime = stf.hrTime;3var test = function() {4 var start = hrTime();5 var end = hrTime();6 console.log("hrTime: " + (end - start));7};8test();

Full Screen

Using AI Code Generation

copy

Full Screen

1var stf = require('devicefarmer-stf');2var hrtime = stf.hrtime;3var start = hrtime();4var diff = hrtime(start);5console.log(diff);6var start = process.hrtime();7var diff = process.hrtime(start);8console.log(diff);

Full Screen

Using AI Code Generation

copy

Full Screen

1var devicefarmer = require('devicefarmer-stf');2var device = new devicefarmer.Device();3device.connect(function(){4 device.getSystemInfo(function(err, data){5 if(err){6 console.log("Error : "+err);7 }8 else{9 console.log("System info : "+JSON.stringify(data));10 }11 });12});13var adbkit = require('adbkit');14var client = adbkit.createClient();15client.listDevices()16.then(function(devices) {17 return Promise.all(devices.map(function(device) {18 return client.getProperties(device.id);19 }));20})21.then(function(properties) {22 properties.forEach(function(property) {23 console.log(property);24 });25})26.catch(function(err) {27 console.error('Something went wrong:', err.stack);28});29System info : {"ro.build.version.sdk":"21","ro.build.version.release":"5.0.2","ro.build.version.codename":"REL","ro.build.version.incremental":"V10c","ro.build.version.all_codenames":"REL","ro.build.version.base_os":"","ro.build.date":"Mon Aug 31 20:09:03 CST 2015","ro.build.date.utc":"1440980143","ro.build.type":"user","ro.build.user":"jenkins","ro.build.host":"jenkins-ubuntu","ro.build.tags":"release-keys","ro.build.flavor":"full_jfltexx-user","ro.product.model":"SM-G900F","ro.product.brand":"samsung","ro.product.name":"jfltexx","ro.product.device":"jfltexx","ro.product.board":"universal5420","ro.product.cpu.abi":"armeabi-v7a","ro.product.cpu.abi2":"armeabi","ro.product.manufacturer":"samsung","ro.product.locale.language":"en","ro.product.locale.region":"GB","ro.wifi.channels":"","ro.board.platform":"universal5420","ro.build.product":"jfltexx","ro.build.description":"jfltexx-user 5.0.2 LRX22G V10c release-keys","ro.build.fingerprint":"samsung/jfltexx/jflte:5.0.2/LRX22G/V10c:user/release-keys","ro.build.characteristics":"default","ro.config.ringtone":"Over_the_horizon.ogg","ro.config.notification_sound":"S_

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run devicefarmer-stf automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful