gopkg.in/dotcloud/docker.v1@v1.13.1/daemon/graphdriver/graphtest/graphbench_unix.go (about)

     1  // +build linux freebsd
     2  
     3  package graphtest
     4  
     5  import (
     6  	"bytes"
     7  	"io"
     8  	"io/ioutil"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/pkg/stringid"
    13  )
    14  
    15  // DriverBenchExists benchmarks calls to exist
    16  func DriverBenchExists(b *testing.B, drivername string, driveroptions ...string) {
    17  	driver := GetDriver(b, drivername, driveroptions...)
    18  	defer PutDriver(b)
    19  
    20  	base := stringid.GenerateRandomID()
    21  
    22  	if err := driver.Create(base, "", nil); err != nil {
    23  		b.Fatal(err)
    24  	}
    25  
    26  	b.ResetTimer()
    27  	for i := 0; i < b.N; i++ {
    28  		if !driver.Exists(base) {
    29  			b.Fatal("Newly created image doesn't exist")
    30  		}
    31  	}
    32  }
    33  
    34  // DriverBenchGetEmpty benchmarks calls to get on an empty layer
    35  func DriverBenchGetEmpty(b *testing.B, drivername string, driveroptions ...string) {
    36  	driver := GetDriver(b, drivername, driveroptions...)
    37  	defer PutDriver(b)
    38  
    39  	base := stringid.GenerateRandomID()
    40  
    41  	if err := driver.Create(base, "", nil); err != nil {
    42  		b.Fatal(err)
    43  	}
    44  
    45  	b.ResetTimer()
    46  	for i := 0; i < b.N; i++ {
    47  		_, err := driver.Get(base, "")
    48  		b.StopTimer()
    49  		if err != nil {
    50  			b.Fatalf("Error getting mount: %s", err)
    51  		}
    52  		if err := driver.Put(base); err != nil {
    53  			b.Fatalf("Error putting mount: %s", err)
    54  		}
    55  		b.StartTimer()
    56  	}
    57  }
    58  
    59  // DriverBenchDiffBase benchmarks calls to diff on a root layer
    60  func DriverBenchDiffBase(b *testing.B, drivername string, driveroptions ...string) {
    61  	driver := GetDriver(b, drivername, driveroptions...)
    62  	defer PutDriver(b)
    63  
    64  	base := stringid.GenerateRandomID()
    65  	if err := driver.Create(base, "", nil); err != nil {
    66  		b.Fatal(err)
    67  	}
    68  
    69  	if err := addFiles(driver, base, 3); err != nil {
    70  		b.Fatal(err)
    71  	}
    72  
    73  	b.ResetTimer()
    74  	for i := 0; i < b.N; i++ {
    75  		arch, err := driver.Diff(base, "")
    76  		if err != nil {
    77  			b.Fatal(err)
    78  		}
    79  		_, err = io.Copy(ioutil.Discard, arch)
    80  		if err != nil {
    81  			b.Fatalf("Error copying archive: %s", err)
    82  		}
    83  		arch.Close()
    84  	}
    85  }
    86  
    87  // DriverBenchDiffN benchmarks calls to diff on two layers with
    88  // a provided number of files on the lower and upper layers.
    89  func DriverBenchDiffN(b *testing.B, bottom, top int, drivername string, driveroptions ...string) {
    90  	driver := GetDriver(b, drivername, driveroptions...)
    91  	defer PutDriver(b)
    92  	base := stringid.GenerateRandomID()
    93  	upper := stringid.GenerateRandomID()
    94  	if err := driver.Create(base, "", nil); err != nil {
    95  		b.Fatal(err)
    96  	}
    97  
    98  	if err := addManyFiles(driver, base, bottom, 3); err != nil {
    99  		b.Fatal(err)
   100  	}
   101  
   102  	if err := driver.Create(upper, base, nil); err != nil {
   103  		b.Fatal(err)
   104  	}
   105  
   106  	if err := addManyFiles(driver, upper, top, 6); err != nil {
   107  		b.Fatal(err)
   108  	}
   109  	b.ResetTimer()
   110  	for i := 0; i < b.N; i++ {
   111  		arch, err := driver.Diff(upper, "")
   112  		if err != nil {
   113  			b.Fatal(err)
   114  		}
   115  		_, err = io.Copy(ioutil.Discard, arch)
   116  		if err != nil {
   117  			b.Fatalf("Error copying archive: %s", err)
   118  		}
   119  		arch.Close()
   120  	}
   121  }
   122  
   123  // DriverBenchDiffApplyN benchmarks calls to diff and apply together
   124  func DriverBenchDiffApplyN(b *testing.B, fileCount int, drivername string, driveroptions ...string) {
   125  	driver := GetDriver(b, drivername, driveroptions...)
   126  	defer PutDriver(b)
   127  	base := stringid.GenerateRandomID()
   128  	upper := stringid.GenerateRandomID()
   129  	if err := driver.Create(base, "", nil); err != nil {
   130  		b.Fatal(err)
   131  	}
   132  
   133  	if err := addManyFiles(driver, base, fileCount, 3); err != nil {
   134  		b.Fatal(err)
   135  	}
   136  
   137  	if err := driver.Create(upper, base, nil); err != nil {
   138  		b.Fatal(err)
   139  	}
   140  
   141  	if err := addManyFiles(driver, upper, fileCount, 6); err != nil {
   142  		b.Fatal(err)
   143  	}
   144  	diffSize, err := driver.DiffSize(upper, "")
   145  	if err != nil {
   146  		b.Fatal(err)
   147  	}
   148  	b.ResetTimer()
   149  	b.StopTimer()
   150  	for i := 0; i < b.N; i++ {
   151  		diff := stringid.GenerateRandomID()
   152  		if err := driver.Create(diff, base, nil); err != nil {
   153  			b.Fatal(err)
   154  		}
   155  
   156  		if err := checkManyFiles(driver, diff, fileCount, 3); err != nil {
   157  			b.Fatal(err)
   158  		}
   159  
   160  		b.StartTimer()
   161  
   162  		arch, err := driver.Diff(upper, "")
   163  		if err != nil {
   164  			b.Fatal(err)
   165  		}
   166  
   167  		applyDiffSize, err := driver.ApplyDiff(diff, "", arch)
   168  		if err != nil {
   169  			b.Fatal(err)
   170  		}
   171  
   172  		b.StopTimer()
   173  		arch.Close()
   174  
   175  		if applyDiffSize != diffSize {
   176  			// TODO: enforce this
   177  			//b.Fatalf("Apply diff size different, got %d, expected %s", applyDiffSize, diffSize)
   178  		}
   179  		if err := checkManyFiles(driver, diff, fileCount, 6); err != nil {
   180  			b.Fatal(err)
   181  		}
   182  	}
   183  }
   184  
   185  // DriverBenchDeepLayerDiff benchmarks calls to diff on top of a given number of layers.
   186  func DriverBenchDeepLayerDiff(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
   187  	driver := GetDriver(b, drivername, driveroptions...)
   188  	defer PutDriver(b)
   189  
   190  	base := stringid.GenerateRandomID()
   191  	if err := driver.Create(base, "", nil); err != nil {
   192  		b.Fatal(err)
   193  	}
   194  
   195  	if err := addFiles(driver, base, 50); err != nil {
   196  		b.Fatal(err)
   197  	}
   198  
   199  	topLayer, err := addManyLayers(driver, base, layerCount)
   200  	if err != nil {
   201  		b.Fatal(err)
   202  	}
   203  
   204  	b.ResetTimer()
   205  	for i := 0; i < b.N; i++ {
   206  		arch, err := driver.Diff(topLayer, "")
   207  		if err != nil {
   208  			b.Fatal(err)
   209  		}
   210  		_, err = io.Copy(ioutil.Discard, arch)
   211  		if err != nil {
   212  			b.Fatalf("Error copying archive: %s", err)
   213  		}
   214  		arch.Close()
   215  	}
   216  }
   217  
   218  // DriverBenchDeepLayerRead benchmarks calls to read a file under a given number of layers.
   219  func DriverBenchDeepLayerRead(b *testing.B, layerCount int, drivername string, driveroptions ...string) {
   220  	driver := GetDriver(b, drivername, driveroptions...)
   221  	defer PutDriver(b)
   222  
   223  	base := stringid.GenerateRandomID()
   224  	if err := driver.Create(base, "", nil); err != nil {
   225  		b.Fatal(err)
   226  	}
   227  
   228  	content := []byte("test content")
   229  	if err := addFile(driver, base, "testfile.txt", content); err != nil {
   230  		b.Fatal(err)
   231  	}
   232  
   233  	topLayer, err := addManyLayers(driver, base, layerCount)
   234  	if err != nil {
   235  		b.Fatal(err)
   236  	}
   237  
   238  	root, err := driver.Get(topLayer, "")
   239  	if err != nil {
   240  		b.Fatal(err)
   241  	}
   242  	defer driver.Put(topLayer)
   243  
   244  	b.ResetTimer()
   245  	for i := 0; i < b.N; i++ {
   246  
   247  		// Read content
   248  		c, err := ioutil.ReadFile(filepath.Join(root, "testfile.txt"))
   249  		if err != nil {
   250  			b.Fatal(err)
   251  		}
   252  
   253  		b.StopTimer()
   254  		if bytes.Compare(c, content) != 0 {
   255  			b.Fatalf("Wrong content in file %v, expected %v", c, content)
   256  		}
   257  		b.StartTimer()
   258  	}
   259  }