statping/types/hits/samples.go

42 lines
736 B
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package hits
import (
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2020-03-04 14:20:47 +00:00
"sync"
2020-03-04 10:29:00 +00:00
"time"
)
2020-03-04 14:20:47 +00:00
var SampleHits = 9900.
func Samples() {
tx := DB().Begin()
sg := new(sync.WaitGroup)
2020-03-04 10:29:00 +00:00
createdAt := time.Now().Add(-1 * types.Month)
2020-03-04 14:20:47 +00:00
for i := int64(1); i <= 4; i++ {
sg.Add(1)
2020-03-04 10:29:00 +00:00
p := utils.NewPerlin(2, 2, 5, time.Now().UnixNano())
2020-03-04 14:20:47 +00:00
go func() {
defer sg.Done()
for hi := 0.; hi <= SampleHits; hi++ {
latency := p.Noise1D(hi / 500)
createdAt = createdAt.Add(1 * time.Minute)
hit := &Hit{
Service: i,
CreatedAt: createdAt.UTC(),
Latency: latency,
}
tx = tx.Create(&hit)
2020-03-04 10:29:00 +00:00
}
2020-03-04 14:20:47 +00:00
}()
2020-03-04 10:29:00 +00:00
}
2020-03-04 14:20:47 +00:00
sg.Wait()
2020-03-04 10:29:00 +00:00
2020-03-04 14:20:47 +00:00
if err := tx.Commit().Error(); err != nil {
log.Error(err)
}
2020-03-04 10:29:00 +00:00
}