From bc6ece83d0d8ef134d7afdc36ce2fad030b6f725 Mon Sep 17 00:00:00 2001 From: GordStep Date: Sun, 10 May 2026 19:16:18 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pkg/{resulter => csv_writer}/resulter.go | 13 +- .../pkg/structures/linked_list/linked_list.go | 4 +- .../source/tests/benchmark/main.go | 152 ++++++++++++++++++ .../source/tests/test_ll/main.go | 8 +- 4 files changed, 167 insertions(+), 10 deletions(-) rename stepushovgs/data-structures/source/pkg/{resulter => csv_writer}/resulter.go (69%) create mode 100644 stepushovgs/data-structures/source/tests/benchmark/main.go diff --git a/stepushovgs/data-structures/source/pkg/resulter/resulter.go b/stepushovgs/data-structures/source/pkg/csv_writer/resulter.go similarity index 69% rename from stepushovgs/data-structures/source/pkg/resulter/resulter.go rename to stepushovgs/data-structures/source/pkg/csv_writer/resulter.go index 5dd6c38..ed67788 100644 --- a/stepushovgs/data-structures/source/pkg/resulter/resulter.go +++ b/stepushovgs/data-structures/source/pkg/csv_writer/resulter.go @@ -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 пишет всё сразу } diff --git a/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go b/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go index 85ef19f..eb1467c 100644 --- a/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go +++ b/stepushovgs/data-structures/source/pkg/structures/linked_list/linked_list.go @@ -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 diff --git a/stepushovgs/data-structures/source/tests/benchmark/main.go b/stepushovgs/data-structures/source/tests/benchmark/main.go new file mode 100644 index 0000000..cd0e575 --- /dev/null +++ b/stepushovgs/data-structures/source/tests/benchmark/main.go @@ -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) +} diff --git a/stepushovgs/data-structures/source/tests/test_ll/main.go b/stepushovgs/data-structures/source/tests/test_ll/main.go index 88d48c6..b13d382 100644 --- a/stepushovgs/data-structures/source/tests/test_ll/main.go +++ b/stepushovgs/data-structures/source/tests/test_ll/main.go @@ -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++ {