2022-06-17 17:29:32 +00:00
|
|
|
/*
|
|
|
|
* This file is part of solvar. (https://git.redxen.eu/caskd/solvar)
|
|
|
|
* Copyright (c) 2022 Alex-David Denes
|
|
|
|
*
|
|
|
|
* solvar is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* solvar is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with solvar. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package solvar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkSolve(b *testing.B) {
|
|
|
|
frand := rand.New(rand.NewSource(100))
|
|
|
|
var g DependencyGraph
|
|
|
|
|
|
|
|
idpool := make([]([]byte), frand.Intn(256*256))
|
|
|
|
for pidx := range idpool {
|
|
|
|
idpool[pidx] = make([]byte, 4)
|
|
|
|
frand.Read(idpool[pidx])
|
|
|
|
}
|
|
|
|
|
|
|
|
for range idpool {
|
|
|
|
var o Object
|
|
|
|
for i := 0; i < frand.Intn(256); i++ {
|
|
|
|
o.Id = append(o.Id, Identifier(idpool[frand.Intn(len(idpool))]))
|
|
|
|
}
|
|
|
|
for rid := 0; rid < frand.Intn(256); rid++ {
|
|
|
|
o.Rel = append(o.Rel, Relation{
|
|
|
|
T: func() RelationType {
|
|
|
|
if (frand.Intn(2))%2 == 0 {
|
2022-06-20 06:11:50 +00:00
|
|
|
return RelationTypeNeed
|
2022-06-17 17:29:32 +00:00
|
|
|
} else {
|
2022-06-20 06:11:50 +00:00
|
|
|
return RelationTypeReject
|
2022-06-17 17:29:32 +00:00
|
|
|
}
|
|
|
|
}(),
|
|
|
|
Id: Identifier(idpool[frand.Intn(len(idpool))]),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
g.Add(o)
|
|
|
|
}
|
|
|
|
var c struct {
|
|
|
|
o int
|
|
|
|
i int
|
|
|
|
r struct{ n, r int }
|
|
|
|
}
|
|
|
|
c.o = len(g.ol)
|
|
|
|
for _, v := range g.ol {
|
|
|
|
c.i += len(v.Id)
|
|
|
|
for _, vol := range v.Rel {
|
2022-06-20 06:11:50 +00:00
|
|
|
if vol.T == RelationTypeNeed {
|
2022-06-17 17:29:32 +00:00
|
|
|
c.r.n += 1
|
|
|
|
} else {
|
|
|
|
c.r.r += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.BuildCache()
|
|
|
|
|
|
|
|
b.Logf("Benchmarking Solve() with %v Objects, %v Idenitifers and Relations with %v Need %v Reject", c.o, c.i, c.r.n, c.r.r)
|
|
|
|
b.ResetTimer() // Setup time
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := g.Solve(Identifier(idpool[0]))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal("Failed to solve:", idpool[0], err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|