How to use ModCurrentlyActiveVUsCount method of lib Package

Best K6 code snippet using lib.ModCurrentlyActiveVUsCount

execution.go

Source:execution.go Github

copy

Full Screen

...303// IMPORTANT: for UI/information purposes only, don't use for synchronization.304func (es *ExecutionState) GetCurrentlyActiveVUsCount() int64 {305 return atomic.LoadInt64(es.activeVUs)306}307// ModCurrentlyActiveVUsCount changes the total number of currently active VUs.308//309// IMPORTANT: for UI/information purposes only, don't use for synchronization.310func (es *ExecutionState) ModCurrentlyActiveVUsCount(mod int64) int64 {311 return atomic.AddInt64(es.activeVUs, mod)312}313// GetFullIterationCount returns the total of full (i.e uninterrupted) iterations314// that have been completed so far.315//316// IMPORTANT: for UI/information purposes only, don't use for synchronization.317func (es *ExecutionState) GetFullIterationCount() uint64 {318 return atomic.LoadUint64(es.fullIterationsCount)319}320// AddFullIterations increments the number of full (i.e uninterrupted) iterations321// by the provided amount.322//323// IMPORTANT: for UI/information purposes only, don't use for synchronization.324func (es *ExecutionState) AddFullIterations(count uint64) uint64 {325 return atomic.AddUint64(es.fullIterationsCount, count)326}327// GetPartialIterationCount returns the total of partial (i.e interrupted)328// iterations that have been completed so far.329//330// IMPORTANT: for UI/information purposes only, don't use for synchronization.331func (es *ExecutionState) GetPartialIterationCount() uint64 {332 return atomic.LoadUint64(es.interruptedIterationsCount)333}334// AddInterruptedIterations increments the number of partial (i.e interrupted)335// iterations by the provided amount.336//337// IMPORTANT: for UI/information purposes only, don't use for synchronization.338func (es *ExecutionState) AddInterruptedIterations(count uint64) uint64 {339 return atomic.AddUint64(es.interruptedIterationsCount, count)340}341// SetExecutionStatus changes the current execution status to the supplied value342// and returns the current value.343func (es *ExecutionState) SetExecutionStatus(newStatus ExecutionStatus) (oldStatus ExecutionStatus) {344 return ExecutionStatus(atomic.SwapUint32(es.executionStatus, uint32(newStatus)))345}346// GetCurrentExecutionStatus returns the current execution status. Don't use347// this for synchronization unless you've made the k6 behavior somewhat348// predictable with options like --paused or --linger.349func (es *ExecutionState) GetCurrentExecutionStatus() ExecutionStatus {350 return ExecutionStatus(atomic.LoadUint32(es.executionStatus))351}352// MarkStarted saves the current timestamp as the test start time.353//354// CAUTION: Calling MarkStarted() a second time for the same execution state will355// result in a panic!356func (es *ExecutionState) MarkStarted() {357 if !atomic.CompareAndSwapInt64(es.startTime, 0, time.Now().UnixNano()) {358 panic("the execution scheduler was started a second time")359 }360 es.SetExecutionStatus(ExecutionStatusStarted)361}362// MarkEnded saves the current timestamp as the test end time.363//364// CAUTION: Calling MarkEnded() a second time for the same execution state will365// result in a panic!366func (es *ExecutionState) MarkEnded() {367 if !atomic.CompareAndSwapInt64(es.endTime, 0, time.Now().UnixNano()) {368 panic("the execution scheduler was stopped a second time")369 }370 es.SetExecutionStatus(ExecutionStatusEnded)371}372// HasStarted returns true if the test has actually started executing.373// It will return false while a test is in the init phase, or if it has374// been initially paused. But if will return true if a test is paused375// midway through its execution (see above for details regarding the376// feasibility of that pausing for normal executors).377func (es *ExecutionState) HasStarted() bool {378 return atomic.LoadInt64(es.startTime) != 0379}380// HasEnded returns true if the test has finished executing. It will return381// false until MarkEnded() is called.382func (es *ExecutionState) HasEnded() bool {383 return atomic.LoadInt64(es.endTime) != 0384}385// IsPaused quickly returns whether the test is currently paused, by reading386// the atomic currentPauseTime timestamp387func (es *ExecutionState) IsPaused() bool {388 return atomic.LoadInt64(es.currentPauseTime) != 0389}390// GetCurrentTestRunDuration returns the duration for which the test has already391// ran. If the test hasn't started yet, that's 0. If it has started, but has392// been paused midway through, it will return the time up until the pause time.393// And if it's currently running, it will return the time since the start time.394//395// IMPORTANT: for UI/information purposes only, don't use for synchronization.396func (es *ExecutionState) GetCurrentTestRunDuration() time.Duration {397 startTime := atomic.LoadInt64(es.startTime)398 if startTime == 0 {399 // The test hasn't started yet400 return 0401 }402 es.pauseStateLock.RLock()403 endTime := atomic.LoadInt64(es.endTime)404 pausedDuration := es.totalPausedDuration405 es.pauseStateLock.RUnlock()406 if endTime == 0 {407 pauseTime := atomic.LoadInt64(es.currentPauseTime)408 if pauseTime != 0 {409 endTime = pauseTime410 } else {411 // The test isn't paused or finished, use the current time instead412 endTime = time.Now().UnixNano()413 }414 }415 return time.Duration(endTime-startTime) - pausedDuration416}417// Pause pauses the current execution. It acquires the lock, writes418// the current timestamp in currentPauseTime, and makes a new419// channel for resumeNotify.420// Pause can return an error if the test was already paused.421func (es *ExecutionState) Pause() error {422 es.pauseStateLock.Lock()423 defer es.pauseStateLock.Unlock()424 if !atomic.CompareAndSwapInt64(es.currentPauseTime, 0, time.Now().UnixNano()) {425 return errors.New("test execution was already paused")426 }427 es.resumeNotify = make(chan struct{})428 return nil429}430// Resume unpauses the test execution. Unless the test wasn't431// yet started, it calculates the duration between now and432// the old currentPauseTime and adds it to433// Resume will emit an error if the test wasn't paused.434func (es *ExecutionState) Resume() error {435 es.pauseStateLock.Lock()436 defer es.pauseStateLock.Unlock()437 currentPausedTime := atomic.SwapInt64(es.currentPauseTime, 0)438 if currentPausedTime == 0 {439 return errors.New("test execution wasn't paused")440 }441 // Check that it's not the pause before execution actually starts442 if atomic.LoadInt64(es.startTime) != 0 {443 es.totalPausedDuration += time.Duration(time.Now().UnixNano() - currentPausedTime)444 }445 close(es.resumeNotify)446 return nil447}448// ResumeNotify returns a channel which will be closed (i.e. could449// be read from) as soon as the test execution is resumed.450//451// Since tests would likely be paused only rarely, unless you452// directly need to be notified via a channel that the test453// isn't paused or that it has resumed, it's probably a good454// idea to first use the IsPaused() method, since it will be much455// faster.456//457// And, since tests won't be paused most of the time, it's458// probably better to check for that like this:459// if executionState.IsPaused() {460// <-executionState.ResumeNotify()461// }462func (es *ExecutionState) ResumeNotify() <-chan struct{} {463 es.pauseStateLock.RLock()464 defer es.pauseStateLock.RUnlock()465 return es.resumeNotify466}467// GetPlannedVU tries to get a pre-initialized VU from the buffer channel. This468// shouldn't fail and should generally be an instantaneous action, but if it469// doesn't happen for MaxTimeToWaitForPlannedVU (for example, because the system470// is overloaded), a warning will be printed. If we reach that timeout more than471// MaxRetriesGetPlannedVU number of times, this function will return an error,472// since we either have a bug with some executor, or the machine is very, very473// overloaded.474//475// If modifyActiveVUCount is true, the method would also increment the counter476// for active VUs. In most cases, that's the desired behavior, but some477// executors might have to retrieve their reserved VUs without using them478// immediately - for example, the externally-controlled executor when the479// configured maxVUs number is greater than the configured starting VUs.480func (es *ExecutionState) GetPlannedVU(logger *logrus.Entry, modifyActiveVUCount bool) (InitializedVU, error) {481 for i := 1; i <= MaxRetriesGetPlannedVU; i++ {482 select {483 case vu := <-es.vus:484 if modifyActiveVUCount {485 es.ModCurrentlyActiveVUsCount(+1)486 }487 // TODO: set environment and exec488 return vu, nil489 case <-time.After(MaxTimeToWaitForPlannedVU):490 logger.Warnf("Could not get a VU from the buffer for %s", time.Duration(i)*MaxTimeToWaitForPlannedVU)491 }492 }493 return nil, fmt.Errorf(494 "could not get a VU from the buffer in %s",495 MaxRetriesGetPlannedVU*MaxTimeToWaitForPlannedVU,496 )497}498// SetInitVUFunc is called by the execution scheduler's init function, and it's499// used for setting the "constructor" function used for the initializing500// unplanned VUs.501//502// TODO: figure out a better dependency injection method?503func (es *ExecutionState) SetInitVUFunc(initVUFunc InitVUFunc) {504 es.initVUFunc = initVUFunc505}506// GetUnplannedVU checks if any unplanned VUs remain to be initialized, and if507// they do, it initializes one and returns it. If all unplanned VUs have already508// been initialized, it returns one from the global vus buffer, but doesn't509// automatically increment the active VUs counter in either case.510//511// IMPORTANT: GetUnplannedVU() doesn't do any checking if the requesting512// executor is actually allowed to have the VU at this particular time.513// Executors are trusted to correctly declare their needs (via their514// GetExecutionRequirements() methods) and then to never ask for more VUs than515// they have specified in those requirements.516func (es *ExecutionState) GetUnplannedVU(ctx context.Context, logger *logrus.Entry) (InitializedVU, error) {517 remVUs := atomic.AddInt64(es.uninitializedUnplannedVUs, -1)518 if remVUs < 0 {519 logger.Debug("Reusing a previously initialized unplanned VU")520 atomic.AddInt64(es.uninitializedUnplannedVUs, 1)521 return es.GetPlannedVU(logger, false)522 }523 logger.Debug("Initializing an unplanned VU, this may affect test results")524 return es.InitializeNewVU(ctx, logger)525}526// InitializeNewVU creates and returns a brand new VU, updating the relevant527// tracking counters.528func (es *ExecutionState) InitializeNewVU(ctx context.Context, logger *logrus.Entry) (InitializedVU, error) {529 if es.initVUFunc == nil {530 return nil, fmt.Errorf("initVUFunc wasn't set in the execution state")531 }532 newVU, err := es.initVUFunc(ctx, logger)533 if err != nil {534 return nil, err535 }536 es.ModInitializedVUsCount(+1)537 return newVU, err538}539// AddInitializedVU is a helper function that adds VUs into the buffer and540// increases the initialized VUs counter.541func (es *ExecutionState) AddInitializedVU(vu InitializedVU) {542 es.vus <- vu543 es.ModInitializedVUsCount(+1)544}545// ReturnVU is a helper function that puts VUs back into the buffer and546// decreases the active VUs counter.547func (es *ExecutionState) ReturnVU(vu InitializedVU, wasActive bool) {548 es.vus <- vu549 if wasActive {550 es.ModCurrentlyActiveVUsCount(-1)551 }552}...

