mirror of
https://github.com/golang/go
synced 2024-11-26 12:58:06 -07:00
ccf82d5323
Change-Id: Ic463bd6d52e7d1d50d2dbd2122b4d514e0b4af60 Reviewed-on: https://go-review.googlesource.com/c/go/+/428755 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> Run-TryBot: Andy Pan <panjf2000@gmail.com> TryBot-Result: Gopher Robot <gobot@golang.org>
78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
// Copyright 2022 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//go:build linux
|
|
// +build linux
|
|
|
|
package net
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkSendFile(b *testing.B) {
|
|
for i := 0; i <= 10; i++ {
|
|
size := 1 << (i + 10)
|
|
bench := sendFileBench{chunkSize: size}
|
|
b.Run(strconv.Itoa(size), bench.benchSendFile)
|
|
}
|
|
}
|
|
|
|
type sendFileBench struct {
|
|
chunkSize int
|
|
}
|
|
|
|
func (bench sendFileBench) benchSendFile(b *testing.B) {
|
|
fileSize := b.N * bench.chunkSize
|
|
f := createTempFile(b, fileSize)
|
|
fileName := f.Name()
|
|
defer os.Remove(fileName)
|
|
defer f.Close()
|
|
|
|
client, server := spliceTestSocketPair(b, "tcp")
|
|
defer server.Close()
|
|
|
|
cleanUp, err := startSpliceClient(client, "r", bench.chunkSize, fileSize)
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
defer cleanUp()
|
|
|
|
b.ReportAllocs()
|
|
b.SetBytes(int64(bench.chunkSize))
|
|
b.ResetTimer()
|
|
|
|
// Data go from file to socket via sendfile(2).
|
|
sent, err := io.Copy(server, f)
|
|
if err != nil {
|
|
b.Fatalf("failed to copy data with sendfile, error: %v", err)
|
|
}
|
|
if sent != int64(fileSize) {
|
|
b.Fatalf("bytes sent mismatch\n\texpect: %d\n\tgot: %d", fileSize, sent)
|
|
}
|
|
}
|
|
|
|
func createTempFile(b *testing.B, size int) *os.File {
|
|
f, err := os.CreateTemp("", "linux-sendfile-test")
|
|
if err != nil {
|
|
b.Fatalf("failed to create temporary file: %v", err)
|
|
}
|
|
|
|
data := make([]byte, size)
|
|
if _, err := f.Write(data); err != nil {
|
|
b.Fatalf("failed to create and feed the file: %v", err)
|
|
}
|
|
if err := f.Sync(); err != nil {
|
|
b.Fatalf("failed to save the file: %v", err)
|
|
}
|
|
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
|
b.Fatalf("failed to rewind the file: %v", err)
|
|
}
|
|
|
|
return f
|
|
}
|