Source file src/os/readfrom_sendfile_test.go

     1  // Copyright 2024 The Go 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  //go:build linux || solaris
     6  
     7  package os_test
     8  
     9  import (
    10  	"io"
    11  	. "os"
    12  	"testing"
    13  )
    14  
    15  func BenchmarkSendFile(b *testing.B) {
    16  	hook := hookSendFileTB(b)
    17  
    18  	// 1 GiB file size for copy.
    19  	const fileSize = 1 << 30
    20  
    21  	src, _ := createTempFile(b, "benchmark-sendfile-src", int64(fileSize))
    22  	dst, err := CreateTemp(b.TempDir(), "benchmark-sendfile-dst")
    23  	if err != nil {
    24  		b.Fatalf("failed to create temporary file of destination: %v", err)
    25  	}
    26  	b.Cleanup(func() {
    27  		dst.Close()
    28  	})
    29  
    30  	b.ReportAllocs()
    31  	b.SetBytes(int64(fileSize))
    32  	b.ResetTimer()
    33  
    34  	for i := 0; i <= b.N; i++ {
    35  		sent, err := io.Copy(dst, src)
    36  
    37  		if err != nil {
    38  			b.Fatalf("failed to copy data: %v", err)
    39  		}
    40  		if !hook.called {
    41  			b.Fatalf("should have called the sendfile(2)")
    42  		}
    43  		if sent != int64(fileSize) {
    44  			b.Fatalf("sent %d bytes, want %d", sent, fileSize)
    45  		}
    46  
    47  		// Rewind the files for the next iteration.
    48  		if _, err := src.Seek(0, io.SeekStart); err != nil {
    49  			b.Fatalf("failed to rewind the source file: %v", err)
    50  		}
    51  		if _, err := dst.Seek(0, io.SeekStart); err != nil {
    52  			b.Fatalf("failed to rewind the destination file: %v", err)
    53  		}
    54  	}
    55  }
    56  

View as plain text