github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/benchmarks/media/ffmpeg_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package media
    15  
    16  import (
    17  	"context"
    18  	"os"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/SagerNet/gvisor/pkg/test/dockerutil"
    23  	"github.com/SagerNet/gvisor/test/benchmarks/harness"
    24  )
    25  
    26  // BenchmarkFfmpeg runs ffmpeg in a container and records runtime.
    27  // BenchmarkFfmpeg should run as root to drop caches.
    28  func BenchmarkFfmpeg(b *testing.B) {
    29  	machine, err := harness.GetMachine()
    30  	if err != nil {
    31  		b.Fatalf("failed to get machine: %v", err)
    32  	}
    33  	defer machine.CleanUp()
    34  
    35  	ctx := context.Background()
    36  	cmd := strings.Split("ffmpeg -i video.mp4 -c:v libx264 -preset veryslow output.mp4", " ")
    37  
    38  	b.ResetTimer()
    39  	b.StopTimer()
    40  
    41  	for i := 0; i < b.N; i++ {
    42  		container := machine.GetContainer(ctx, b)
    43  		defer container.CleanUp(ctx)
    44  		if err := harness.DropCaches(machine); err != nil {
    45  			b.Skipf("failed to drop caches: %v. You probably need root.", err)
    46  		}
    47  
    48  		b.StartTimer()
    49  		if _, err := container.Run(ctx, dockerutil.RunOpts{
    50  			Image: "benchmarks/ffmpeg",
    51  		}, cmd...); err != nil {
    52  			b.Fatalf("failed to run container: %v", err)
    53  		}
    54  		b.StopTimer()
    55  	}
    56  }
    57  
    58  func TestMain(m *testing.M) {
    59  	harness.Init()
    60  	os.Exit(m.Run())
    61  }