Best JavaScript code snippet using best
hub.ts
Source:hub.ts
...139 const matchingAgents = this.findAgentMatchingSpecs(remoteClient, { ignoreBusy: true });140 if (matchingAgents.length > 0) {141 const remoteAgent = matchingAgents[0];142 this.activeClients.set(remoteClient, remoteAgent);143 this.emit(BEST_RPC.AGENT_RUNNING_CLIENT, { clientId: remoteClient.getId(), agentId: remoteAgent.getId(), jobs: remoteClient.getPendingBenchmarks() });144 try {145 await remoteAgent.runBenchmarks(remoteClient);146 } catch(err) {147 console.log(`[HUB] Error running benchmark for remote client ${remoteClient.getId()}`);148 remoteClient.disconnectClient(`Error running benchmark ${err}`); // make sure we disconnect the agent149 } finally {150 this.activeClients.delete(remoteClient);151 queueMicrotask(() => this.runQueuedBenchmarks());152 }153 } else {154 console.log('[HUB] All agents are busy at this moment...');155 }156 } else {157 console.log(`[HUB] Client ${remoteClient.getId()} is actively running already`)158 }159 }160 runQueuedBenchmarks() {161 Array.from(this.connectedClients).forEach((remoteClient) => {162 if (!this.activeClients.has(remoteClient)) {163 if (this.idleAgentMatchingSpecs(remoteClient) && remoteClient.getPendingBenchmarks() > 0) {164 console.log(`[HUB] Running benchmark: "${remoteClient.getId()}" has ${remoteClient.getPendingBenchmarks()} to run`);165 this.runBenchmarks(remoteClient);166 } else {167 console.log(`[HUB] All matching agents still busy for ${remoteClient.getId()}`);168 }169 }170 });171 }172 getAgentSpecs(): BrowserSpec[] {173 const specs: BrowserSpec[] = [];174 for (const agent of this.connectedAgents) {175 specs.push(...agent.getSpecs());176 }177 return specs;178 }...
agent.ts
Source:agent.ts
...58 socketClient.emit(BEST_RPC.AGENT_REJECTION, invalidConfig);59 return socketClient.disconnect(true);60 }61 const remoteClient = this.setupNewClient(socketClient, config);62 console.log(`[AGENT] Connected clients: ${this.connectedClients.size} | state: ${this.state} | activePending: ${this.activeClient && this.activeClient.getPendingBenchmarks()}`);63 if (this.idleState) {64 this.runBenchmark(remoteClient);65 } else {66 remoteClient.log(`Client enqueued. Waiting for the agent to be free...`);67 this.emit(BEST_RPC.AGENT_QUEUED_CLIENT, { clientId: remoteClient.getId(), jobs: config.jobs, specs: config.specs });68 }69 }70 async runBenchmark(remoteClient: RemoteClient) {71 if (this.idleState) {72 this.state = AgentState.BUSY;73 this.activeClient = remoteClient;74 try {75 console.log(`[AGENT] Requesting benchmark from RemoteClient ${remoteClient.getId()}`);76 const benchmarkBuild = await remoteClient.requestJob();77 const bundleConfig = createBundleConfig(benchmarkBuild, this.agentConfig);78 console.log(`[AGENT] Running benchmark ${benchmarkBuild.benchmarkSignature} from RemoteClient ${remoteClient.getId()}`);79 this.interruption = new RunnerInterruption(benchmarkBuild.benchmarkSignature);80 const results = await runBenchmarks(bundleConfig, remoteClient, this.interruption);81 console.log(`[AGENT] Completed benchmark ${benchmarkBuild.benchmarkSignature} from RemoteClient ${remoteClient.getId()}`);82 remoteClient.sendResults(results);83 } catch(err) {84 console.log(`[AGENT] Error running benchmark for remote client ${remoteClient.getId()}`);85 console.log(err);86 remoteClient.disconnectClient(`Error running benchmark ${err}`); // make sure we disconnect the agent87 } finally {88 this.state = AgentState.IDLE;89 this.interruption = undefined;90 queueMicrotask(() => this.runQueuedBenchmarks());91 }92 } else {93 console.log('[AGENT] Benchmark already running...');94 }95 }96 runQueuedBenchmarks() {97 if (this.idleState) {98 console.log('[AGENT] Checking for queued agents and tasks...');99 if (this.activeClient && this.activeClient.getPendingBenchmarks()) {100 console.log(`[AGENT] Active Client "${this.activeClient.getId()}" has still ${this.activeClient.getPendingBenchmarks()} pending`);101 this.runBenchmark(this.activeClient);102 } else {103 // Note that there might be some clients with no jobs still connected (we give them some time to disconnect)104 // So to avoid race conditions we check for remaining jobs, rather that just check for an arbitrary client on the queue105 const remoteClient = Array.from(this.connectedClients).find(client => client.getPendingBenchmarks() > 0);106 if (remoteClient) {107 console.log(`[AGENT] Client "${remoteClient.getId()}" has ${remoteClient.getPendingBenchmarks()} to run`);108 this.runBenchmark(remoteClient);109 } else {110 console.log('[AGENT] No more jobs to run at the moment');111 }112 }113 } else {114 console.log(`[AGENT] Busy, running ${this.activeClient!.getId()}`);115 }116 }117 get idleState() { return this.state === AgentState.IDLE; }118 getStateInfo() {119 return `120 |> state: ${this.idleState}121 |> clients: ${this.connectedClients.size}...
remote-agent.ts
Source:remote-agent.ts
...84 }85 isIdle(): boolean {86 return this.state === AgentState.IDLE;87 }88 async runBenchmarks(remoteClient: RemoteClient, jobsToRun: number = remoteClient.getPendingBenchmarks()) {89 if (this.isIdle() && remoteClient.getPendingBenchmarks() > 0) {90 this.state = AgentState.BUSY;91 const iterator = Array.from(Array(jobsToRun), (x, index) => index + 1);92 const runnerConfig: any = { uri: this.uri, specs: remoteClient.getSpecs(), jobs: 1, options: {} };93 if (this.token) {94 runnerConfig.options.authToken = this.token;95 }96 try {97 for (const job of iterator) {98 console.log(`[REMOTE_AGENT] Running job ${job} of ${jobsToRun}`);99 const benchmarkBuild = await remoteClient.requestJob();100 this.runner = new RunnerRemote([benchmarkBuild], remoteClient, runnerConfig);101 const results = await this.runner.run();102 remoteClient.sendResults(results);103 console.log(`[REMOTE_AGENT_${this.getId()}] Completed job ${job} of ${jobsToRun}`);...
Using AI Code Generation
1var bestBenchmarker = require('best-benchmarker');2var benchmarker = new bestBenchmarker();3var pendingBenchmarks = benchmarker.getPendingBenchmarks();4console.log(pendingBenchmarks);5var bestBenchmarker = require('best-benchmarker');6var benchmarker = new bestBenchmarker();7var pendingBenchmarks = benchmarker.getPendingBenchmarks();8console.log(pendingBenchmarks);9var bestBenchmarker = require('best-benchmarker');10var benchmarker = new bestBenchmarker();11var pendingBenchmarks = benchmarker.getPendingBenchmarks();12console.log(pendingBenchmarks);13var bestBenchmarker = require('best-benchmarker');14var benchmarker = new bestBenchmarker();15var pendingBenchmarks = benchmarker.getPendingBenchmarks();16console.log(pendingBenchmarks);17var bestBenchmarker = require('best-benchmarker');18var benchmarker = new bestBenchmarker();19var pendingBenchmarks = benchmarker.getPendingBenchmarks();20console.log(pendingBenchmarks);21var bestBenchmarker = require('best-benchmarker');22var benchmarker = new bestBenchmarker();23var pendingBenchmarks = benchmarker.getPendingBenchmarks();24console.log(pendingBenchmarks);25var bestBenchmarker = require('best-benchmarker');
Using AI Code Generation
1var BestBenchmarks = require('./bestBenchmarks');2var bestBenchmarks = new BestBenchmarks();3var pendingBenchmarks = bestBenchmarks.getPendingBenchmarks();4var completedBenchmarks = bestBenchmarks.getCompletedBenchmarks();5console.log('Pending Benchmarks: ' + pendingBenchmarks);6console.log('Completed Benchmarks: ' + completedBenchmarks);7class BestBenchmarks {8 getPendingBenchmarks() {9 return ['Benchmark 1', 'Benchmark 2', 'Benchmark 3'];10 }11 getCompletedBenchmarks() {12 return ['Benchmark 4', 'Benchmark 5', 'Benchmark 6'];13 }14}15module.exports = BestBenchmarks;16class BestBenchmarks {17 getPendingBenchmarks() {18 return ['Benchmark 1', 'Benchmark 2', 'Benchmark 3'];19 }20 getCompletedBenchmarks() {21 return ['Benchmark 4', 'Benchmark 5', 'Benchmark 6'];22 }23}24exports.BestBenchmarks = BestBenchmarks;
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!