github.com/koko1123/flow-go-1@v0.29.6/ledger/complete/checkpoint_benchmark_test.go (about) 1 package complete_test 2 3 import ( 4 "errors" 5 "flag" 6 "fmt" 7 "os" 8 "path/filepath" 9 "strconv" 10 "testing" 11 "time" 12 13 "github.com/rs/zerolog" 14 15 "github.com/koko1123/flow-go-1/ledger/complete/wal" 16 ) 17 18 // To benchmark with local data, using this command: 19 // $ go test -c -o benchmark 20 // $ GOARCH=amd64 GOOS=linux ./benchmark -test.bench=BenchmarkStoreCheckpointV6Concurrently -test.benchmem --checkpointFile ./root.checkpoint 21 var checkpointFile = flag.String("checkpointFile", "", "input checkpoint filename") 22 23 func BenchmarkStoreCheckpointV5(b *testing.B) { 24 benchmarkStoreCheckpoint(b, 5, false) 25 } 26 27 func BenchmarkStoreCheckpointV6(b *testing.B) { 28 benchmarkStoreCheckpoint(b, 6, false) 29 } 30 31 func BenchmarkStoreCheckpointV6Concurrently(b *testing.B) { 32 benchmarkStoreCheckpoint(b, 6, true) 33 } 34 35 func benchmarkStoreCheckpoint(b *testing.B, version int, concurrent bool) { 36 if version != 5 && version != 6 { 37 b.Fatalf("checkpoint file version must be 5 or 6, version %d isn't supported", version) 38 } 39 40 log := zerolog.Nop() 41 42 // Check if checkpoint file exists 43 _, err := os.Stat(*checkpointFile) 44 if errors.Is(err, os.ErrNotExist) { 45 b.Fatalf("input checkpoint file %s doesn't exist", *checkpointFile) 46 } 47 48 dir, fileName := filepath.Split(*checkpointFile) 49 subdir := strconv.FormatInt(time.Now().UnixNano(), 10) 50 outputDir := filepath.Join(dir, subdir) 51 err = os.Mkdir(outputDir, 0755) 52 if err != nil { 53 b.Fatalf("cannot create output dir %s: %s", outputDir, err) 54 } 55 defer func() { 56 // Remove output directory and its contents. 57 os.RemoveAll(outputDir) 58 }() 59 60 // Load checkpoint 61 tries, err := wal.LoadCheckpoint(*checkpointFile, &log) 62 if err != nil { 63 b.Fatalf("cannot load checkpoint: %s", err) 64 } 65 66 start := time.Now() 67 b.ResetTimer() 68 69 // Serialize checkpoint V5. 70 switch version { 71 case 5: 72 err = wal.StoreCheckpointV5(outputDir, fileName, &log, tries...) 73 case 6: 74 if concurrent { 75 err = wal.StoreCheckpointV6Concurrently(tries, outputDir, fileName, &log) 76 } else { 77 err = wal.StoreCheckpointV6SingleThread(tries, outputDir, fileName, &log) 78 } 79 } 80 81 b.StopTimer() 82 elapsed := time.Since(start) 83 84 if err != nil { 85 b.Fatalf("cannot store checkpoint: %s", err) 86 } 87 88 b.ReportMetric(float64(elapsed/time.Millisecond), fmt.Sprintf("storecheckpoint_v%d_time_(ms)", version)) 89 b.ReportAllocs() 90 } 91 92 func BenchmarkLoadCheckpoint(b *testing.B) { 93 // Check if input checkpoint file exists 94 _, err := os.Stat(*checkpointFile) 95 if errors.Is(err, os.ErrNotExist) { 96 b.Fatalf("input checkpoint file %s doesn't exist", *checkpointFile) 97 } 98 99 log := zerolog.Nop() 100 101 start := time.Now() 102 b.ResetTimer() 103 104 // Load checkpoint 105 _, err = wal.LoadCheckpoint(*checkpointFile, &log) 106 107 b.StopTimer() 108 elapsed := time.Since(start) 109 110 if err != nil { 111 b.Fatalf("cannot load checkpoint : %s", err) 112 } 113 114 b.ReportMetric(float64(elapsed/time.Millisecond), "loadcheckpoint_time_(ms)") 115 b.ReportAllocs() 116 }