Best JavaScript code snippet using best
prometheus-observer.js
Source:prometheus-observer.js
1/*2* Licensed under the Apache License, Version 2.0 (the "License");3* you may not use this file except in compliance with the License.4* You may obtain a copy of the License at5*6* http://www.apache.org/licenses/LICENSE-2.07*8* Unless required by applicable law or agreed to in writing, software9* distributed under the License is distributed on an "AS IS" BASIS,10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11* See the License for the specific language governing permissions and12* limitations under the License.13*/14'use strict';15const TestObserverInterface = require('./observer-interface');16const PrometheusQueryClient = require('../../common/prometheus/prometheus-query-client');17const PrometheusQueryHelper = require('../../common/prometheus/prometheus-query-helper');18const Utils = require('../../common/utils/caliper-utils');19const Logger = Utils.getLogger('prometheus-observer');20/**21 * PrometheusObserver class used to observe test statistics via terminal22 */23class PrometheusObserver extends TestObserverInterface {24 /**25 * Constructor26 * @param {object} benchmarkConfig The benchmark configuration object.27 */28 constructor(benchmarkConfig) {29 super(benchmarkConfig);30 // determine interval31 const interval = (this.benchmarkConfig.observer && this.benchmarkConfig.observer.interval) ? this.benchmarkConfig.observer.interval : 1;32 this.observeInterval = interval * 1000;33 // Define the query client34 const queryUrl = this.benchmarkConfig.monitor.prometheus.url;35 this.queryClient = new PrometheusQueryClient(queryUrl);36 Logger.info(`Configured observer to query URL ${queryUrl} every ${interval} seconds`);37 }38 /**39 * Perform an update40 */41 async update() {42 // Update using Prometheus Query43 // -Successful transactions44 const txSuccessQuery = `sum(caliper_txn_success{instance="${this.testName}", round="${this.testRound}"})`;45 const txSuccessCountResponse = await this.queryClient.query(txSuccessQuery, Date.now()/1000);46 const txSuccessCount = txSuccessCountResponse ? PrometheusQueryHelper.extractFirstValueFromQueryResponse(txSuccessCountResponse) : '-';47 // -Failed transactions48 const txFailQuery = `sum(caliper_txn_failure{instance="${this.testName}", round="${this.testRound}"})`;49 const txFailCountResponse = await this.queryClient.query(txFailQuery, Date.now()/1000);50 const txFailCount = txFailCountResponse ? PrometheusQueryHelper.extractFirstValueFromQueryResponse(txFailCountResponse) : '-';51 // -Pending transactions52 const txPendingQuery = `sum(caliper_txn_pending{instance="${this.testName}", round="${this.testRound}"})`;53 const txPendingCountResponse = await this.queryClient.query(txPendingQuery, Date.now()/1000);54 const txPendingCount = txPendingCountResponse ? PrometheusQueryHelper.extractFirstValueFromQueryResponse(txPendingCountResponse) : '-';55 // Could use query on submitted, but quicker to do addition here than use query service56 Logger.info('[' + this.testName + ' Round ' + this.testRound + ' Transaction Info] - Submitted: ' + (txSuccessCount + txFailCount + txPendingCount) +57 ' Succ: ' + txSuccessCount +58 ' Fail:' + txFailCount +59 ' Unfinished:' + txPendingCount);60 }61 /**62 * Start observing the test output63 * @param {ClientOrchestrator} clientOrchestrator the client orchestrator64 */65 startWatch(clientOrchestrator) {66 Logger.info(`Starting observer cycle with interval ${this.observeInterval} ms`);67 this.clientOrchestrator = clientOrchestrator;68 if(!this.observeIntervalObject) {69 // start an interval to query updates70 const self = this;71 this.observeIntervalObject = setInterval(async() => { await self.update(); }, this.observeInterval);72 }73 }74 /**75 * Stop watching the test output76 * @async77 */78 async stopWatch() {79 if(this.observeIntervalObject) {80 clearInterval(this.observeIntervalObject);81 this.observeIntervalObject = null;82 await Utils.sleep(this.observeInterval);83 await this.update();84 }85 }86 /**87 * Set the test name to be reported88 * @param {String} name the benchmark name89 */90 setBenchmark(name) {91 this.testName = name;92 }93 /**94 * Set the test round for the watcher95 * @param{*} roundIdx the round index96 */97 setRound(roundIdx) {98 this.testRound = roundIdx;99 }100}101/**102 * Creates a new PrometheusObserver instance.103 * @param {object} benchmarkConfig The benchmark configuration object.104 * @return {TestObserverInterface} The PrometheusObserver instance.105 */106function createTestObserver(benchmarkConfig) {107 return new PrometheusObserver(benchmarkConfig);108}...
null-observer.js
Source:null-observer.js
1/*2* Licensed under the Apache License, Version 2.0 (the "License");3* you may not use this file except in compliance with the License.4* You may obtain a copy of the License at5*6* http://www.apache.org/licenses/LICENSE-2.07*8* Unless required by applicable law or agreed to in writing, software9* distributed under the License is distributed on an "AS IS" BASIS,10* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11* See the License for the specific language governing permissions and12* limitations under the License.13*/14'use strict';15const TestObserverInterface = require('./observer-interface');16const Logger = require('../../common/utils/caliper-utils').getLogger('null-observer');17/**18 * NullObserver class used to omit test statistics observation19 */20class NullObserver extends TestObserverInterface {21 /**22 * Constructor23 * @param {object} benchmarkConfig The benchmark configuration object.24 */25 constructor(benchmarkConfig) {26 super(benchmarkConfig);27 Logger.info('Configured "null" observer');28 }29 /**30 * Perform an update31 */32 async update() {33 // No action34 Logger.debug('No action taken by NullObserver on update');35 }36 /**37 * Start observing the test output38 * @param {ClientOrchestrator} clientOrchestrator the client orchestrator39 */40 startWatch(clientOrchestrator) {41 Logger.debug('No action taken by NullObserver on startWatch');42 }43 /**44 * Stop watching the test output45 * @async46 */47 async stopWatch() {48 Logger.debug('No action taken by NullObserver on stopWatch');49 }50 /**51 * Set the test name to be reported52 * @param {String} name the benchmark name53 */54 setBenchmark(name) {55 Logger.debug('No action taken by NullObserver on setBenchmark');56 }57 /**58 * Set the test round for the watcher59 * @param{*} roundIdx the round index60 */61 setRound(roundIdx) {62 Logger.debug('No action taken by NullObserver on setRound');63 }64}65/**66 * Creates a new NullObserver instance.67 * @param {object} benchmarkConfig The benchmark configuration object.68 * @return {TestObserverInterface} The NullObserver instance.69 */70function createTestObserver(benchmarkConfig) {71 return new NullObserver(benchmarkConfig);72}...
util.ts
Source:util.ts
1import { Language, BenchmarkConfiguration, Benchmark } from './types'2export const getLanguage = (app: string): Language => app.split('-')[0] as Language3export const getFileExtension = (language: Language) => (language === 'ts' ? 'tsx' : 'js')4export const getBenchmark = (benchmarkConfig: BenchmarkConfiguration): [Benchmark, {}] =>...
Using AI Code Generation
1var Benchmark = require('benchmark');2var suite = new Benchmark.Suite;3var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');4var bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();5suite.add('BestTimeToBuyAndSellStock#benchmarkConfig', function() {6 bestTimeToBuyAndSellStock.benchmarkConfig();7})8.on('cycle', function(event) {9 console.log(String(event.target));10})11.on('complete', function() {12 console.log('Fastest is ' + this.filter('fastest').map('name'));13})14.run({ 'async': true });
Using AI Code Generation
1var Benchmark = require('benchmark');2var suite = new Benchmark.Suite;3var fs = require('fs');4var output = fs.createWriteStream('results.txt');5suite.add('RegExp#test', function() {6 /o/.test('Hello World!');7})8.add('String#indexOf', function() {9 'Hello World!'.indexOf('o') > -1;10})11.add('String#match', function() {12 !!'Hello World!'.match(/o/);13})14.on('cycle', function(event) {15 output.write(String(event.target) + '\n');16})17.on('complete', function() {18 output.write('Fastest is ' + this.filter('fastest').map('name') + '\n');19})20.run({ 'async': true });
Using AI Code Generation
1const BestTime = require('./BestTime.js');2var bestTime = new BestTime();3bestTime.benchmarkConfig([test1, test2, test3], 10000, {name: 'test4', verbose: true});4bestTime.benchmark();5var bestTimeResult = bestTime.getBestTime();6var bestMethodResult = bestTime.getBestMethod();7console.log('Best Time: ' + bestTimeResult);8console.log('Best Method: ' + bestMethodResult);9function test1() {10}11function test2() {12}13function test3() {14}
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!!