153 lines
3.4 KiB
Go
153 lines
3.4 KiB
Go
package benchmark
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
csvwriter "source/pkg/csv_writer"
|
|
ds "source/pkg/data_struct"
|
|
dg "source/pkg/gen_data"
|
|
ll "source/pkg/structures/linked_list"
|
|
|
|
// csv "source/pkg/csv_ri"
|
|
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
countUsers = 10_000
|
|
countRepeat = 5
|
|
countRandomSearch = 100
|
|
countNotExitstSearch = 10
|
|
countDeletes = 50
|
|
)
|
|
|
|
type TestData struct {
|
|
Items []ds.MyData // все записи
|
|
ItemsSorted []ds.MyData // все записи отсортированные
|
|
Existing []ds.MyData // для поиска (существующие)
|
|
NonExisting []ds.MyData // для поиска (несуществующие)
|
|
ToDelete []ds.MyData // для удаления
|
|
}
|
|
|
|
type DataStructure interface {
|
|
Insert(data ds.MyData)
|
|
Search(name string) *ds.MyData
|
|
Delete(name string) bool
|
|
Size() int
|
|
}
|
|
|
|
func uniqueElements(data []ds.MyData) []ds.MyData {
|
|
res := make([]ds.MyData, 0, len(data))
|
|
isUnique := true
|
|
for _, el := range data {
|
|
for _, resEl := range res {
|
|
if el == resEl {
|
|
isUnique = false
|
|
break
|
|
}
|
|
}
|
|
if isUnique {
|
|
res = append(res, el)
|
|
}
|
|
}
|
|
|
|
return res
|
|
}
|
|
|
|
func GenerateTestData() TestData {
|
|
items := dg.RecordsShuffled(countUsers)
|
|
itemsSort := ds.QSort(items, 0, len(items)-1)
|
|
|
|
uniqueItems := uniqueElements(items)
|
|
existing := make([]ds.MyData, countRandomSearch)
|
|
// notExisting := [countNotExitstSearch]ds.MyData{}
|
|
notExisting := make([]ds.MyData, countNotExitstSearch)
|
|
toDelete := make([]ds.MyData, countDeletes)
|
|
|
|
countUniq := len(uniqueItems)
|
|
for i := 0; i < countUniq; i++ {
|
|
// randInd := rand.Intn(countUsers)
|
|
randInd := rand.Intn(countUniq)
|
|
existing[i] = uniqueItems[randInd]
|
|
// fmt.Println(randInd)
|
|
}
|
|
|
|
for i := 0; i < countUniq; i++ {
|
|
// randInd := rand.Intn(countUsers)
|
|
randInd := rand.Intn(10)
|
|
name := fmt.Sprintf("User_%d", randInd)
|
|
notExisting[i] = *ds.NewData(name, "")
|
|
// fmt.Println(randInd)
|
|
}
|
|
|
|
return TestData{
|
|
Items: items,
|
|
ItemsSorted: itemsSort,
|
|
Existing: existing,
|
|
NonExisting: notExisting,
|
|
ToDelete: toDelete,
|
|
}
|
|
}
|
|
|
|
func testOneInsert(structure DataStructure, data []ds.MyData) float64 {
|
|
start := time.Now()
|
|
|
|
for _, item := range data {
|
|
structure.Insert(item)
|
|
}
|
|
|
|
return time.Since(start).Seconds()
|
|
}
|
|
|
|
func testRepInsert(structure DataStructure, data []ds.MyData, nameStruct, mode string) {
|
|
BenchRes := make([]csvwriter.BenchmarkResult, 0)
|
|
|
|
allTestTime := time.Now()
|
|
averageTime := 0.
|
|
|
|
// Тест Слчайной вставки
|
|
|
|
for i := 0; i < countRepeat; i++ {
|
|
|
|
head := structure
|
|
resTime := testOneInsert(head, data)
|
|
|
|
averageTime += resTime
|
|
BenchRes = append(BenchRes, csvwriter.BenchmarkResult{
|
|
Structure: nameStruct,
|
|
Mode: mode,
|
|
Operation: "Вставка",
|
|
Time: resTime,
|
|
})
|
|
}
|
|
|
|
averageTime = time.Since(allTestTime).Seconds() / countRepeat
|
|
|
|
BenchRes = append(BenchRes, csvwriter.BenchmarkResult{
|
|
Structure: nameStruct,
|
|
Mode: mode,
|
|
Operation: "Вставка",
|
|
Time: averageTime,
|
|
})
|
|
|
|
csvwriter.AppendRaw(BenchRes)
|
|
}
|
|
|
|
func Test(nameStruct string, structure DataStructure, data TestData) {
|
|
// BenchRes := make([]csvwriter.BenchmarkResult, 0)
|
|
|
|
// allTestTime := time.Now()
|
|
|
|
testRepInsert(structure, data.Items, nameStruct, "Случайный")
|
|
testRepInsert(structure, data.ItemsSorted, nameStruct, "Отсортированный")
|
|
|
|
}
|
|
|
|
func main() {
|
|
testData := GenerateTestData()
|
|
|
|
var head_ll *ll.LinkedList = nil
|
|
|
|
Test("Связный список", head_ll, testData)
|
|
}
|