github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/performance/benchmarks/main.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"log"
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    24  )
    25  
    26  const (
    27  	smallSet  = 1000
    28  	mediumSet = 100000
    29  	largeSet  = 10000000
    30  )
    31  
    32  var outputPath = flag.String("outputPath", "./", "the path where the serialized results file will be stored.")
    33  var outputFormat = flag.String("outputFormat", ".csv", "the format used to serialize the benchmarking results.")
    34  var resultsTableName = flag.String("resultsTableName", "results", "the name of the results table.")
    35  var csvFlag = flag.Bool("csv", false, "test importing .csv file into dolt")
    36  var jsonFlag = flag.Bool("json", false, "test importing .json file into dolt")
    37  var sqlFlag = flag.Bool("sql", false, "test importing .sql file into dolt")
    38  
    39  var flagStrs = []flagStr{
    40  	{b: csvFlag, s: csvExt},
    41  	{b: jsonFlag, s: jsonExt},
    42  	{b: sqlFlag, s: sqlExt},
    43  }
    44  
    45  type flagStr struct {
    46  	b *bool
    47  	s string
    48  }
    49  
    50  func main() {
    51  	flag.Parse()
    52  
    53  	results := make([]result, 0)
    54  
    55  	testFmts := make([]string, 0)
    56  	for _, fs := range flagStrs {
    57  		if *fs.b {
    58  			if fs.s == sqlExt {
    59  				log.Fatal("benchmarking dolt sql imports currently disabled")
    60  			}
    61  			testFmts = append(testFmts, fs.s)
    62  		}
    63  	}
    64  
    65  	if len(testFmts) == 0 {
    66  		log.Fatal("must provide flag(s) format for testing dolt imports, ie -csv, -json, -sql \n")
    67  	}
    68  
    69  	for _, frmt := range testFmts {
    70  		benchmarks := []struct {
    71  			Name    string
    72  			Format  string
    73  			Rows    int
    74  			Columns int
    75  			BM      func(b *testing.B)
    76  		}{
    77  			{
    78  				Name:    "dolt_import_small",
    79  				Format:  frmt,
    80  				Rows:    smallSet,
    81  				Columns: len(genSampleCols()),
    82  				BM:      BenchmarkDoltImport(smallSet, genSampleCols(), frmt),
    83  			},
    84  			{
    85  				Name:    "dolt_import_medium",
    86  				Format:  frmt,
    87  				Rows:    mediumSet,
    88  				Columns: len(genSampleCols()),
    89  				BM:      BenchmarkDoltImport(mediumSet, genSampleCols(), frmt),
    90  			},
    91  			{
    92  				Name:    "dolt_import_large",
    93  				Format:  frmt,
    94  				Rows:    largeSet,
    95  				Columns: len(genSampleCols()),
    96  				BM:      BenchmarkDoltImport(largeSet, genSampleCols(), frmt),
    97  			},
    98  		}
    99  
   100  		for _, b := range benchmarks {
   101  			br := testing.Benchmark(b.BM)
   102  			res := result{
   103  				name:    b.Name,
   104  				format:  b.Format,
   105  				rows:    b.Rows,
   106  				columns: b.Columns,
   107  				br:      br,
   108  			}
   109  			results = append(results, res)
   110  		}
   111  	}
   112  
   113  	// benchmark other dolt commands with and just use a single import format
   114  	for _, frmt := range []string{csvExt} {
   115  		benchmarks := []struct {
   116  			Name    string
   117  			Format  string
   118  			Rows    int
   119  			Columns int
   120  			BM      func(b *testing.B)
   121  		}{
   122  			{
   123  				Name:    "dolt_export_small",
   124  				Format:  frmt,
   125  				Rows:    smallSet,
   126  				Columns: len(genSampleCols()),
   127  				BM:      BenchmarkDoltExport(smallSet, genSampleCols(), frmt),
   128  			},
   129  			{
   130  				Name:    "dolt_export_medium",
   131  				Format:  frmt,
   132  				Rows:    mediumSet,
   133  				Columns: len(genSampleCols()),
   134  				BM:      BenchmarkDoltExport(mediumSet, genSampleCols(), frmt),
   135  			},
   136  			{
   137  				Name:    "dolt_export_large",
   138  				Format:  frmt,
   139  				Rows:    largeSet,
   140  				Columns: len(genSampleCols()),
   141  				BM:      BenchmarkDoltExport(largeSet, genSampleCols(), frmt),
   142  			},
   143  			{
   144  				Name:    "dolt_sql_select_small",
   145  				Format:  frmt,
   146  				Rows:    smallSet,
   147  				Columns: len(genSampleCols()),
   148  				BM:      BenchmarkDoltSQLSelect(smallSet, genSampleCols(), frmt),
   149  			},
   150  			{
   151  				Name:    "dolt_sql_select_medium",
   152  				Format:  frmt,
   153  				Rows:    mediumSet,
   154  				Columns: len(genSampleCols()),
   155  				BM:      BenchmarkDoltSQLSelect(mediumSet, genSampleCols(), frmt),
   156  			},
   157  			{
   158  				Name:    "dolt_sql_select_large",
   159  				Format:  frmt,
   160  				Rows:    largeSet,
   161  				Columns: len(genSampleCols()),
   162  				BM:      BenchmarkDoltSQLSelect(largeSet, genSampleCols(), frmt),
   163  			},
   164  		}
   165  
   166  		for _, b := range benchmarks {
   167  			br := testing.Benchmark(b.BM)
   168  			res := result{
   169  				name:    b.Name,
   170  				format:  b.Format,
   171  				rows:    b.Rows,
   172  				columns: b.Columns,
   173  				br:      br,
   174  			}
   175  			results = append(results, res)
   176  		}
   177  	}
   178  
   179  	// write results data
   180  	serializeResults(results, *outputPath, *resultsTableName, *outputFormat)
   181  
   182  	// cleanup temp dolt data dir
   183  	removeTempDoltDataDir(filesys.LocalFS)
   184  
   185  	os.Exit(0)
   186  }