github.com/tuingking/flamingo@v0.0.0-20220403134817-2796ae0e84ca/internal/product/service__integ_test.go (about)

     1  package product_test
     2  
     3  // import (
     4  // 	"context"
     5  // 	"fmt"
     6  // 	"os"
     7  // 	"path"
     8  // 	"runtime"
     9  // 	"testing"
    10  // 	"time"
    11  
    12  // 	"github.com/tuingking/flamingo/config"
    13  // 	"github.com/tuingking/flamingo/infra/logger"
    14  // 	"github.com/tuingking/flamingo/infra/mysql"
    15  // 	"github.com/tuingking/flamingo/internal/product"
    16  
    17  // 	. "github.com/smartystreets/goconvey/convey"
    18  // )
    19  
    20  // type DI struct {
    21  // 	cfg  config.Config
    22  // 	log  logger.Logger
    23  // 	sql  mysql.MySQL
    24  // 	repo product.Repository
    25  // 	svc  product.Service
    26  // }
    27  
    28  // var (
    29  // 	di DI
    30  // )
    31  
    32  // func TestMain(m *testing.M) {
    33  // 	// change dir
    34  // 	_, filename, _, _ := runtime.Caller(0)
    35  // 	dir := path.Join(path.Dir(filename), "..", "..")
    36  // 	err := os.Chdir(dir)
    37  // 	if err != nil {
    38  // 		panic(err)
    39  // 	}
    40  
    41  // 	di = DI{}
    42  
    43  // 	// init config
    44  // 	di.cfg = config.Init(
    45  // 		config.WithConfigFile("config"),
    46  // 		config.WithConfigType("yaml"),
    47  // 	)
    48  
    49  // 	fmt.Printf("[DEBUG] MaxOpenConn: %+v\n", di.cfg.MySQL.MaxOpenConn)
    50  // 	fmt.Printf("[DEBUG] MaxIdleConn: %+v\n", di.cfg.MySQL.MaxIdleConn)
    51  
    52  // 	// init dependency
    53  // 	di.log = logger.New(logger.Config{Level: "error"})
    54  // 	di.sql = mysql.New(di.cfg.MySQL)
    55  // 	di.repo = product.NewRepository(di.sql)
    56  // 	di.svc = product.NewService(di.cfg.Product.Service, di.log, di.repo)
    57  
    58  // 	defer func() {
    59  // 		di.sql.Close()
    60  // 	}()
    61  
    62  // 	exitVal := m.Run()
    63  
    64  // 	os.Exit(exitVal)
    65  // }
    66  
    67  // func doPrecondition(totalProduct int) (f *os.File, err error) {
    68  // 	return di.svc.GenerateProductsCsv(context.Background(), int64(totalProduct))
    69  // }
    70  
    71  // func Test_CreateBulk_WorkerVariation(t *testing.T) {
    72  // 	var (
    73  // 		totalProduct  = 10000
    74  // 		tWorker       = []int{1, 5, 10, 30, 40, 50}
    75  // 		results       = make(map[int]time.Duration)
    76  // 		minTime       = 10 * time.Minute
    77  // 		optimumWorker = tWorker[0]
    78  // 	)
    79  
    80  // 	Convey(fmt.Sprintf("Test bulk create with %d product", totalProduct), t, func() {
    81  // 		Convey("do precondition", func() {
    82  // 			f, err := doPrecondition(totalProduct)
    83  // 			Convey("precondition should be success", func() {
    84  // 				So(err, ShouldBeNil)
    85  // 			})
    86  // 			defer os.Remove(f.Name())
    87  
    88  // 			for _, worker := range tWorker {
    89  // 				if worker >= totalProduct {
    90  // 					continue
    91  // 				}
    92  
    93  // 				Convey(fmt.Sprintf("Then insert %d task with %d worker", totalProduct, worker), func() {
    94  // 					start := time.Now()
    95  // 					defer func() {
    96  // 						elapsed := time.Since(start)
    97  // 						fmt.Print("took: ", elapsed)
    98  
    99  // 						if elapsed < minTime {
   100  // 							optimumWorker = worker
   101  // 							minTime = elapsed
   102  // 						}
   103  
   104  // 						results[optimumWorker] = elapsed
   105  // 					}()
   106  
   107  // 					svc := product.NewService(product.ConfigSvc{Worker: worker}, di.log, di.repo)
   108  // 					err := svc.BulkCreate(context.Background(), f.Name())
   109  // 					Convey("Product should be created", func() {
   110  // 						So(err, ShouldBeNil)
   111  // 					})
   112  // 				})
   113  // 			}
   114  // 		})
   115  // 	})
   116  
   117  // 	fmt.Printf("🥳🥳🥳 The most optimum to handle %d product is %d worker which took %v\n", totalProduct, optimumWorker, minTime)
   118  // }
   119  
   120  // func Test_CreateBulk_TaskVariation(t *testing.T) {
   121  // 	var (
   122  // 		tProduct = []int{5}
   123  // 		tWorker  = 10
   124  // 	)
   125  
   126  // 	for _, tproduct := range tProduct {
   127  // 		Convey(fmt.Sprintf("Test bulk create with %d product with %d worker", tproduct, tWorker), t, func() {
   128  // 			Convey("do precondition", func() {
   129  // 				f, err := doPrecondition(tproduct)
   130  // 				Convey("precondition should be success", func() {
   131  // 					So(err, ShouldBeNil)
   132  // 				})
   133  // 				defer os.Remove(f.Name())
   134  
   135  // 				Convey(fmt.Sprintf("Then insert %d product", tproduct), func() {
   136  // 					start := time.Now()
   137  // 					defer func() {
   138  // 						elapsed := time.Since(start)
   139  // 						fmt.Print("took: ", elapsed)
   140  // 					}()
   141  
   142  // 					svc := product.NewService(product.ConfigSvc{Worker: tWorker}, di.log, di.repo)
   143  // 					err := svc.BulkCreate(context.Background(), f.Name())
   144  // 					Convey(fmt.Sprintf("%d product should be created", tproduct), func() {
   145  // 						So(err, ShouldBeNil)
   146  // 					})
   147  // 				})
   148  // 			})
   149  // 		})
   150  // 	}
   151  // }