forked from UNN/2026-rff_mp
Начало написания тестов
This commit is contained in:
parent
b9c4421127
commit
bc6ece83d0
|
|
@ -1,4 +1,4 @@
|
|||
package resulter
|
||||
package csvwriter
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
|
|
@ -17,9 +17,12 @@ type BenchmarkResult struct {
|
|||
func (b *BenchmarkResult) ToString() string {
|
||||
return fmt.Sprintf("%s %s %s %f", b.Structure, b.Mode, b.Operation, b.Time)
|
||||
}
|
||||
func (b *BenchmarkResult) ToStrings() []string {
|
||||
return []string{b.Structure, b.Mode, b.Operation, fmt.Sprintf("%d", b.Time)}
|
||||
}
|
||||
|
||||
// AppendRaw дописывает произвольные строки в CSV
|
||||
func AppendRaw(rows [][]string) error {
|
||||
func AppendRaw(results []BenchmarkResult) error {
|
||||
|
||||
filename := filepath.Join("results", "benchmarks.csv")
|
||||
|
||||
|
|
@ -32,5 +35,11 @@ func AppendRaw(rows [][]string) error {
|
|||
writer := csv.NewWriter(file)
|
||||
defer writer.Flush()
|
||||
|
||||
rows := make([][]string, len(results))
|
||||
|
||||
for i, res := range results {
|
||||
rows[i] = res.ToStrings()
|
||||
}
|
||||
|
||||
return writer.WriteAll(rows) // WriteAll пишет всё сразу
|
||||
}
|
||||
|
|
@ -99,7 +99,9 @@ func (ll *LinkedList) Delete(targetName string) (*LinkedList, bool) {
|
|||
return nil, false
|
||||
}
|
||||
if ll.data.Name == targetName {
|
||||
return ll.next, true
|
||||
ll.data = ll.next.data
|
||||
ll.next = ll.next.next
|
||||
return ll, true
|
||||
}
|
||||
|
||||
prev := ll
|
||||
|
|
|
|||
152
stepushovgs/data-structures/source/tests/benchmark/main.go
Normal file
152
stepushovgs/data-structures/source/tests/benchmark/main.go
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
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)
|
||||
}
|
||||
|
|
@ -11,13 +11,7 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
countUsers = 10000
|
||||
countRepeat = 5
|
||||
countRandomSearch = 10000
|
||||
countNotExitstSearch = 10
|
||||
countDeletes = 50
|
||||
)
|
||||
|
||||
|
||||
func isInArr(arr []int, length int, target int) bool {
|
||||
for i := 0; i < length; i++ {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user