From 5177e03bef2768a89dbac74dff19c65b2c0938f2 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 6 Jun 2024 00:46:14 +0500 Subject: [PATCH] feat: use stream JSON instead of array. --- cmd/amocli/getter.go | 68 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/cmd/amocli/getter.go b/cmd/amocli/getter.go index 0961069..d3b89c9 100644 --- a/cmd/amocli/getter.go +++ b/cmd/amocli/getter.go @@ -33,23 +33,20 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { log.Fatalf("NewAmoClient(...): %s\n", err) } c.API.SetMRPS(opts.MRPS) - finalValues := []V{} + enc := json.NewEncoder(os.Stdout) + if opts.Indent { + enc.SetIndent("", " ") + } inter := make(chan os.Signal, 1) signal.Notify(inter, os.Interrupt) go func() { <-inter - enc := json.NewEncoder(os.Stdout) - if opts.Indent { - enc.SetIndent("", " ") - } - err = enc.Encode(finalValues) - if err != nil { - log.Fatalf("json.Encode(...): %s\n", err) - } + os.Stdout.Sync() os.Exit(0) }() + finalNum := 0 if opts.All { page := opts.StartPage values, next, err := g.GetValues( @@ -62,11 +59,20 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { "%s(...): %s\n", g.GetFuncName(), err, ) } - finalValues = append(finalValues, values...) + finalNum += len(values) + if opts.Verbose { log.Printf("Got %d %s (%d, page %d)\n", - len(values), g.GetNameMul(), len(finalValues), page) + len(values), g.GetNameMul(), finalNum, page) } + + for _, value := range values { + err := enc.Encode(value) + if err != nil { + log.Fatalf("json.Encode(...): %s\n", err) + } + } + page++ for page <= opts.EndPage && next != nil { values, next, err = next() @@ -76,21 +82,23 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { g.GetFuncName(), err, ) } - finalValues = append(finalValues, values...) + finalNum += len(values) if opts.Verbose { log.Printf("Got %d %s (%d, page %d)\n", len(values), g.GetNameMul(), - len(finalValues), page) + finalNum, page) } + + for _, value := range values { + err = enc.Encode(value) + if err != nil { + log.Fatalf("json.Encode(...): %s\n", err) + } + } + page++ } - bts, err := json.MarshalIndent(finalValues, "", " ") - if err != nil { - log.Fatalf("json.MarshalIndent(...) %s\n", err) - } - os.Stdout.Write(bts) - return } ids := ReadIDs(idStrs) @@ -143,12 +151,18 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { }() for values := range valueChan { - finalValues = append(finalValues, values...) + finalNum += len(values) + for _, value := range values { + err := enc.Encode(value) + if err != nil { + log.Fatalf("json.Encode(...): %s\n", err) + } + } if opts.Verbose { log.Printf( "thtread(main) %.2f%%, sument=%d", - float32(len(finalValues))/float32(len(ids))*100., - len(finalValues), + float32(finalNum)/float32(len(ids))*100., + finalNum, ) } } @@ -157,7 +171,7 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { rm := c.API.RequestsMade() log.Printf( "Summarized got %d %s\n", - len(finalValues), g.GetNameMul(), + finalNum, g.GetNameMul(), ) log.Printf( "Made %d requests in process\n", @@ -171,12 +185,4 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { log.Printf("RPS = %f\n", float64(rm)/took) } - enc := json.NewEncoder(os.Stdout) - if opts.Indent { - enc.SetIndent("", " ") - } - err = enc.Encode(finalValues) - if err != nil { - log.Fatalf("json.Encode(...): %s\n", err) - } }