Giter Club home page Giter Club logo

starter's Introduction

GoDoc Go Report Card

starter

For some performance tests, I need to prepare a number of runners and then wait for all of them to be ready. Only then should the experiment be started. In order to coordinate those runners, I've written this metaphorical starter pistol.

Installation

You can import this module into your program using go get:

go get -u github.com/noxer/starter

Example

A short example to get you started:

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/noxer/starter"
)

func main() {
	pistol := starter.Ready()
	wg := &sync.WaitGroup{}
	wg.Add(100)

	for i := 0; i < 100; i++ {
		go func() {
			defer wg.Done()

			prepareRunner()
			pistol.Wait()
			run()
		}()
	}

	fmt.Println("Waiting for the runners to be ready...")
	start := pistol.Steady(100).Go()
	wg.Wait()
	fmt.Printf("It took %s to complete the tasks\n", time.Since(start))
}

func prepareRunner() { time.Sleep(time.Second) }
func run()           { /* do something important */ }

Usage

Initialize a new starter pistol by calling starter.Ready():

pistol := starter.Ready()

Now you pass the pistol to your runners (this is where the metaphor falls apart):

go func() {
    // prepare the runner
    // ...

    // then wait for the bang
    pistol.Wait()

    // and do the work
    // ...
}()

You can now wait for the runners to be ready (aka. waiting):

// wait for 10 runners to be ready
pistol.Steady(10)

It's perfectly fine to call Steady multiple times, you can use it to provide an indication about how many runners are ready:

pistol.Steady(1)
fmt.Println("First runner is ready!")

pistol.Steady(100)
fmt.Println("All runners are ready!")

When all runners are ready, give the signal. Go will return the current timestamp, so you can measure the time it took:

start := pistol.Go()

Be aware that it may take some time for the workers to actually start, waking up a million runners may take 100ms.

Although discouraged, you can reuse the pistol for the next run. This also means that runners that are late to call Wait will block even if Go had previously been called.

License

MIT

starter's People

Contributors

noxer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.