feat: use stream JSON instead of array.

This commit is contained in:
Andrey Parhomenko 2024-06-06 00:46:14 +05:00
parent e981111774
commit 5177e03bef

View file

@ -33,23 +33,20 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
log.Fatalf("NewAmoClient(...): %s\n", err) log.Fatalf("NewAmoClient(...): %s\n", err)
} }
c.API.SetMRPS(opts.MRPS) c.API.SetMRPS(opts.MRPS)
finalValues := []V{} enc := json.NewEncoder(os.Stdout)
if opts.Indent {
enc.SetIndent("", " ")
}
inter := make(chan os.Signal, 1) inter := make(chan os.Signal, 1)
signal.Notify(inter, os.Interrupt) signal.Notify(inter, os.Interrupt)
go func() { go func() {
<-inter <-inter
enc := json.NewEncoder(os.Stdout) os.Stdout.Sync()
if opts.Indent {
enc.SetIndent("", " ")
}
err = enc.Encode(finalValues)
if err != nil {
log.Fatalf("json.Encode(...): %s\n", err)
}
os.Exit(0) os.Exit(0)
}() }()
finalNum := 0
if opts.All { if opts.All {
page := opts.StartPage page := opts.StartPage
values, next, err := g.GetValues( 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, "%s(...): %s\n", g.GetFuncName(), err,
) )
} }
finalValues = append(finalValues, values...) finalNum += len(values)
if opts.Verbose { if opts.Verbose {
log.Printf("Got %d %s (%d, page %d)\n", 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++ page++
for page <= opts.EndPage && next != nil { for page <= opts.EndPage && next != nil {
values, next, err = next() values, next, err = next()
@ -76,21 +82,23 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
g.GetFuncName(), err, g.GetFuncName(), err,
) )
} }
finalValues = append(finalValues, values...) finalNum += len(values)
if opts.Verbose { if opts.Verbose {
log.Printf("Got %d %s (%d, page %d)\n", log.Printf("Got %d %s (%d, page %d)\n",
len(values), g.GetNameMul(), 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++ page++
} }
bts, err := json.MarshalIndent(finalValues, "", " ")
if err != nil {
log.Fatalf("json.MarshalIndent(...) %s\n", err)
}
os.Stdout.Write(bts)
return
} }
ids := ReadIDs(idStrs) ids := ReadIDs(idStrs)
@ -143,12 +151,18 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
}() }()
for values := range valueChan { 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 { if opts.Verbose {
log.Printf( log.Printf(
"thtread(main) %.2f%%, sument=%d", "thtread(main) %.2f%%, sument=%d",
float32(len(finalValues))/float32(len(ids))*100., float32(finalNum)/float32(len(ids))*100.,
len(finalValues), finalNum,
) )
} }
} }
@ -157,7 +171,7 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
rm := c.API.RequestsMade() rm := c.API.RequestsMade()
log.Printf( log.Printf(
"Summarized got %d %s\n", "Summarized got %d %s\n",
len(finalValues), g.GetNameMul(), finalNum, g.GetNameMul(),
) )
log.Printf( log.Printf(
"Made %d requests in process\n", "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) 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)
}
} }