How to use forceSample method in wpt

Best JavaScript code snippet using wpt

profile-utils.js

Source:profile-utils.js Github

copy

Full Screen

1(function(global) {2 const TEST_SAMPLE_INTERVAL = 10;3 function forceSample() {4 // Spin for |TEST_SAMPLE_INTERVAL + 500|ms to ensure that a sample occurs5 // before this function returns. As periodic sampling is enforced by a6 // SHOULD clause, it is indeed testable.7 //8 // More reliable sampling will be handled in a future testdriver RFC9 // (https://github.com/web-platform-tests/rfcs/pull/81).10 for (const deadline = performance.now() + TEST_SAMPLE_INTERVAL + 500; performance.now() < deadline;);11 }12 // Creates a new profile that captures the execution of when the given13 // function calls the `sample` function passed to it.14 async function profileFunction(func) {15 const profiler = new Profiler({16 sampleInterval: TEST_SAMPLE_INTERVAL,17 maxBufferSize: Number.MAX_SAFE_INTEGER,18 });19 func(() => forceSample());20 const trace = await profiler.stop();21 // Sanity check ensuring that we captured a sample.22 assert_greater_than(trace.resources.length, 0);23 assert_greater_than(trace.frames.length, 0);24 assert_greater_than(trace.stacks.length, 0);25 assert_greater_than(trace.samples.length, 0);26 return trace;27 }28 async function testFunction(func, frame) {29 const trace = await profileFunction(func);30 assert_true(containsFrame(trace, frame), 'trace contains frame');31 }32 function substackMatches(trace, stackId, expectedStack) {33 if (expectedStack.length === 0) {34 return true;35 }36 if (stackId === undefined) {37 return false;38 }39 const stackElem = trace.stacks[stackId];40 const expectedFrame = expectedStack[0];41 if (!frameMatches(trace.frames[stackElem.frameId], expectedFrame)) {42 return false;43 }44 return substackMatches(trace, stackElem.parentId, expectedStack.slice(1));45 }46 // Returns true if the trace contains a frame matching the given specification.47 // We define a "match" as follows: a frame A matches an expectation E if (and48 // only if) for each field of E, A has the same value.49 function containsFrame(trace, expectedFrame) {50 return trace.frames.find(frame => {51 return frameMatches(frame, expectedFrame);52 }) !== undefined;53 }54 // Returns true if a trace contains a substack in one of its samples, ordered55 // leaf to root.56 function containsSubstack(trace, expectedStack) {57 return trace.samples.find(sample => {58 let stackId = sample.stackId;59 while (stackId !== undefined) {60 if (substackMatches(trace, stackId, expectedStack)) {61 return true;62 }63 stackId = trace.stacks[stackId].parentId;64 }65 return false;66 }) !== undefined;67 }68 function containsResource(trace, expectedResource) {69 return trace.resources.includes(expectedResource);70 }71 // Compares each set field of `expected` against the given frame `actual`.72 function frameMatches(actual, expected) {73 return (expected.name === undefined || expected.name === actual.name) &&74 (expected.resourceId === undefined || expected.resourceId === actual.resourceId) &&75 (expected.line === undefined || expected.line === actual.line) &&76 (expected.column === undefined || expected.column === actual.column);77 }78 function forceSampleFrame(frame) {79 const channel = new MessageChannel();80 const replyPromise = new Promise(res => {81 channel.port1.onmessage = res;82 });83 frame.postMessage('', '*', [channel.port2]);84 return replyPromise;85 }86 window.addEventListener('message', message => {87 // Force sample in response to messages received.88 (function sampleFromMessage() {89 ProfileUtils.forceSample();90 message.ports[0].postMessage('');91 })();92 });93 global.ProfileUtils = {94 // Capturing95 profileFunction,96 forceSample,97 // Containment checks98 containsFrame,99 containsSubstack,100 containsResource,101 // Cross-frame sampling102 forceSampleFrame,103 // Assertions...

Full Screen

Full Screen

use-speedcurve-lux.spec.ts

Source:use-speedcurve-lux.spec.ts Github

copy

Full Screen

1import { renderHook } from '@testing-library/react-hooks';2import { useSpeedCurveLUX } from '../src';3type LUXProps = 'auto' | 'debug' | 'label' | 'samplerate';4interface Window {5 LUX?: {6 addData: (key: string, value: string | number) => void,7 forceSample: () => void,8 init: () => void,9 send: () => void,10 };11}12describe('useSpeedCurveLUX', () => {13 beforeEach(() => {14 (window as Window).LUX = {15 addData: jest.fn(),16 forceSample: jest.fn(),17 init: jest.fn(),18 send: jest.fn(),19 };20 });21 describe.each<[LUXProps, boolean | string | number]>([22 ['auto', true],23 ['debug', true],24 ['label', 'my page'],25 ['samplerate', 20],26 ])('%s', (key, value) => {27 it('sets the property if a value is given', () => {28 renderHook(() => useSpeedCurveLUX({ [key]: value }));29 expect(window.LUX[key]).toBe(value);30 });31 it('sets the property if a value is given and window.LUX is undefined', () => {32 delete (window as Window).LUX;33 renderHook(() => useSpeedCurveLUX({ [key]: value }));34 expect(window.LUX[key]).toBe(value);35 });36 it('does not set the property if a value is not given', () => {37 renderHook(() => useSpeedCurveLUX());38 expect(window.LUX[key]).toBeUndefined();39 });40 });41 describe('forceSample', () => {42 it('calls the function if the relevant flag is given', () => {43 renderHook(() => useSpeedCurveLUX({ forceSample: true }));44 expect(window.LUX.forceSample).toHaveBeenCalledTimes(1);45 });46 it('does not call the function if no flag is given', () => {47 renderHook(() => useSpeedCurveLUX());48 expect(window.LUX.forceSample).not.toHaveBeenCalled();49 });50 });51 describe('data', () => {52 it('adds the given data', () => {53 renderHook(() => useSpeedCurveLUX({54 data: {55 'cart size': 128,56 'experiment A': 'control',57 },58 }));59 expect(window.LUX.addData).toHaveBeenCalledTimes(2);60 expect(window.LUX.addData).toHaveBeenCalledWith('cart size', 128);61 expect(window.LUX.addData).toHaveBeenCalledWith('experiment A', 'control');62 });63 it('does not attempt to add any data if none given', () => {64 renderHook(() => useSpeedCurveLUX());65 expect(window.LUX.addData).not.toHaveBeenCalled();66 });67 });...

Full Screen

Full Screen

use-speedcurve-lux.ts

Source:use-speedcurve-lux.ts Github

copy

Full Screen

...32 if (samplerate) {33 window.LUX.samplerate = samplerate;34 }35 if (forceSample) {36 window.LUX.forceSample();37 }38 Object.entries(data || {}).forEach(([key, value]) => {39 window.LUX.addData(key, value);40 });41 }, []);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11wpt.getLocations(function(err, data) {12 if (err) {13 console.log(err);14 } else {15 console.log(data);16 }17});18var wpt = require('webpagetest');19var wpt = new WebPageTest('www.webpagetest.org');20wpt.getTesters(function(err, data) {21 if (err) {22 console.log(err);23 } else {24 console.log(data);25 }26});27var wpt = require('webpagetest');28var wpt = new WebPageTest('www.webpagetest.org');29wpt.getTesters(function(err, data) {30 if (err) {31 console.log(err);32 } else {33 console.log(data);34 }35});36var wpt = require('webpagetest');37var wpt = new WebPageTest('www.webpagetest.org');38wpt.getTesters(function(err, data) {39 if (err) {40 console.log(err);41 } else {42 console.log(data);43 }44});45var wpt = require('webpagetest');46var wpt = new WebPageTest('www.webpagetest.org');47wpt.getTesters(function(err, data) {48 if (err) {49 console.log(err);50 } else {51 console.log(data);52 }53});54var wpt = require('webpagetest');55var wpt = new WebPageTest('www.webpagetest.org');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q');3 if (err) return console.error(err);4 console.log(data);5 wpt.forceSample(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log(data);8 });9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.runTest(options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted. You can check the status at %sresult/%s/', wpt.testUrl, data.data.testId);8 wpt.forceSample(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Sample forced');11 });12});13### WebPageTest(testUrl, apiKey, options)14* `testUrl` - the URL of the WebPageTest server (e.g. `www.webpagetest.org` or `www.webpagetest.com`)15 * `protocol` - the protocol to use for requests (default: `http`)16 * `port` - the port to use for requests (default: `80` or `443` depending on `protocol`)17 * `timeout` - the timeout in milliseconds to use for requests (default: `30000`)18 * `firstViewOnly` - whether to only test the first view (default: `false`)19### WebPageTest.getLocations(callback)20wpt.getLocations(function(err, data) {21 if (err) return console.error(err);22 console.log(data);23});24### WebPageTest.getTests(callback)25wpt.getTests(function(err, data) {26 if (err) return console.error(err);27 console.log(data);28});29### WebPageTest.getTestStatus(testId, callback)30wpt.getTestStatus('140530_9Y_1', function(err, data) {31 if (err) return console.error(err);32 console.log(data);33});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.5d9b5d5d5d5d5d5d5d5d5d5d5d5d5d5d5');3wpt.forceSample('www.webpagetest.org', function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});

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 wpt 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