Best K6 code snippet using cloud.flushHTTPTrails
output.go
Source:output.go
...215 case <-aggregationTicker.C:216 out.aggregateHTTPTrails(aggregationWaitPeriod)217 case <-out.stopAggregation:218 out.aggregateHTTPTrails(0)219 out.flushHTTPTrails()220 return221 }222 }223 }()224 }225 out.outputDone.Add(1)226 go func() {227 defer out.outputDone.Done()228 pushTicker := time.NewTicker(out.config.MetricPushInterval.TimeDuration())229 defer pushTicker.Stop()230 for {231 select {232 case <-out.stopSendingMetrics:233 return234 default:235 }236 select {237 case <-out.stopOutput:238 out.pushMetrics()239 return240 case <-pushTicker.C:241 out.pushMetrics()242 }243 }244 }()245}246// Stop gracefully stops all metric emission from the output and when all metric247// samples are emitted, it sends an API to the cloud to finish the test run.248func (out *Output) Stop() error {249 out.logger.Debug("Stopping the cloud output...")250 close(out.stopAggregation)251 out.aggregationDone.Wait() // could be a no-op, if we have never started the aggregation252 out.logger.Debug("Aggregation stopped, stopping metric emission...")253 close(out.stopOutput)254 out.outputDone.Wait()255 out.logger.Debug("Metric emission stopped, calling cloud API...")256 err := out.testFinished()257 if err != nil {258 out.logger.WithFields(logrus.Fields{"error": err}).Warn("Failed to send test finished to the cloud")259 } else {260 out.logger.Debug("Cloud output successfully stopped!")261 }262 return err263}264// Description returns the URL with the test run results.265func (out *Output) Description() string {266 return fmt.Sprintf("cloud (%s)", cloudapi.URLForResults(out.referenceID, out.config))267}268// SetRunStatus receives the latest run status from the Engine.269func (out *Output) SetRunStatus(status lib.RunStatus) {270 out.runStatus = status271}272// SetThresholds receives the thresholds before the output is Start()-ed.273func (out *Output) SetThresholds(scriptThresholds map[string]metrics.Thresholds) {274 thresholds := make(map[string][]*metrics.Threshold)275 for name, t := range scriptThresholds {276 thresholds[name] = append(thresholds[name], t.Thresholds...)277 }278 out.thresholds = thresholds279}280// SetTestRunStopCallback receives the function that stops the engine on error281func (out *Output) SetTestRunStopCallback(stopFunc func(error)) {282 out.engineStopFunc = stopFunc283}284func useCloudTags(source *httpext.Trail) *httpext.Trail {285 name, nameExist := source.Tags.Get("name")286 url, urlExist := source.Tags.Get("url")287 if !nameExist || !urlExist || name == url {288 return source289 }290 newTags := source.Tags.CloneTags()291 newTags["url"] = name292 dest := new(httpext.Trail)293 *dest = *source294 dest.Tags = metrics.IntoSampleTags(&newTags)295 dest.Samples = nil296 return dest297}298// AddMetricSamples receives a set of metric samples. This method is never299// called concurrently, so it defers as much of the work as possible to the300// asynchronous goroutines initialized in Start().301func (out *Output) AddMetricSamples(sampleContainers []metrics.SampleContainer) {302 select {303 case <-out.stopSendingMetrics:304 return305 default:306 }307 if out.referenceID == "" {308 return309 }310 newSamples := []*Sample{}311 newHTTPTrails := []*httpext.Trail{}312 for _, sampleContainer := range sampleContainers {313 switch sc := sampleContainer.(type) {314 case *httpext.Trail:315 sc = useCloudTags(sc)316 // Check if aggregation is enabled,317 if out.config.AggregationPeriod.Duration > 0 {318 newHTTPTrails = append(newHTTPTrails, sc)319 } else {320 newSamples = append(newSamples, NewSampleFromTrail(sc))321 }322 case *netext.NetTrail:323 // TODO: aggregate?324 values := map[string]float64{325 metrics.DataSentName: float64(sc.BytesWritten),326 metrics.DataReceivedName: float64(sc.BytesRead),327 }328 if sc.FullIteration {329 values[metrics.IterationDurationName] = metrics.D(sc.EndTime.Sub(sc.StartTime))330 values[metrics.IterationsName] = 1331 }332 newSamples = append(newSamples, &Sample{333 Type: DataTypeMap,334 Metric: "iter_li_all",335 Data: &SampleDataMap{336 Time: toMicroSecond(sc.GetTime()),337 Tags: sc.GetTags(),338 Values: values,339 },340 })341 default:342 for _, sample := range sampleContainer.GetSamples() {343 newSamples = append(newSamples, &Sample{344 Type: DataTypeSingle,345 Metric: sample.Metric.Name,346 Data: &SampleDataSingle{347 Type: sample.Metric.Type,348 Time: toMicroSecond(sample.Time),349 Tags: sample.Tags,350 Value: sample.Value,351 },352 })353 }354 }355 }356 if len(newSamples) > 0 || len(newHTTPTrails) > 0 {357 out.bufferMutex.Lock()358 out.bufferSamples = append(out.bufferSamples, newSamples...)359 out.bufferHTTPTrails = append(out.bufferHTTPTrails, newHTTPTrails...)360 out.bufferMutex.Unlock()361 }362}363//nolint:funlen,nestif,gocognit364func (out *Output) aggregateHTTPTrails(waitPeriod time.Duration) {365 out.bufferMutex.Lock()366 newHTTPTrails := out.bufferHTTPTrails367 out.bufferHTTPTrails = nil368 out.bufferMutex.Unlock()369 aggrPeriod := int64(out.config.AggregationPeriod.Duration)370 // Distribute all newly buffered HTTP trails into buckets and sub-buckets371 // this key is here specifically to not incur more allocations then necessary372 // if you change this code please run the benchmarks and add the results to the commit message373 var subBucketKey [3]string374 for _, trail := range newHTTPTrails {375 trailTags := trail.GetTags()376 bucketID := trail.GetTime().UnixNano() / aggrPeriod377 // Get or create a time bucket for that trail period378 bucket, ok := out.aggrBuckets[bucketID]379 if !ok {380 bucket = make(map[[3]string]aggregationBucket)381 out.aggrBuckets[bucketID] = bucket382 }383 subBucketKey[0], _ = trailTags.Get("name")384 subBucketKey[1], _ = trailTags.Get("group")385 subBucketKey[2], _ = trailTags.Get("status")386 subBucket, ok := bucket[subBucketKey]387 if !ok {388 subBucket = aggregationBucket{}389 bucket[subBucketKey] = subBucket390 }391 // Either use an existing subbucket key or use the trail tags as a new one392 subSubBucketKey := trailTags393 subSubBucket, ok := subBucket[subSubBucketKey]394 if !ok {395 for sbTags, sb := range subBucket {396 if trailTags.IsEqual(sbTags) {397 subSubBucketKey = sbTags398 subSubBucket = sb399 break400 }401 }402 }403 subBucket[subSubBucketKey] = append(subSubBucket, trail)404 }405 // Which buckets are still new and we'll wait for trails to accumulate before aggregating406 bucketCutoffID := time.Now().Add(-waitPeriod).UnixNano() / aggrPeriod407 iqrRadius := out.config.AggregationOutlierIqrRadius.Float64408 iqrLowerCoef := out.config.AggregationOutlierIqrCoefLower.Float64409 iqrUpperCoef := out.config.AggregationOutlierIqrCoefUpper.Float64410 newSamples := []*Sample{}411 // Handle all aggregation buckets older than bucketCutoffID412 for bucketID, subBuckets := range out.aggrBuckets {413 if bucketID > bucketCutoffID {414 continue415 }416 for _, subBucket := range subBuckets {417 for tags, httpTrails := range subBucket {418 // start := time.Now() // this is in a combination with the log at the end419 trailCount := int64(len(httpTrails))420 if trailCount < out.config.AggregationMinSamples.Int64 {421 for _, trail := range httpTrails {422 newSamples = append(newSamples, NewSampleFromTrail(trail))423 }424 continue425 }426 aggrData := &SampleDataAggregatedHTTPReqs{427 Time: toMicroSecond(time.Unix(0, bucketID*aggrPeriod+aggrPeriod/2)),428 Type: "aggregated_trend",429 Tags: tags,430 }431 if out.config.AggregationSkipOutlierDetection.Bool {432 // Simply add up all HTTP trails, no outlier detection433 for _, trail := range httpTrails {434 aggrData.Add(trail)435 }436 } else {437 connDurations := make(durations, trailCount)438 reqDurations := make(durations, trailCount)439 for i, trail := range httpTrails {440 connDurations[i] = trail.ConnDuration441 reqDurations[i] = trail.Duration442 }443 var minConnDur, maxConnDur, minReqDur, maxReqDur time.Duration444 if trailCount < out.config.AggregationOutlierAlgoThreshold.Int64 {445 // Since there are fewer samples, we'll use the interpolation-enabled and446 // more precise sorting-based algorithm447 minConnDur, maxConnDur = connDurations.SortGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef, true)448 minReqDur, maxReqDur = reqDurations.SortGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef, true)449 } else {450 minConnDur, maxConnDur = connDurations.SelectGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef)451 minReqDur, maxReqDur = reqDurations.SelectGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef)452 }453 for _, trail := range httpTrails {454 if trail.ConnDuration < minConnDur ||455 trail.ConnDuration > maxConnDur ||456 trail.Duration < minReqDur ||457 trail.Duration > maxReqDur {458 // Seems like an outlier, add it as a standalone metric459 newSamples = append(newSamples, NewSampleFromTrail(trail))460 } else {461 // Aggregate the trail462 aggrData.Add(trail)463 }464 }465 }466 aggrData.CalcAverages()467 if aggrData.Count > 0 {468 /*469 out.logger.WithFields(logrus.Fields{470 "http_samples": aggrData.Count,471 "ratio": fmt.Sprintf("%.2f", float64(aggrData.Count)/float64(trailCount)),472 "t": time.Since(start),473 }).Debug("Aggregated HTTP metrics")474 //*/475 newSamples = append(newSamples, &Sample{476 Type: DataTypeAggregatedHTTPReqs,477 Metric: "http_req_li_all",478 Data: aggrData,479 })480 }481 }482 }483 delete(out.aggrBuckets, bucketID)484 }485 if len(newSamples) > 0 {486 out.bufferMutex.Lock()487 out.bufferSamples = append(out.bufferSamples, newSamples...)488 out.bufferMutex.Unlock()489 }490}491func (out *Output) flushHTTPTrails() {492 out.bufferMutex.Lock()493 defer out.bufferMutex.Unlock()494 newSamples := []*Sample{}495 for _, trail := range out.bufferHTTPTrails {496 newSamples = append(newSamples, NewSampleFromTrail(trail))497 }498 for _, bucket := range out.aggrBuckets {499 for _, subBucket := range bucket {500 for _, trails := range subBucket {501 for _, trail := range trails {502 newSamples = append(newSamples, NewSampleFromTrail(trail))503 }504 }505 }...
collector.go
Source:collector.go
...188 case <-aggregationTicker.C:189 c.aggregateHTTPTrails(aggregationWaitPeriod)190 case <-ctx.Done():191 c.aggregateHTTPTrails(0)192 c.flushHTTPTrails()193 close(signalQuit)194 return195 }196 }197 }()198 }199 defer func() {200 wg.Wait()201 c.testFinished()202 }()203 pushTicker := time.NewTicker(time.Duration(c.config.MetricPushInterval.Duration))204 for {205 select {206 case <-c.stopSendingMetricsCh:207 return208 default:209 }210 select {211 case <-quit:212 c.pushMetrics()213 return214 case <-pushTicker.C:215 c.pushMetrics()216 }217 }218}219func useCloudTags(source *httpext.Trail) *httpext.Trail {220 name, nameExist := source.Tags.Get("name")221 url, urlExist := source.Tags.Get("url")222 if !nameExist || !urlExist || name == url {223 return source224 }225 newTags := source.Tags.CloneTags()226 newTags["url"] = name227 dest := new(httpext.Trail)228 *dest = *source229 dest.Tags = stats.IntoSampleTags(&newTags)230 dest.Samples = nil231 return dest232}233// Collect receives a set of samples. This method is never called concurrently, and only while234// the context for Run() is valid, but should defer as much work as possible to Run().235func (c *Collector) Collect(sampleContainers []stats.SampleContainer) {236 select {237 case <-c.stopSendingMetricsCh:238 return239 default:240 }241 if c.referenceID == "" {242 return243 }244 newSamples := []*Sample{}245 newHTTPTrails := []*httpext.Trail{}246 for _, sampleContainer := range sampleContainers {247 switch sc := sampleContainer.(type) {248 case *httpext.Trail:249 sc = useCloudTags(sc)250 // Check if aggregation is enabled,251 if c.config.AggregationPeriod.Duration > 0 {252 newHTTPTrails = append(newHTTPTrails, sc)253 } else {254 newSamples = append(newSamples, NewSampleFromTrail(sc))255 }256 case *netext.NetTrail:257 //TODO: aggregate?258 values := map[string]float64{259 metrics.DataSent.Name: float64(sc.BytesWritten),260 metrics.DataReceived.Name: float64(sc.BytesRead),261 }262 if sc.FullIteration {263 values[metrics.IterationDuration.Name] = stats.D(sc.EndTime.Sub(sc.StartTime))264 values[metrics.Iterations.Name] = 1265 }266 newSamples = append(newSamples, &Sample{267 Type: DataTypeMap,268 Metric: "iter_li_all",269 Data: &SampleDataMap{270 Time: Timestamp(sc.GetTime()),271 Tags: sc.GetTags(),272 Values: values,273 }})274 default:275 for _, sample := range sampleContainer.GetSamples() {276 newSamples = append(newSamples, &Sample{277 Type: DataTypeSingle,278 Metric: sample.Metric.Name,279 Data: &SampleDataSingle{280 Type: sample.Metric.Type,281 Time: Timestamp(sample.Time),282 Tags: sample.Tags,283 Value: sample.Value,284 },285 })286 }287 }288 }289 if len(newSamples) > 0 || len(newHTTPTrails) > 0 {290 c.bufferMutex.Lock()291 c.bufferSamples = append(c.bufferSamples, newSamples...)292 c.bufferHTTPTrails = append(c.bufferHTTPTrails, newHTTPTrails...)293 c.bufferMutex.Unlock()294 }295}296func (c *Collector) aggregateHTTPTrails(waitPeriod time.Duration) {297 c.bufferMutex.Lock()298 newHTTPTrails := c.bufferHTTPTrails299 c.bufferHTTPTrails = nil300 c.bufferMutex.Unlock()301 aggrPeriod := int64(c.config.AggregationPeriod.Duration)302 // Distribute all newly buffered HTTP trails into buckets and sub-buckets303 for _, trail := range newHTTPTrails {304 trailTags := trail.GetTags()305 bucketID := trail.GetTime().UnixNano() / aggrPeriod306 // Get or create a time bucket for that trail period307 bucket, ok := c.aggrBuckets[bucketID]308 if !ok {309 bucket = aggregationBucket{}310 c.aggrBuckets[bucketID] = bucket311 }312 // Either use an existing subbucket key or use the trail tags as a new one313 subBucketKey := trailTags314 subBucket, ok := bucket[subBucketKey]315 if !ok {316 for sbTags, sb := range bucket {317 if trailTags.IsEqual(sbTags) {318 subBucketKey = sbTags319 subBucket = sb320 break321 }322 }323 }324 bucket[subBucketKey] = append(subBucket, trail)325 }326 // Which buckets are still new and we'll wait for trails to accumulate before aggregating327 bucketCutoffID := time.Now().Add(-waitPeriod).UnixNano() / aggrPeriod328 iqrRadius := c.config.AggregationOutlierIqrRadius.Float64329 iqrLowerCoef := c.config.AggregationOutlierIqrCoefLower.Float64330 iqrUpperCoef := c.config.AggregationOutlierIqrCoefUpper.Float64331 newSamples := []*Sample{}332 // Handle all aggregation buckets older than bucketCutoffID333 for bucketID, subBuckets := range c.aggrBuckets {334 if bucketID > bucketCutoffID {335 continue336 }337 for tags, httpTrails := range subBuckets {338 trailCount := int64(len(httpTrails))339 if trailCount < c.config.AggregationMinSamples.Int64 {340 for _, trail := range httpTrails {341 newSamples = append(newSamples, NewSampleFromTrail(trail))342 }343 continue344 }345 aggrData := &SampleDataAggregatedHTTPReqs{346 Time: Timestamp(time.Unix(0, bucketID*aggrPeriod+aggrPeriod/2)),347 Type: "aggregated_trend",348 Tags: tags,349 }350 if c.config.AggregationSkipOutlierDetection.Bool {351 // Simply add up all HTTP trails, no outlier detection352 for _, trail := range httpTrails {353 aggrData.Add(trail)354 }355 } else {356 connDurations := make(durations, trailCount)357 reqDurations := make(durations, trailCount)358 for i, trail := range httpTrails {359 connDurations[i] = trail.ConnDuration360 reqDurations[i] = trail.Duration361 }362 var minConnDur, maxConnDur, minReqDur, maxReqDur time.Duration363 if trailCount < c.config.AggregationOutlierAlgoThreshold.Int64 {364 // Since there are fewer samples, we'll use the interpolation-enabled and365 // more precise sorting-based algorithm366 minConnDur, maxConnDur = connDurations.SortGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef, true)367 minReqDur, maxReqDur = reqDurations.SortGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef, true)368 } else {369 minConnDur, maxConnDur = connDurations.SelectGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef)370 minReqDur, maxReqDur = reqDurations.SelectGetNormalBounds(iqrRadius, iqrLowerCoef, iqrUpperCoef)371 }372 for _, trail := range httpTrails {373 if trail.ConnDuration < minConnDur ||374 trail.ConnDuration > maxConnDur ||375 trail.Duration < minReqDur ||376 trail.Duration > maxReqDur {377 // Seems like an outlier, add it as a standalone metric378 newSamples = append(newSamples, NewSampleFromTrail(trail))379 } else {380 // Aggregate the trail381 aggrData.Add(trail)382 }383 }384 }385 aggrData.CalcAverages()386 if aggrData.Count > 0 {387 logrus.WithFields(logrus.Fields{388 "http_samples": aggrData.Count,389 }).Debug("Aggregated HTTP metrics")390 newSamples = append(newSamples, &Sample{391 Type: DataTypeAggregatedHTTPReqs,392 Metric: "http_req_li_all",393 Data: aggrData,394 })395 }396 }397 delete(c.aggrBuckets, bucketID)398 }399 if len(newSamples) > 0 {400 c.bufferMutex.Lock()401 c.bufferSamples = append(c.bufferSamples, newSamples...)402 c.bufferMutex.Unlock()403 }404}405func (c *Collector) flushHTTPTrails() {406 c.bufferMutex.Lock()407 defer c.bufferMutex.Unlock()408 newSamples := []*Sample{}409 for _, trail := range c.bufferHTTPTrails {410 newSamples = append(newSamples, NewSampleFromTrail(trail))411 }412 for _, bucket := range c.aggrBuckets {413 for _, trails := range bucket {414 for _, trail := range trails {415 newSamples = append(newSamples, NewSampleFromTrail(trail))416 }417 }418 }419 c.bufferHTTPTrails = nil...
flushHTTPTrails
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 b, err := beat.NewBeat("testbeat", "0.0.1", beat.BeatOptions{})5 if err != nil {6 fmt.Println(err)7 }8 pipeline, err := b.Publisher.ConnectWith(beat.ClientConfig{9 Processing: beat.ProcessingConfig{10 },11 Queue: queue.Config{12 },13 Output: outputs.Load(b.Info, nil, common.NewConfig())14 })15 if err != nil {16 fmt.Println(err)17 }18 event := beat.Event{19 Timestamp: time.Now(),20 Fields: common.MapStr{21 },22 }23 pipeline.Publish(event)24 pipeline.Close()25 b.Stop()26}27import (28func main() {29 fmt.Println("Hello World")30 b, err := beat.NewBeat("testbeat", "0.0.1", beat.BeatOptions{})31 if err != nil {32 fmt.Println(err)
flushHTTPTrails
Using AI Code Generation
1import (2func main() {3 ctx := cloud.NewContext("my-project-id", cloud.NewJSONKey("path/to/key.json"))4 logger := logging.NewClient(ctx, "my-log-name")5 logger.FlushHTTPTrails()6}7import (8func main() {9 ctx := cloud.NewContext("my-project-id", cloud.NewJSONKey("path/to/key.json"))10 logger := logging.NewClient(ctx, "my-log-name")11 logger.FlushHTTPTrails()12}13import java.util.*;14public class Main {15 public static void main(String[] args) {16 Scanner sc = new Scanner(System.in);17 int n = sc.nextInt();18 int m = sc.nextInt();19 int[] arr = new int[n];20 for (int i = 0; i < n; i++) {21 arr[i] = sc.nextInt();22 }23 for (int i = 0; i < m; i++) {24 int a = sc.nextInt();25 int b = sc.nextInt();26 int c = sc.nextInt();27 if (a == 1) {28 arr[b - 1] = c;29 } else {30 int sum = 0;31 for (int j = b - 1; j < c; j++) {32 sum += arr[j];33 }34 System.out.println(sum);35 }36 }37 }38}39import java.util.*;40public class Main {41 public static void main(String[] args) {42 Scanner sc = new Scanner(System.in);43 int n = sc.nextInt();44 int m = sc.nextInt();
flushHTTPTrails
Using AI Code Generation
1import (2func main() {3 cloud.FlushHTTPTrails()4 fmt.Println("Hello, playground")5}6import (7func main() {8 cloud.FlushHTTPTrails()9 fmt.Println("Hello, playground")10}11import (12func main() {13 cloud.FlushHTTPTrails()14 fmt.Println("Hello, playground")15}16import (17func main() {18 cloud.FlushHTTPTrails()19 fmt.Println("Hello, playground")20}21import (22func main() {23 cloud.FlushHTTPTrails()24 fmt.Println("Hello, playground")25}26import (27func main() {28 cloud.FlushHTTPTrails()29 fmt.Println("Hello, playground")30}31import (32func main() {33 cloud.FlushHTTPTrails()34 fmt.Println("Hello, playground")35}36import (37func main() {38 cloud.FlushHTTPTrails()39 fmt.Println("Hello, playground")40}41import (42func main() {43 cloud.FlushHTTPTrails()44 fmt.Println("Hello, playground")45}46import (47func main() {48 cloud.FlushHTTPTrails()49 fmt.Println("Hello, playground
flushHTTPTrails
Using AI Code Generation
1import (2func main() {3 client := &http.Client{Transport: apmhttp.WrapRoundTripper(http.DefaultTransport)}4 if err != nil {5 fmt.Println(err)6 }7 apm.DefaultTracer.Flush(nil)8 time.Sleep(1 * time.Second)9}102019/03/28 18:09:54 elasticapm: sending 1 transaction(s)112019/03/28 18:09:54 elasticapm: sending 1 span(s)122019/03/28 18:09:54 elasticapm: sending 1 error(s)132019/03/28 18:09:54 elasticapm: sent 3 document(s) in 1 batches142019/03/28 18:09:54 elasticapm: flushing 1 transaction(s), 1 span(s), 1 error(s)152019/03/28 18:09:54 elasticapm: sent 3 document(s) in 1 batches162019/03/28 18:09:54 elasticapm: flushing 1 transaction(s), 1 span(s), 1 error(s)172019/03/28 18:09:54 elasticapm: sent 3 document(s) in 1 batches
flushHTTPTrails
Using AI Code Generation
1import (2func main() {3 cloud.flushHTTPTrails()4}5import (6func main() {7 cloud.flushHTTPTrails()8}
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!!