Loading cmd/stars/main.go 0 → 100644 +50 −0 Original line number Diff line number Diff line package main import ( "flag" "fmt" "os" "time" "repositories.muehmer.net/bsmrgo/stars/pkg/stars/simulator" ) func main() { fmt.Fprintln(os.Stderr, "main(): started.") defer fmt.Fprintln(os.Stderr, "main(): terminated.") var tickDuration time.Duration var timeoutDuration time.Duration flag.DurationVar(&tickDuration, "tick", 50*time.Microsecond, "tick duration for the simulator") flag.DurationVar(&timeoutDuration, "timeout", 10*time.Second, "timeout until main() stops the simulator") flag.Parse() t := time.After(timeoutDuration) stop := make(chan bool) defer func() { close(stop) fmt.Fprintln(os.Stderr, "main(): closed stop channel") }() s := simulator.New(stop, tickDuration) go func() { fmt.Fprintln(os.Stderr, "main(): simulator started.") s.Run() fmt.Fprintln(os.Stderr, "main(): simulator finished.") }() loop: for { select { case <-t: fmt.Fprintln(os.Stderr, "main(): sending stop to simulator") stop <- true break loop } } r := <-s.DoneChannel() fmt.Fprintf(os.Stderr, "main(): simulator statistics: %s\n", r) } pkg/stars/simulator/simulator.go 0 → 100644 +72 −0 Original line number Diff line number Diff line package simulator import ( "fmt" "time" ) // Simulator ... type Simulator struct { config Config stop chan bool done chan Done } // Config ... type Config struct { Duration time.Duration } // Done ... type Done struct { Loops uint64 Ticks uint64 Defaults uint64 } // String ... func (d Done) String() string { return fmt.Sprintf("loops: %d, ticks %d, defaults %d", d.Loops, d.Ticks, d.Defaults) } // New ... func New(stop chan bool, tickDuration time.Duration) *Simulator { c := Config{Duration: tickDuration} return &Simulator{ config: c, stop: stop, } } // DoneChannel ... func (s *Simulator) DoneChannel() chan Done { return s.done } // Run ... func (s *Simulator) Run() { s.done = make(chan Done) defer close(s.done) tick := time.Tick(s.config.Duration) loops := uint64(0) ticks := uint64(0) defaults := uint64(0) for { select { case <-s.stop: s.done <- Done{ Loops: loops, Ticks: ticks, Defaults: defaults, } return case <-tick: ticks++ default: defaults++ } loops++ } } Loading
cmd/stars/main.go 0 → 100644 +50 −0 Original line number Diff line number Diff line package main import ( "flag" "fmt" "os" "time" "repositories.muehmer.net/bsmrgo/stars/pkg/stars/simulator" ) func main() { fmt.Fprintln(os.Stderr, "main(): started.") defer fmt.Fprintln(os.Stderr, "main(): terminated.") var tickDuration time.Duration var timeoutDuration time.Duration flag.DurationVar(&tickDuration, "tick", 50*time.Microsecond, "tick duration for the simulator") flag.DurationVar(&timeoutDuration, "timeout", 10*time.Second, "timeout until main() stops the simulator") flag.Parse() t := time.After(timeoutDuration) stop := make(chan bool) defer func() { close(stop) fmt.Fprintln(os.Stderr, "main(): closed stop channel") }() s := simulator.New(stop, tickDuration) go func() { fmt.Fprintln(os.Stderr, "main(): simulator started.") s.Run() fmt.Fprintln(os.Stderr, "main(): simulator finished.") }() loop: for { select { case <-t: fmt.Fprintln(os.Stderr, "main(): sending stop to simulator") stop <- true break loop } } r := <-s.DoneChannel() fmt.Fprintf(os.Stderr, "main(): simulator statistics: %s\n", r) }
pkg/stars/simulator/simulator.go 0 → 100644 +72 −0 Original line number Diff line number Diff line package simulator import ( "fmt" "time" ) // Simulator ... type Simulator struct { config Config stop chan bool done chan Done } // Config ... type Config struct { Duration time.Duration } // Done ... type Done struct { Loops uint64 Ticks uint64 Defaults uint64 } // String ... func (d Done) String() string { return fmt.Sprintf("loops: %d, ticks %d, defaults %d", d.Loops, d.Ticks, d.Defaults) } // New ... func New(stop chan bool, tickDuration time.Duration) *Simulator { c := Config{Duration: tickDuration} return &Simulator{ config: c, stop: stop, } } // DoneChannel ... func (s *Simulator) DoneChannel() chan Done { return s.done } // Run ... func (s *Simulator) Run() { s.done = make(chan Done) defer close(s.done) tick := time.Tick(s.config.Duration) loops := uint64(0) ticks := uint64(0) defaults := uint64(0) for { select { case <-s.stop: s.done <- Done{ Loops: loops, Ticks: ticks, Defaults: defaults, } return case <-tick: ticks++ default: defaults++ } loops++ } }