gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"gvisor.dev/gvisor/pkg/test/dockerutil"
    23  	"gvisor.dev/gvisor/test/benchmarks/harness"
    24  	"gvisor.dev/gvisor/test/metricsviz"
    25  )
    26  
    27  // BenchmarkFfmpeg runs ffmpeg in a container and records runtime.
    28  // BenchmarkFfmpeg should run as root to drop caches.
    29  func BenchmarkFfmpeg(b *testing.B) {
    30  	machine, err := harness.GetMachine()
    31  	if err != nil {
    32  		b.Fatalf("failed to get machine: %v", err)
    33  	}
    34  	defer machine.CleanUp()
    35  
    36  	ctx := context.Background()
    37  	cmd := strings.Split("ffmpeg -i video.mp4 -c:v libx264 -preset veryslow output.mp4", " ")
    38  
    39  	b.ResetTimer()
    40  	b.StopTimer()
    41  
    42  	for i := 0; i < b.N; i++ {
    43  		func() {
    44  			container := machine.GetContainer(ctx, b)
    45  			defer container.CleanUp(ctx)
    46  			if i == 0 {
    47  				defer metricsviz.FromContainerLogs(ctx, b, container)
    48  			}
    49  			if err := harness.DropCaches(machine); err != nil {
    50  				b.Skipf("failed to drop caches: %v. You probably need root.", err)
    51  			}
    52  
    53  			b.StartTimer()
    54  			if _, err := container.Run(ctx, dockerutil.RunOpts{
    55  				Image: "benchmarks/ffmpeg",
    56  			}, cmd...); err != nil {
    57  				b.Fatalf("failed to run container: %v", err)
    58  			}
    59  			b.StopTimer()
    60  		}()
    61  	}
    62  }
    63  
    64  func TestMain(m *testing.M) {
    65  	harness.Init()
    66  	os.Exit(m.Run())
    67  }