package benchmarks

import (
	"bufio"
	"fmt"
	"github.com/stretchr/testify/require"
	"net/http"
	"sync"
	"testing"
	"time"
)

//500
//PS D:\GoProject\pena_pj\customer\tests\benchmarks> go test -bench=BenchmarkAccountPipe -benchmem -timeout 30m
//WAITER OK
//goos: windows
//goarch: amd64
//pkg: penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks
//cpu: AMD Ryzen 5 5600H with Radeon Graphics
//BenchmarkAccountPipe/COUNT_500-12                      1        540604425700 ns/op      16440056 B/op      77490 allocs/op
//PASS
//ok      penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks  540.669s

//1000
//PS D:\GoProject\pena_pj\customer\tests\benchmarks> go test -bench=BenchmarkAccountPipe -benchmem -timeout 30m
//WAITER OK
//goos: windows
//goarch: amd64
//pkg: penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks
//cpu: AMD Ryzen 5 5600H with Radeon Graphics
//BenchmarkAccountPipe/COUNT_1000-12                     1        1075578283500 ns/op     32757832 B/op     154755 allocs/op
//PASS
//ok      penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks  1075.632s

func BenchmarkAccountPipe(b *testing.B) {
	for _, n := range []int{500} {
		b.Run(fmt.Sprintf("COUNT_%d", n), func(b *testing.B) {
			var wg sync.WaitGroup
			for i := 0; i < b.N; i++ {
				wg.Add(n)
				for j := 0; j < n; j++ {
					go func(j int) {
						defer wg.Done()
						userID := fmt.Sprintf("user%d", j)
						fmt.Println(userID)
						resp, err := http.Get(fmt.Sprintf("http://localhost:3000/account-pipe/%s", userID))
						require.NoError(b, err)
						defer resp.Body.Close()
						scanner := bufio.NewScanner(resp.Body)
						count := 0
						for scanner.Scan() {
							if count == 25 {
								break
							}
							count++
							line := scanner.Text()
							fmt.Println("Received:", line)
						}
						time.Sleep(2 * time.Second)
					}(j)
					time.Sleep(time.Second)
				}
				wg.Wait()
				fmt.Println("WAITER OK")
				time.Sleep(1 * time.Second)
			}
		})
	}
}

//500
//PS D:\GoProject\pena_pj\customer\tests\benchmarks> go test -bench=BenchmarkAccountPipeWithWorker -benchmem
//goos: windows
//goarch: amd64
//pkg: penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks
//cpu: AMD Ryzen 5 5600H with Radeon Graphics
//BenchmarkAccountPipeWithWorker/COUNT_500-12                    1        571782250200 ns/op      16471520 B/op      77575 allocs/op
//PASS
//ok      penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks  571.868s

//1000
//PS D:\GoProject\pena_pj\customer\tests\benchmarks> go test -bench=BenchmarkAccountPipeWithWorker -benchmem -timeout 30m
//goos: windows
//goarch: amd64
//pkg: penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks
//cpu: AMD Ryzen 5 5600H with Radeon Graphics
//BenchmarkAccountPipeWithWorker/COUNT_1000-12                   1        1076407088000 ns/op     34201936 B/op     156101 allocs/op
//PASS
//ok      penahub.gitlab.yandexcloud.net/pena-services/customer/tests/benchmarks  1076.521s

func BenchmarkAccountPipeWithWorker(b *testing.B) {
	for _, n := range []int{500} {
		b.Run(fmt.Sprintf("COUNT_%d", n), func(b *testing.B) {
			var wg sync.WaitGroup
			for i := 0; i < b.N; i++ {
				wg.Add(n)
				for j := 0; j < n; j++ {
					go func(j int) {
						defer wg.Done()
						userID := fmt.Sprintf("user%d", j)
						//fmt.Println(userID)
						resp, err := http.Get(fmt.Sprintf("http://localhost:3000/account-pipe-wc/%s", userID))
						require.NoError(b, err)
						defer resp.Body.Close()
						scanner := bufio.NewScanner(resp.Body)
						count := 0
						for scanner.Scan() {
							if count == 25 {
								break
							}
							count++
							line := scanner.Text()
							fmt.Println("Received:", line)
						}
						time.Sleep(2 * time.Second)
					}(j)
					time.Sleep(time.Second)
				}
				wg.Wait()
				time.Sleep(1 * time.Second)
			}
		})
	}
}
