Esempio n. 1
0
func main() {
	data, err := ioutil.ReadFile("../tmp/test.json")
	if err != nil {
		fmt.Println(err)
		return
	}

	f := jf.NewFactory()
	err = f.LoadJSON(data)
	if err != nil {
		fmt.Println(err)
		return
	}

	machines, err := f.CreateNetwork("main")
	if err != nil {
		fmt.Println(err)
		return
	}

	env := engine.NewEnvironment()
	for _, m := range machines {
		env.AddMachine(m)
	}

	limits := engine.NewLimits()
	limits.Duration = 1.0
	env.Run(limits)
}
Esempio n. 2
0
func Test_RunSimulation(t *testing.T) {
	var m sm.Machine
	err := json.Unmarshal([]byte(`{
		"name" : "Example",
		"states" : [ "idle", "working" ],
		"transitions" : {
			"idle" : { "working" : [ { "type" : "deterministic", "parameter" : 1 } ] },
			"working" : { "idle" : [ { "type" : "deterministic", "parameter" : 1 } ] }
		}
	}`), &m)
	assert.Nil(t, err)

	e := engine.NewEnvironment()
	e.AddMachine(hps.IMachine(&m))

	hits := counters.Hits{}
	working, _ := m.GetState("working")
	working.Entering(hits.StateHandler())

	limits := engine.NewLimits()
	limits.Time = 1.0
	e.Run(limits)

	assert.NotEqual(t, 0, hits.Value)
}
Esempio n. 3
0
func main() {
	r := &Rnd{}
	r.mt = rnd.MT19937New(uint32(time.Now().UnixNano()))

	limits := engine.NewLimits()
	limits.Events = 10000

	f := jf.NewFactory()

	f.SetRnd(hps.IRnd(r))

	content, err := ioutil.ReadFile(os.Args[1])
	if err != nil {
		fmt.Println(err)
		return
	}

	run := func() (float64, error) {
		environment := engine.NewEnvironment()

		err = f.LoadJSON(content)
		if err != nil {
			fmt.Println(err)
			return 0, err
		}

		machines, err := f.CreateNetwork("default")
		if err != nil {
			fmt.Println(err)
			return 0, err
		}

		for _, m := range machines {
			environment.AddMachine(m)
		}

		count := 0

		environment.Iteration(func(env hps.IEnvironment, evt hps.IEvent) {
			count++
			for _, m := range env.Machines() {
				property5, _ := m.Property("property5")
				property5.SetValue("test")
			}
		})

		begin := time.Now()

		result := environment.Run(limits)

		end := time.Now()
		duration := end.Sub(begin)

		statusText := ""
		switch result.Status {
		case engine.COMPLETED_BY_DURATION:
			statusText = "duration limit is reached"
		case engine.COMPLETED_BY_TIME:
			statusText = "time limit is reached"
		case engine.COMPLETED_BY_ITERATIONS:
			statusText = "iterations limit is reached"
		case engine.COMPLETED_BY_IDLE:
			statusText = "no more events"
		}

		fmt.Printf("%v events in %v, terminated by '%s'\n", count, duration, statusText)
		return duration.Seconds(), nil
	}

	runs := []float64{}
	for i := 0; i < 10; i++ {
		duration, _ := run()
		runs = append(runs, duration)
	}
	runsAvg := 0.0
	for i := 0; i < len(runs); i++ {
		runsAvg += runs[i] / float64(len(runs))
	}
	stdDev := 0.0
	for i := 0; i < len(runs); i++ {
		stdDev += (runs[i] - runsAvg) * (runs[i] - runsAvg) / float64(len(runs))
	}
	stdDev = math.Sqrt(stdDev)
	fmt.Printf("10 runs, avg %f, std dev %f\n", runsAvg, stdDev)
}