Full Screen

Full Screen

externally_controlled.go

Source:externally_controlled.go Github

copy

Full Screen

...311 wg := sync.WaitGroup{}312 state := rs.executor.executionState313 getVU := func() (lib.InitializedVU, error) {314 wg.Add(1)315 state.ModCurrentlyActiveVUsCount(+1)316 atomic.AddInt64(rs.activeVUsCount, +1)317 return initVU, nil318 }319 returnVU := func(_ lib.InitializedVU) {320 state.ModCurrentlyActiveVUsCount(-1)321 atomic.AddInt64(rs.activeVUsCount, -1)322 wg.Done()323 }324 ctx, cancel := context.WithCancel(rs.ctx)325 return &manualVUHandle{326 vuHandle: newStoppedVUHandle(ctx, getVU, returnVU, &rs.executor.config.BaseConfig, logger),327 initVU: initVU,328 wg: &wg,329 cancelVU: cancel,330 }331}332// externallyControlledRunState is created and initialized by the Run() method333// of the externally controlled executor. It is used to track and modify various334// details of the execution, including handling of live config changes....

Full Screen

Full Screen

execution_test.go

Source:execution_test.go Github

copy

Full Screen

...349 Exec: "default",350 GetNextIterationCounters: func() (uint64, uint64) { return 3, 4 },351 })352 execState := execScheduler.GetState()353 execState.ModCurrentlyActiveVUsCount(+1)354 err = vu.RunOnce()355 assert.NoError(t, err)356 })357 }358}...

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 influxdbConf := influxdb.Config{4 }5 influxdbConf.PushInterval = null.NewString("1s", false)6 influxdbConf.User = null.NewString("admin", false)7 influxdbConf.Password = null.NewString("admin", false)8 influxdbConf.Tags = map[string]string{9 }10 influxdbConf.Client = influxdb.NewClient(influxdbConf)11 influxdbConf.Client.CreateDatabase(influxdbConf.Database)12 influxdbConf.Client.CreateRetentionPolicy(influxdbConf.Database, influxdbConf.RetentionPolicy, influxdbConf.Retention, false, influxdbConf.Default)13 for i := 0; i < 10; i++ {14 influxdbConf.PushMetrics([]stats.Sample{15 {16 Time: time.Now(),17 Value: float64(i),18 },19 })20 }21 time.Sleep(5 * time.Second)22 fmt.Println("done")23}24import (25func main() {26 influxdbConf := influxdb.Config{27 }28 influxdbConf.PushInterval = null.NewString("1s", false)29 influxdbConf.User = null.NewString("admin", false)30 influxdbConf.Password = null.NewString("admin", false)31 influxdbConf.Tags = map[string]string{32 }33 influxdbConf.Client = influxdb.NewClient(influxdbConf)34 influxdbConf.Client.CreateDatabase(influxdbConf.Database)35 influxdbConf.Client.CreateRetentionPolicy(influxdbConf.Database, influxdbConf.RetentionPolicy, influxdbConf.Retention, false,

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r, err := testutils.NewTestRunner()4 if err != nil {5 panic(err)6 }7 libInst := lib.New(r)8 ctx, cancel := libInst.NewVUContext()9 defer cancel()10 vu, err := libInst.NewVU(1, ctx)11 if err != nil {12 panic(err)13 }14 metric := stats.New("my_metric", stats.Counter)15 vu.Metrics.Add(metric, 1)16 vus := libInst.ModCurrentlyActiveVUsCount(1)17 fmt.Println(vus)18 vu.RunOnce()19 time.Sleep(1 * time.Second)20 vus = libInst.ModCurrentlyActiveVUsCount(-1)21 fmt.Println(vus)22}23import (24func main() {25 r, err := testutils.NewTestRunner()26 if err != nil {27 panic(err)28 }29 libInst := lib.New(r)30 ctx, cancel := libInst.NewVUContext()31 defer cancel()32 vu, err := libInst.NewVU(1, ctx)33 if err != nil {34 panic(err)35 }36 metric := stats.New("my_metric", stats.Counter)37 vu.Metrics.Add(metric, 1)

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))4}5import (6func main() {7 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))8}9import (10func main() {11 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))12}13import (14func main() {15 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))16}17import (18func main() {19 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))20}21import (22func main() {23 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))24}25import (26func main() {27 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))28}29import (30func main() {31 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))32}33import (34func main() {35 fmt.Println(lib.ModCurrentlyActiveVUsCount(0))36}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 lib.ModCurrentlyActiveVUsCount(types.VUs{VUs: 10})5}6import (7func ModCurrentlyActiveVUsCount(vus types.VUs) {8}9type VUs struct {10}11import (12func main() {13 fmt.Println("Hello, playground")14 lib.ModCurrentlyActiveVUsCount(types.VUs{VUs: 10})15}16import (17func ModCurrentlyActiveVUsCount(vus types.VUs) {18}19type VUs struct {20}21import (22func main() {23 fmt.Println("Hello, playground")24 lib.ModCurrentlyActiveVUsCount(types.VUs{VUs: 10})25}26import (27func ModCurrentlyActiveVUsCount(vus types.VUs) {28}29type VUs struct {30}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 libInstance := lib.New()4 libInstance.ModCurrentlyActiveVUsCount(10)5 fmt.Println(libInstance.CurrentlyActiveVUsCount())6}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib.ModCurrentlyActiveVUsCount(10)4 fmt.Println(lib.CurrentlyActiveVUsCount)5}6func ModCurrentlyActiveVUsCount(count int) {7}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lib := lib.New()4 vu, _ := lib.NewVU(nil, nil)5 ctx, _ := lib.NewContext(vu)6 scenario := types.NewScenario("test", 1*time.Second, 1*time.Second, 1*time.Second, 1*time.Second)7 ctx.AddScenario(scenario)8 ctx.StartScenario(scenario.Name)9 vuCount := lib.ModCurrentlyActiveVUsCount(ctx, 1)10 fmt.Println("currently active VUs count: ", vuCount)11 ctx.Wait()12 ctx.StopScenario(scenario.Name)13}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m = &lib.ModCurrentlyActiveVUsCount{}4 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))5}6import (7func main() {8 m = &lib.ModMaxAllowedVUsCount{}9 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))10}11import (12func main() {13 m = &lib.ModVUInitProgress{}14 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))15}16import (17func main() {18 m = &lib.ModVUInitDuration{}19 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))20}21import (22func main() {23 m = &lib.ModVUDuration{}24 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))25}26import (27func main() {28 m = &lib.ModIterationDuration{}29 fmt.Println(m.Apply(stats.Sample{Metric: stats.VUs, Value: 5}))30}

Full Screen

Full Screen

ModCurrentlyActiveVUsCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)4 fmt.Println(vuCount)5}6import (7func main() {8 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)9 fmt.Println(vuCount)10}11import (12func main() {13 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)14 fmt.Println(vuCount)15}16import (17func main() {18 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)19 fmt.Println(vuCount)20}21import (22func main() {23 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)24 fmt.Println(vuCount)25}26import (27func main() {28 vuCount = libInstance.ModCurrentlyActiveVUsCount(1)29 fmt.Println(vuCount)30}31import (32func main() {

Full Screen

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 K6 automation tests on LambdaTest cloud grid

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

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful