Commit 9f8996ad authored by vicotor's avatar vicotor

update benchmark test

parent 99e11018
...@@ -21,6 +21,7 @@ import ( ...@@ -21,6 +21,7 @@ import (
var ( var (
maxModelId = 1000 maxModelId = 1000
idlist = make([]string, 0, 1000000) idlist = make([]string, 0, 1000000)
workeridList = make([]string, 0, 1000000)
database = "test" database = "test"
once = sync.Once{} once = sync.Once{}
) )
...@@ -41,15 +42,20 @@ func ConnectMongoDB() (*mongo.Client, error) { ...@@ -41,15 +42,20 @@ func ConnectMongoDB() (*mongo.Client, error) {
return client, nil return client, nil
} }
func initdata(client *mongo.Client, count int, running bool, installed bool) []string { func initdata(client *mongo.Client, count int, running bool, installed bool, workerid bool) []string {
t1 := time.Now() t1 := time.Now()
db := NewDBWorker(client, database) db := NewDBWorker(client, database)
dbRunning := NewDBWorkerRunning(client, database) dbRunning := NewDBWorkerRunning(client, database)
dbInstalled := NewDBWorkerInstalled(client, database) dbInstalled := NewDBWorkerInstalled(client, database)
ids := make([]string, 0, count)
// Insert 1,000,000 DbWorkerInfo to operator // Insert 1,000,000 DbWorkerInfo to operator
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
worker := generateAWorker() worker := generateAWorker()
if workerid {
ids = append(ids, worker.WorkerId)
}
result, err := db.InsertWorker(context.Background(), worker) result, err := db.InsertWorker(context.Background(), worker)
if err != nil { if err != nil {
panic(fmt.Sprintf("insert worker failed with err:%s", err)) panic(fmt.Sprintf("insert worker failed with err:%s", err))
...@@ -88,16 +94,18 @@ func initdata(client *mongo.Client, count int, running bool, installed bool) []s ...@@ -88,16 +94,18 @@ func initdata(client *mongo.Client, count int, running bool, installed bool) []s
} }
} }
if !workerid {
id, ok := result.InsertedID.(primitive.ObjectID) id, ok := result.InsertedID.(primitive.ObjectID)
if !ok { if !ok {
panic("inserted id is not primitive.ObjectID") panic("inserted id is not primitive.ObjectID")
} }
idlist = append(idlist, id.Hex()) ids = append(ids, id.Hex())
}
//fmt.Printf("insert worker %s: %v\n", id.Hex(), worker) //fmt.Printf("insert worker %s: %v\n", id.Hex(), worker)
} }
t2 := time.Now() t2 := time.Now()
fmt.Printf("init data cost %s\n", t2.Sub(t1).String()) fmt.Printf("init data cost %s\n", t2.Sub(t1).String())
return idlist return ids
} }
func getRandId(max int) string { func getRandId(max int) string {
...@@ -568,7 +576,7 @@ func BenchmarkDbWorker_UpdateHardware(b *testing.B) { ...@@ -568,7 +576,7 @@ func BenchmarkDbWorker_UpdateHardware(b *testing.B) {
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 10000, false, false) idlist = initdata(client, 10000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
...@@ -599,7 +607,7 @@ func BenchmarkDbWorker_UpdateHardware_Parallel(b *testing.B) { ...@@ -599,7 +607,7 @@ func BenchmarkDbWorker_UpdateHardware_Parallel(b *testing.B) {
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 10000, false, false) idlist = initdata(client, 10000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
...@@ -618,6 +626,45 @@ func BenchmarkDbWorker_UpdateHardware_Parallel(b *testing.B) { ...@@ -618,6 +626,45 @@ func BenchmarkDbWorker_UpdateHardware_Parallel(b *testing.B) {
}) })
} }
func BenchmarkDbWorker_UpdateGPUUsage_Parallel(b *testing.B) {
client, err := ConnectMongoDB()
if err != nil {
log.Fatal(err)
}
b.StopTimer()
db := NewDBWorker(client, database)
defer db.client.Disconnect(context.Background())
once.Do(func() {
db.Clear()
if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err))
}
idlist = initdata(client, 10000, false, false, true)
})
b.StartTimer()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
b.StopTimer()
idx := rand.Intn(len(idlist))
usage := rand.Intn(10) + 50
memfree := int64(rand.Intn(10) + 10)
pwoer := rand.Intn(10) + 20
temp := rand.Intn(10) + 30
gpuusages := []types.GpuUsage{
{0, usage, memfree, pwoer, temp},
{1, usage + 1, memfree + 1, pwoer + 1, temp + 1},
{2, usage + 2, memfree + 2, pwoer + 2, temp + 2},
}
b.StartTimer()
if err := db.UpdateGPUUsage(context.Background(), idlist[idx], gpuusages); err != nil {
panic(fmt.Sprintf("update gpu usage failed with err:%s", err))
}
}
})
}
func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam(b *testing.B) { func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam(b *testing.B) {
client, err := ConnectMongoDB() client, err := ConnectMongoDB()
if err != nil { if err != nil {
...@@ -632,7 +679,7 @@ func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam(b *testing.B) { ...@@ -632,7 +679,7 @@ func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam(b *testing.B) {
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 10000, false, false) idlist = initdata(client, 10000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
b.ResetTimer() b.ResetTimer()
...@@ -664,7 +711,7 @@ func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam_Parallel(b *testi ...@@ -664,7 +711,7 @@ func BenchmarkDbWorker_FindWorkerByInstallModelAndSortByGpuRam_Parallel(b *testi
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 10000, false, false) idlist = initdata(client, 10000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
b.ResetTimer() b.ResetTimer()
...@@ -700,7 +747,7 @@ func BenchmarkDbWorker_FindWorkerByRunningModelAndSortByWaitTime(b *testing.B) { ...@@ -700,7 +747,7 @@ func BenchmarkDbWorker_FindWorkerByRunningModelAndSortByWaitTime(b *testing.B) {
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 1000, false, false) idlist = initdata(client, 1000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
b.ResetTimer() b.ResetTimer()
...@@ -730,7 +777,7 @@ func BenchmarkDbWorker_FindWorkerByRunningModelAndSortByWaitTime_Parallel(b *tes ...@@ -730,7 +777,7 @@ func BenchmarkDbWorker_FindWorkerByRunningModelAndSortByWaitTime_Parallel(b *tes
if err := db.CreateIndex(context.Background()); err != nil { if err := db.CreateIndex(context.Background()); err != nil {
panic(fmt.Sprintf("create index failed with err:%s", err)) panic(fmt.Sprintf("create index failed with err:%s", err))
} }
idlist = initdata(client, 1000, false, false) idlist = initdata(client, 1000, false, false, true)
}) })
b.StartTimer() b.StartTimer()
b.ResetTimer() b.ResetTimer()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment