func TestAScenarioAvoidingConcurrency(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() bak := stdout stdout = new(bytes.Buffer) defer func() { stdout = bak }() // init gg.Testscenario("scenario1", scenario1) // main part err := gg.ReadConfigValidate(noConcurrencyLoadmodel, LoadmodelSchema) if err != nil { t.Fatalf("Error while reading loadmodel config: %s!", err.Error()) } start := time.Now() gg.Exec() // exec the scenario that has been selected in the config file execution := time.Now().Sub(start) // verify total run time of the baseline senario if execution != 360*time.Second { t.Errorf("Error: execution time of scenario1 not as expected: %v\n", execution) } gg.Report(stdout) // verify Report! report := stdout.(*bytes.Buffer).String() if report != ("01_01_teststep, 50.000000, 50.000000, 50.000000, 2000, 0\n") { t.Fatalf("Report output of scenario1 not as expected: %s", report) } }
func TestFirstByteAfterReader(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() tr := testReader{} fbr := newMetricReader(tr) b1 := make([]byte, 4) fbr.Read(b1) body := string(b1) if !(body == "mark") { t.Fatalf("Read buffer was expected '%s', but was: '%v'", "mark", body) } if fbr.firstByteAfter != gogrinder.Elapsed(55*time.Millisecond) { t.Fatalf("First byte was expected after 55 ms but was: %v", fbr.firstByteAfter) } // read a second time b2 := make([]byte, 4) fbr.Read(b2) body = string(b2) if body != "fink" { t.Fatalf("Read buffer was expected '%s', but was: '%v'", "fink", body) } }
func TestThinktimeVariance(t *testing.T) { // create a fake loadmodel for testing var fake = NewTest() fake.status = Running fake.config["Scenario"] = "scenario1" fake.config["ThinkTimeFactor"] = 2.0 fake.config["ThinkTimeVariance"] = 0.1 min, max, avg := 20.0, 20.0, 0.0 time.Freeze(time.Now()) defer time.Unfreeze() for i := 0; i < 1000; i++ { start := time.Now() fake.Thinktime(0.010) sleep := float64(time.Now().Sub(start)) / float64(time.Millisecond) if sleep < min { min = sleep } if max < sleep { max = sleep } avg += sleep } avg = avg / 1000 if min < 18.0 { t.Errorf("Minimum sleep time %f out of defined range!\n", min) } if max >= 22.0 { t.Errorf("Maximum sleep time %f out of defined range!", max) } if avg < 19.9 || avg > 20.1 { t.Fatalf("Average sleep time %f out of defined range!", avg) } }
func TestRunSequential(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() fake := NewTest() fake.config["Scenario"] = "scenario1" var counter int = 0 // assemble testcase tc1 := func(meta *Meta, s Settings) { // check meta if meta.Iteration != counter { t.Errorf("Iteration %d but expected %d!", meta.Iteration, counter) } if meta.User != 0 { t.Error("User meta not as expected!") } time.Sleep(20) counter++ } // run the testcase start := time.Now() fake.DoIterations(tc1, 20, 0, false) if time.Now().Sub(start) != 400 { t.Error("Testcase execution time not as expected!") } if counter != 20 { t.Error("Testcase iteration counter not as expected!") } }
func TestRouteSaveConfig(t *testing.T) { // prepare time.Freeze(time.Now()) defer time.Unfreeze() file, _ := ioutil.TempFile(os.TempDir(), "gogrinder_test") defer os.Remove(file.Name()) srv := TestServer{} scenario := NewTest() scenario.filename = file.Name() srv.test = scenario { config := `{"Scenario":"scenario1","ThinkTimeFactor":2,"ThinkTimeVariance":0.1}` req, _ := http.NewRequest("PUT", "/config", strings.NewReader(config)) rsp := httptest.NewRecorder() srv.Router().ServeHTTP(rsp, req) if rsp.Code != http.StatusOK { t.Fatalf("Status code expected: %s but was: %v", "200", rsp.Code) } } buf, err := ioutil.ReadFile(file.Name()) if err != nil { t.Errorf("Unexpected problem while reading from the config file %s!", file.Name()) } loadmodel := string(buf) if loadmodel != `{"Scenario":"scenario1","ThinkTimeFactor":2,"ThinkTimeVariance":0.1}` { t.Errorf("Config not as expected: %s!", loadmodel) } }
func TestRouteGetConfig(t *testing.T) { // prepare time.Freeze(time.Now()) defer time.Unfreeze() file, _ := ioutil.TempFile(os.TempDir(), "gogrinder_test") defer os.Remove(file.Name()) srv := TestServer{} srv.test = NewTest() loadmodel := `{ "Scenario": "scenario1", "ThinkTimeFactor": 2.0, "ThinkTimeVariance": 0.1 }` srv.test.ReadConfigValidate(loadmodel, LoadmodelSchema) req, _ := http.NewRequest("GET", "/config", nil) rsp := httptest.NewRecorder() srv.Router().ServeHTTP(rsp, req) if rsp.Code != http.StatusOK { t.Fatalf("Status code expected: %s but was: %v", "200", rsp.Code) } config := rsp.Body.String() if config != `{"config":{"Scenario":"scenario1","ThinkTimeFactor":2,`+ `"ThinkTimeVariance":0.1},"mtime":"0001-01-01T00:00:00Z"}` { t.Errorf("Config not as expected: %s!", config) } }
func TestPaceMakerNegativeValue(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() var fake = NewTest() fake.config["Scenario"] = "scenario1" fake.status = Running start := time.Now() fake.paceMaker(-10, 0) if time.Now().Sub(start) != 0 { t.Fatal("Function paceMaker sleep out of range!") } }
func TestThinktimeStops(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() // create a fake loadmodel for testing var fake = NewTest() fake.status = Stopping fake.config["Scenario"] = "scenario1" start := time.Now() fake.Thinktime(10.0) sleep := float64(time.Now().Sub(start)) / float64(time.Millisecond) if sleep != 0 { t.Errorf("Thinktime did not stop! It sleept: %v\n", sleep) } }
func TestPaceMakerStops(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() // create a fake loadmodel for testing var fake = NewTest() fake.status = Stopping fake.config["Scenario"] = "scenario1" start := time.Now() fake.paceMaker(time.Duration(10*time.Second), time.Duration(0)) sleep := float64(time.Now().Sub(start)) / float64(time.Millisecond) if sleep != 0 { t.Errorf("PaceMaker did not stop! It sleept: %v\n", sleep) } }
func TestThinktimeNoVariance(t *testing.T) { // create a fake loadmodel for testing var fake = NewTest() fake.status = Running fake.config["Scenario"] = "scenario1" time.Freeze(time.Now()) defer time.Unfreeze() start := time.Now() fake.Thinktime(0.020) sleep := time.Now().Sub(start) if sleep != 20*time.Millisecond { t.Errorf("Expected to sleep for 20ms but something went wrong: %v", sleep) } }
func TestBaseline2(t *testing.T) { time.Freeze(time.Now()) defer time.Unfreeze() bak := stdout stdout = new(bytes.Buffer) defer func() { stdout = bak }() // we do not need a full loadmodel to run the baseline scenario loadmodel := `{ "Scenario": "baseline", "ThinkTimeFactor": 2.0, "ThinkTimeVariance": 0.1, "PacingVariance": 0.0 }` // no Loadmodel required! ,"Loadmodel": [] // init gg.Testscenario("baseline", baseline2) // main part err := gg.ReadConfigValidate(loadmodel, LoadmodelSchema) if err != nil { t.Fatalf("Error while reading loadmodel config: %s!", err.Error()) } start := time.Now() gg.Exec() // exec the scenario that has been selected in the config file execution := time.Now().Sub(start) // verify total run time of the baseline senario // 18 * (100+100) + 90 = 3690 //if execution <= 369000*time.Millisecond { if execution <= 3690*time.Millisecond { t.Errorf("Error: execution time of scenario1 not as expected: %v\n", execution) } gg.Report(stdout) // verify Report! report := stdout.(*bytes.Buffer).String() if report != ("01_01_teststep, 50.000000, 50.000000, 50.000000, 18, 0\n" + "02_01_teststep, 100.000000, 100.000000, 100.000000, 9, 0\n" + "03_01_teststep, 150.000000, 150.000000, 150.000000, 6, 0\n") { t.Fatalf("Report output of baseline2 scenario not as expected: %s", report) } }
func TestDebug(t *testing.T) { // just run a single testcase once time.Freeze(time.Now()) defer time.Unfreeze() bak := stdout stdout = new(bytes.Buffer) defer func() { stdout = bak }() // we do not need a full loadmodel for this loadmodel := `{ "Scenario": "01_testcase", "ThinkTimeFactor": 2.0, "ThinkTimeVariance": 0.0, "PacingVariance": 0.0 }` // init gg.Reset() gg.Testscenario("baseline", baseline1) gg.Testscenario("01_testcase", tc1) // main part err := gg.ReadConfigValidate(loadmodel, LoadmodelSchema) if err != nil { t.Fatalf("Error while reading loadmodel config: %s!", err.Error()) } start := time.Now() gg.Exec() // exec the scenario that has been selected in the config file execution := time.Now().Sub(start) // verify total run time of the baseline senario // 50+2*50 =150ms if execution != 150*time.Millisecond { t.Errorf("Error: execution time of debug test not as expected: %f ms.\n", d2f(execution)) } gg.Report(stdout) // verify Report! report := stdout.(*bytes.Buffer).String() if report != "01_01_teststep, 50.000000, 50.000000, 50.000000, 1, 0\n" { t.Fatalf("Report output of debug test not as expected: %s", report) } }
func TestReader(t *testing.T) { // prepare time.Freeze(time.Now()) defer time.Unfreeze() size := 18000 latency := 500 * time.Millisecond start := time.Now() slow := NewSlowReader(size, latency) all, err := ioutil.ReadAll(slow) if err != nil { t.Fatalf("Error while reading from SlowReader: %v", err) } elapsed := time.Now().Sub(start) if elapsed != latency { t.Fatalf("Elapsed expected: %v but was: %v", latency, elapsed) } if len(all) != size { t.Fatalf("Buffer sise expected: %d but was: %d", size, len(all)) } }
func TestRouteStartStop(t *testing.T) { // prepare time.Freeze(time.Now()) defer time.Unfreeze() srv := TestServer{} srv.test = NewTest() tc1 := func(meta *Meta, s Settings) { srv.test.Thinktime(0.050) } srv.test.Testscenario("fake", func() { gg.DoIterations(tc1, 500, 0, false) }) loadmodel := `{"Scenario": "fake", "ThinkTimeFactor": 2.0, "ThinkTimeVariance": 0.0 }` srv.test.ReadConfigValidate(loadmodel, LoadmodelSchema) { // startTest req, _ := http.NewRequest("POST", "/test", nil) rsp := httptest.NewRecorder() srv.Router().ServeHTTP(rsp, req) if rsp.Code != http.StatusOK { t.Fatalf("Status code expected: %s but was: %v", "200", rsp.Code) } } // another fake clock problem here! // if srv.test.status != running { // t.Fatalf("Status code expected: %v but was: %v", running, srv.test.status) // } { // stopTest req, _ := http.NewRequest("DELETE", "/test", nil) rsp := httptest.NewRecorder() srv.Router().ServeHTTP(rsp, req) if rsp.Code != http.StatusOK { t.Fatalf("Status code expected: %s but was: %v", "200", rsp.Code) } } if srv.test.Status() == Running { t.Fatalf("Status code expected not running but was: %v", srv.test.Status()) } }
func TestPaceMakerVariance(t *testing.T) { // create a fake loadmodel for testing var fake = NewTest() fake.status = Running fake.config["Scenario"] = "scenario1" fake.config["ThinkTimeFactor"] = 2.0 fake.config["ThinkTimeVariance"] = 0.1 fake.config["PacingVariance"] = 0.1 min, max, avg := 1000.0, 1000.0, 0.0 time.Freeze(time.Now()) defer time.Unfreeze() for i := 0; i < 1000; i++ { start := time.Now() fake.paceMaker(time.Duration(1*time.Second), time.Duration(0)) sleep := float64(time.Now().Sub(start)) / float64(time.Millisecond) if sleep < min { min = sleep } if max < sleep { max = sleep } avg += sleep } avg = avg / 1000 if min < 900.0 { t.Errorf("Minimum pace time %f out of defined range!\n", min) } if max >= 1100.0 { t.Errorf("Maximum pace time %f out of defined range!", max) } if avg < 990.0 || avg > 1010.0 { t.Fatalf("Average pace time %f out of defined range!", avg) } }