github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/cmd/slicer/memiter.go (about) 1 // Copyright 2019 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache 2.0 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "context" 9 "flag" 10 "fmt" 11 "log" 12 "os" 13 14 "github.com/grailbio/bigslice" 15 "github.com/grailbio/bigslice/exec" 16 ) 17 18 var memiterBaseline = bigslice.Func(func(n int, size int) (slice bigslice.Slice) { 19 slice = bigslice.Const(1, make([]int, n)) 20 slice = bigslice.Map(slice, func(i int) []byte { 21 return make([]byte, size) 22 }) 23 return slice 24 }) 25 26 var memiterTest = bigslice.Func(func(baseline bigslice.Slice) bigslice.Slice { 27 return bigslice.Map(baseline, func(p []byte) []byte { 28 q := make([]byte, len(p)) 29 copy(q, p) 30 return q 31 }) 32 }) 33 34 func memiter(sess *exec.Session, args []string) error { 35 var ( 36 flags = flag.NewFlagSet("memiter", flag.ExitOnError) 37 nalloc = flags.Int("n", 128, "number of baseline allocations") 38 size = flags.Int("size", 10<<20, "baseline allocation size") 39 niter = flags.Int("iter", 100, "number of iterations") 40 ) 41 flags.Usage = func() { 42 fmt.Fprintln(os.Stderr, `usage: slicer memiter [-n N] [-size S] [-iter I]`) 43 flags.PrintDefaults() 44 os.Exit(2) 45 } 46 if err := flags.Parse(args); err != nil { 47 log.Fatal(err) 48 } 49 if flags.NArg() != 0 { 50 flags.Usage() 51 } 52 ctx := context.Background() 53 54 result, err := sess.Run(ctx, memiterBaseline, *nalloc, *size) 55 if err != nil { 56 return err 57 } 58 for i := 0; i < *niter; i++ { 59 if _, err := sess.Run(ctx, memiterTest, result); err != nil { 60 return fmt.Errorf("iteration %d: %v", i, err) 61 } 62 log.Printf("iteration %d: ok", i) 63 } 64 return nil 65 66 // Gc, heap profile. 67 68 }