本文整理匯總了Golang中github.com/finklabs/ttime.Now函數的典型用法代碼示例。如果您正苦於以下問題:Golang Now函數的具體用法?Golang Now怎麽用?Golang Now使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Now函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
// Run a testcase. Settings are specified in Seconds!
func (test *TestScenario) Run(name string, testcase func(*Meta, Settings),
delay float64, runfor float64, rampup float64, users int, pacing float64,
settings Settings) {
test.wg.Add(1) // the "Scheduler" itself is a goroutine!
go func(test *TestScenario) {
// ramp up the users
defer test.wg.Done()
time.Sleep(time.Duration(delay * float64(time.Second)))
userStart := time.Now()
test.wg.Add(int(users))
for i := 0; i < users; i++ {
// start user
go func(nbr int) {
defer test.wg.Done()
time.Sleep(time.Duration(float64(nbr) * rampup * float64(time.Second)))
for j := 0; time.Now().Sub(userStart) <
time.Duration(runfor*float64(time.Second)); j++ {
// next iteration
start := time.Now()
meta := &Meta{Testcase: name, Iteration: j, User: nbr}
if test.status == Stopping {
break
}
testcase(meta, settings)
if test.status == Stopping {
break
}
test.paceMaker(time.Duration(pacing*float64(time.Second)), time.Now().Sub(start))
}
}(i)
}
}(test)
}
示例2: TestRunSequential
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!")
}
}
示例3: TestThinktimeVariance
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)
}
}
示例4: TestAScenarioAvoidingConcurrency
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)
}
}
示例5: DoIterations
func (test *TestScenario) DoIterations(testcase func(*Meta, Settings),
iterations int, pacing float64, parallel bool) {
f := func(test *TestScenario) {
settings := test.GetSettings()
defer test.wg.Done()
for i := 0; i < iterations; i++ {
start := time.Now()
meta := &Meta{Iteration: i, User: 0}
if test.status == Stopping {
break
}
testcase(meta, settings)
if test.status == Stopping {
break
}
test.paceMaker(time.Duration(pacing*float64(time.Second)), time.Now().Sub(start))
}
}
if parallel {
test.wg.Add(1)
go f(test)
} else {
test.wg.Wait() // sequential processing: wait for running goroutines to finish
test.wg.Add(1)
f(test)
}
}
示例6: TestPaceMakerNegativeValue
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!")
}
}
示例7: TestPaceMakerStops
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)
}
}
示例8: TestThinktimeStops
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)
}
}
示例9: TestThinktimeNoVariance
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)
}
}
示例10: TestHttpMetricUpdate
func TestHttpMetricUpdate(t *testing.T) {
hmr := NewHttpMetricReporter()
// add datapoint
hm := &HttpMetric{gogrinder.Meta{"01_tc", "01_01_ts", 0, 0, gogrinder.Timestamp(time.Now()),
gogrinder.Elapsed(600 * time.Millisecond), "something is wrong!"},
gogrinder.Elapsed(500 * time.Millisecond), 10240, http.StatusOK}
hmr.Update(hm)
// check that datapoint was reported
if exp, got := 600.0, readSummaryVec(hmr.elapsed,
prometheus.Labels{"teststep": "01_01_ts"})[0].GetValue(); exp != got {
t.Errorf("Expected elapsed %d, got %d.", exp, got)
}
if exp, got := 500.0, readSummaryVec(hmr.firstByte,
prometheus.Labels{"teststep": "01_01_ts"})[0].GetValue(); exp != got {
t.Errorf("Expected firstByte %d, got %d.", exp, got)
}
if exp, got := 10.0, readSummaryVec(hmr.bytes,
prometheus.Labels{"teststep": "01_01_ts"})[0].GetValue(); exp != got {
t.Errorf("Expected kb %d, got %d.", exp, got)
}
if exp, got := 1.0, readCounterVec(hmr.error,
prometheus.Labels{"teststep": "01_01_ts"}); exp != got {
t.Errorf("Expected error counter %f, got %f.", exp, got)
}
if exp, got := 1.0, readCounterVec(hmr.code,
prometheus.Labels{"teststep": "01_01_ts", "code": "200"}); exp != got {
t.Errorf("Expected code counter %f, got %f.", exp, got)
}
}
示例11: TestFirstByteAfterReader
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)
}
}
示例12: TestRouteGetConfig
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)
}
}
示例13: TestRouteGetCsv
////////////////////////////////
// test routes
////////////////////////////////
func TestRouteGetCsv(t *testing.T) {
// test with 3 measurements
fake := NewTest()
srv := TestServer{}
srv.test = fake
// put 3 measurements into the fake server
done := fake.Collect() // this needs a collector to unblock update
now := Timestamp(time.Now().UTC())
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(8 * time.Millisecond), Timestamp: now})
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(10 * time.Millisecond), Timestamp: now})
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(2 * time.Millisecond), Timestamp: now})
close(fake.measurements)
<-done
// invoke REST service
req, _ := http.NewRequest("GET", "/csv", nil)
rsp := httptest.NewRecorder()
// I separated the Router() from the actual Webserver()
// In this way I can test routes without running a server
srv.Router().ServeHTTP(rsp, req)
if rsp.Code != http.StatusOK {
t.Fatalf("Status code expected: %v but was: %v", http.StatusOK, rsp.Code)
}
body := rsp.Body.String()
if body != "teststep, avg_ms, min_ms, max_ms, count, error\n"+
"sth, 6.666666, 2.000000, 10.000000, 3, 0\n" {
t.Fatalf("Response not as expected: %s!", body)
}
}
示例14: TestRouteGetStatistics
func TestRouteGetStatistics(t *testing.T) {
// test with 3 measurements
fake := NewTest()
srv := TestServer{}
srv.test = fake
// put 3 measurements into the fake server
done := fake.Collect() // this needs a collector to unblock update
now := time.Now().UTC()
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(8 * time.Millisecond), Timestamp: Timestamp(now)})
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(10 * time.Millisecond), Timestamp: Timestamp(now)})
fake.Update(&Meta{Teststep: "sth", Elapsed: Elapsed(2 * time.Millisecond), Timestamp: Timestamp(now)})
close(fake.measurements)
<-done
// invoke REST service
req, _ := http.NewRequest("GET", "/statistics", nil)
rsp := httptest.NewRecorder()
// I separated the Router() from the actual Webserver()
// In this way I can test routes without running a server
srv.Router().ServeHTTP(rsp, req)
if rsp.Code != http.StatusOK {
t.Fatalf("Status code expected: %s but was: %v", "200", rsp.Code)
}
body := rsp.Body.String()
if body != fmt.Sprintf(`{"results":[{"teststep":"sth","avg_ms":6.666666,"min_ms":2,`+
`"max_ms":10,"count":3,"error":0,"last":"%s"}],"running":false}`,
now.Format(ISO8601)) {
t.Fatalf("Response not as expected: %s", body)
}
}
示例15: TestRouteSaveConfig
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)
}
}