github.com/Schaudge/hts@v0.0.0-20240223063651-737b4d69d68c/bam/boom_benchmarks_test.go (about)

     1  // Copyright ©2013 The bíogo Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build boom
     6  
     7  package bam
     8  
     9  import (
    10  	"os"
    11  	"testing"
    12  
    13  	"github.com/biogo/boom"
    14  )
    15  
    16  func BenchmarkReadBoom(b *testing.B) {
    17  	if *file == "" {
    18  		b.Skip("no file specified")
    19  	}
    20  	b.StopTimer()
    21  	br, err := boom.OpenBAM(*file)
    22  	if err != nil {
    23  		b.Fatalf("Open failed: %v", err)
    24  	}
    25  	b.StartTimer()
    26  
    27  	for i := 0; i < b.N; i++ {
    28  		for {
    29  			_, _, err := br.Read()
    30  			if err != nil {
    31  				break
    32  			}
    33  		}
    34  	}
    35  
    36  	br.Close()
    37  }
    38  
    39  func BenchmarkWriteBoom(b *testing.B) {
    40  	if *file == "" {
    41  		b.Skip("no file specified")
    42  	}
    43  	b.StopTimer()
    44  	br, err := boom.OpenBAM(*file)
    45  	if err != nil {
    46  		b.Fatalf("Open failed: %v", err)
    47  	}
    48  	f, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_RDWR, 0666)
    49  	if err != nil {
    50  		b.Fatalf("Open failed: %v", err)
    51  	}
    52  	bw, err := boom.OpenBAMFile(f, "bw", br.Header())
    53  	if err != nil {
    54  		b.Fatalf("NewWriter failed: %v", err)
    55  	}
    56  	r, _, err := br.Read()
    57  	if err != nil {
    58  		b.Fatalf("Read failed: %v", err)
    59  	}
    60  	b.StartTimer()
    61  
    62  	for i := 0; i < b.N; i++ {
    63  		_, err = bw.Write(r)
    64  		if err != nil {
    65  			b.Fatalf("Write failed: %v", err)
    66  		}
    67  	}
    68  
    69  	br.Close()
    70  	f.Close()
    71  }
    72  
    73  func BenchmarkReadFileBoom(b *testing.B) {
    74  	if *file == "" {
    75  		b.Skip("no file specified")
    76  	}
    77  
    78  	for i := 0; i < b.N; i++ {
    79  		br, err := boom.OpenBAM(*file)
    80  		if err != nil {
    81  			b.Fatalf("Open failed: %v", err)
    82  		}
    83  		for {
    84  			_, _, err := br.Read()
    85  			if err != nil {
    86  				break
    87  			}
    88  		}
    89  		br.Close()
    90  	}
    91  }
    92  
    93  func BenchmarkRoundtripFileBoom(b *testing.B) {
    94  	if *file == "" {
    95  		b.Skip("no file specified")
    96  	}
    97  
    98  	for i := 0; i < b.N; i++ {
    99  		br, err := boom.OpenBAM(*file)
   100  		if err != nil {
   101  			b.Fatalf("Open failed: %v", err)
   102  		}
   103  		f, err := os.OpenFile("/dev/null", os.O_APPEND|os.O_RDWR, 0666)
   104  		if err != nil {
   105  			b.Fatalf("Open failed: %v", err)
   106  		}
   107  		bw, err := boom.OpenBAMFile(f, "bw", br.Header())
   108  		if err != nil {
   109  			b.Fatalf("NewWriter failed: %v", err)
   110  		}
   111  		for {
   112  			r, _, err := br.Read()
   113  			if err != nil {
   114  				break
   115  			}
   116  			_, err = bw.Write(r)
   117  			if err != nil {
   118  				b.Fatalf("Write failed: %v", err)
   119  			}
   120  		}
   121  		br.Close()
   122  		f.Close()
   123  	}
   124  }