|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "runtime" |
| 7 | + "syscall" |
| 8 | + |
| 9 | + "github.com/RoaringBitmap/gocroaring" |
| 10 | +) |
| 11 | + |
| 12 | +// Exits with code 0 on success, 1 on test failure |
| 13 | +// or 2 on program failure (e.g. OOM or syscall error). |
| 14 | + |
| 15 | +func main() { |
| 16 | + const ( |
| 17 | + N = 10000 |
| 18 | + M = 10000 |
| 19 | + ) |
| 20 | + |
| 21 | + var rusage syscall.Rusage |
| 22 | + var maxrss_0, maxrss_free, maxrss_gc int64 |
| 23 | + |
| 24 | + fmt.Printf("Starting execution... ") |
| 25 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 26 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 27 | + os.Exit(2) |
| 28 | + } |
| 29 | + maxrss_0 = rusage.Maxrss |
| 30 | + fmt.Printf("max RSS: %d bytes\n", maxrss_0) |
| 31 | + |
| 32 | + bitmap := gocroaring.New() |
| 33 | + for i := uint32(0); i < M; i++ { |
| 34 | + bitmap.Add(i*10) |
| 35 | + } |
| 36 | + fmt.Printf("Created reference bitmap... ") |
| 37 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 38 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 39 | + os.Exit(2) |
| 40 | + } |
| 41 | + fmt.Printf("max RSS: %d bytes\n", rusage.Maxrss) |
| 42 | + |
| 43 | + bitmaps := make([]*gocroaring.Bitmap, N) |
| 44 | + fmt.Printf("Allocated slice for %d bitmap pointers... ", N) |
| 45 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 46 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 47 | + os.Exit(2) |
| 48 | + } |
| 49 | + maxrss_free = rusage.Maxrss |
| 50 | + fmt.Printf("max RSS: %d bytes\n", maxrss_free) |
| 51 | + |
| 52 | + for i := range bitmaps { |
| 53 | + bitmaps[i] = bitmap.Clone() |
| 54 | + bitmaps[i].Free() |
| 55 | + } |
| 56 | + fmt.Printf("Copied then explicitly freed %d bitmaps... ", N) |
| 57 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 58 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 59 | + os.Exit(2) |
| 60 | + } |
| 61 | + fmt.Printf("max RSS: %d bytes\n", rusage.Maxrss) |
| 62 | + |
| 63 | + bitmaps = nil |
| 64 | + runtime.GC() |
| 65 | + fmt.Printf("GC'd old bitmaps... ") |
| 66 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 67 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 68 | + os.Exit(2) |
| 69 | + } |
| 70 | + fmt.Printf("max RSS: %d bytes\n", rusage.Maxrss) |
| 71 | + |
| 72 | + bitmaps = make([]*gocroaring.Bitmap, N) |
| 73 | + fmt.Printf("Allocated slice for %d bitmap pointers... ", N) |
| 74 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 75 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 76 | + os.Exit(2) |
| 77 | + } |
| 78 | + fmt.Printf("max RSS: %d bytes\n", rusage.Maxrss) |
| 79 | + |
| 80 | + for i := range bitmaps { |
| 81 | + bitmaps[i] = bitmap.Clone() |
| 82 | + } |
| 83 | + bitmaps = nil |
| 84 | + runtime.GC() |
| 85 | + fmt.Printf("Copied then GC'd %d bitmaps... ", N) |
| 86 | + if err := syscall.Getrusage(syscall.RUSAGE_SELF, &rusage); err != nil { |
| 87 | + fmt.Printf("Getrusage failed with error: %s\n", err) |
| 88 | + os.Exit(2) |
| 89 | + } |
| 90 | + maxrss_gc = rusage.Maxrss |
| 91 | + fmt.Printf("max RSS: %d bytes\n", maxrss_gc) |
| 92 | + |
| 93 | + ballpark_free := maxrss_free - maxrss_0 |
| 94 | + ballpark_gc := maxrss_gc - maxrss_0 |
| 95 | + fmt.Printf("Used ~%d bytes with explicit free\n", ballpark_free) |
| 96 | + fmt.Printf("Used ~%d bytes without explicit free\n", ballpark_gc) |
| 97 | + |
| 98 | + if ballpark_gc < 100*ballpark_free { |
| 99 | + fmt.Printf("Expected a much greater difference!\n") |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | +} |
0 commit comments