github.com/cockroachdb/pebble@v1.1.2/internal/mkbench/write_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  const testdataSummaryDir = "./testdata/write-throughput/"
    13  
    14  var (
    15  	testdataSummaryFile            = filepath.Join(testdataSummaryDir, summaryFilename)
    16  	testdataPerRunSummaryFilenames = []string{
    17  		"20211027-pebble-write-size=1024-run_1-summary.json",
    18  		"20211028-pebble-write-size=1024-run_1-summary.json",
    19  	}
    20  )
    21  
    22  func TestParseWrite_FromScratch(t *testing.T) {
    23  	maybeSkip(t)
    24  
    25  	testFn := func(t *testing.T, dataDir string) {
    26  		// Set up the output directory for the test.
    27  		testDir := t.TempDir()
    28  		summaryDir := filepath.Join(testDir, "write-throughput")
    29  		err := os.Mkdir(summaryDir, 0700)
    30  		require.NoError(t, err)
    31  
    32  		// Write out the new summary file.
    33  		err = parseWrite(dataDir, summaryDir)
    34  		require.NoError(t, err)
    35  
    36  		// Confirm new summary.json file matches what we expect.
    37  		err = filesEqual(testdataSummaryFile, filepath.Join(summaryDir, summaryFilename))
    38  		require.NoError(t, err)
    39  
    40  		// The individual per-run *summary.json files are equal.
    41  		for _, p := range testdataPerRunSummaryFilenames {
    42  			err = filesEqual(filepath.Join(testdataSummaryDir, p), filepath.Join(summaryDir, p))
    43  			require.NoError(t, err)
    44  		}
    45  	}
    46  
    47  	for _, dir := range dataDirPaths {
    48  		t.Run(dir, func(t *testing.T) {
    49  			testFn(t, dir)
    50  		})
    51  	}
    52  }
    53  
    54  func TestParseWrite_Existing(t *testing.T) {
    55  	maybeSkip(t)
    56  
    57  	testFn := func(t *testing.T, dataDir string) {
    58  		// Set up the output directory for the test.
    59  		testDir := t.TempDir()
    60  		summaryDir := filepath.Join(testDir, "write-throughput")
    61  		err := os.Mkdir(summaryDir, 0700)
    62  		require.NoError(t, err)
    63  
    64  		// Copy all files into the test dir excluding one day.
    65  		newDataDir := filepath.Join(testDir, "data")
    66  		err = copyDir(dataDir, newDataDir)
    67  		require.NoError(t, err)
    68  		err = os.RemoveAll(filepath.Join(newDataDir, "20211027"))
    69  		require.NoError(t, err)
    70  
    71  		// Write out the new summary file.
    72  		err = parseWrite(newDataDir, summaryDir)
    73  		require.NoError(t, err)
    74  
    75  		// Confirm new summary.json files are NOT equal.
    76  		err = filesEqual(testdataSummaryFile, filepath.Join(summaryDir, summaryFilename))
    77  		require.Error(t, err)
    78  
    79  		// The only per-run *summary.json files are for the days we did not remove
    80  		// (i.e. 20211028/**-summary.json)
    81  		var perRunFiles []string
    82  		err = filepath.Walk(summaryDir, func(path string, info os.FileInfo, err error) error {
    83  			basename := filepath.Base(path)
    84  			if strings.HasSuffix(basename, "-summary.json") {
    85  				perRunFiles = append(perRunFiles, basename)
    86  			}
    87  			return nil
    88  		})
    89  		require.NoError(t, err)
    90  		require.Equal(t, perRunFiles, testdataPerRunSummaryFilenames[1:])
    91  
    92  		// Re-construct the summary.json file with the full set of data (i.e. the
    93  		// original data directory).
    94  		err = parseWrite(dataDir, summaryDir)
    95  		require.NoError(t, err)
    96  
    97  		// Confirm the two summary.json files are now equal.
    98  		err = filesEqual(testdataSummaryFile, filepath.Join(summaryDir, summaryFilename))
    99  		require.NoError(t, err)
   100  
   101  		// The individual per-run *summary.json files are equal.
   102  		for _, p := range testdataPerRunSummaryFilenames {
   103  			err = filesEqual(filepath.Join(testdataSummaryDir, p), filepath.Join(summaryDir, p))
   104  			require.NoError(t, err)
   105  		}
   106  	}
   107  
   108  	for _, dir := range dataDirPaths {
   109  		t.Run(dir, func(t *testing.T) {
   110  			testFn(t, dir)
   111  		})
   112  	}
   113  }