github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/bench/pgbench_test.go (about)

     1  // Copyright 2016 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package bench
    12  
    13  import (
    14  	"context"
    15  	"fmt"
    16  	"math/rand"
    17  	"net"
    18  	"net/url"
    19  	"os/exec"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/cockroachdb/cockroach/pkg/base"
    24  	"github.com/cockroachdb/cockroach/pkg/security"
    25  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    26  	"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
    27  	"github.com/cockroachdb/cockroach/pkg/util/log"
    28  	"github.com/cockroachdb/cockroach/pkg/util/retry"
    29  )
    30  
    31  // Tests a batch of queries very similar to those that that PGBench runs
    32  // in its TPC-B(ish) mode.
    33  func BenchmarkPgbenchQuery(b *testing.B) {
    34  	defer log.Scope(b).Close(b)
    35  	ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
    36  		if err := SetupBenchDB(db.DB, 20000, true /*quiet*/); err != nil {
    37  			b.Fatal(err)
    38  		}
    39  		src := rand.New(rand.NewSource(5432))
    40  		b.ResetTimer()
    41  		for i := 0; i < b.N; i++ {
    42  			if err := RunOne(db.DB, src, 20000); err != nil {
    43  				b.Fatal(err)
    44  			}
    45  		}
    46  		b.StopTimer()
    47  	})
    48  }
    49  
    50  // Tests a batch of queries very similar to those that that PGBench runs
    51  // in its TPC-B(ish) mode.
    52  func BenchmarkPgbenchQueryParallel(b *testing.B) {
    53  	defer log.Scope(b).Close(b)
    54  	ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
    55  		if err := SetupBenchDB(db.DB, 20000, true /*quiet*/); err != nil {
    56  			b.Fatal(err)
    57  		}
    58  
    59  		retryOpts := retry.Options{
    60  			InitialBackoff: 1 * time.Millisecond,
    61  			MaxBackoff:     200 * time.Millisecond,
    62  			Multiplier:     2,
    63  		}
    64  
    65  		b.ResetTimer()
    66  		b.RunParallel(func(pb *testing.PB) {
    67  			src := rand.New(rand.NewSource(5432))
    68  			r := retry.Start(retryOpts)
    69  			var err error
    70  			for pb.Next() {
    71  				r.Reset()
    72  				for r.Next() {
    73  					err = RunOne(db.DB, src, 20000)
    74  					if err == nil {
    75  						break
    76  					}
    77  				}
    78  				if err != nil {
    79  					b.Fatal(err)
    80  				}
    81  			}
    82  		})
    83  		b.StopTimer()
    84  	})
    85  }
    86  
    87  func execPgbench(b *testing.B, pgURL url.URL) {
    88  	if _, err := exec.LookPath("pgbench"); err != nil {
    89  		b.Skip("pgbench is not available on PATH")
    90  	}
    91  	c, err := SetupExec(pgURL, "bench", 20000, b.N)
    92  	if err != nil {
    93  		b.Fatal(err)
    94  	}
    95  
    96  	b.ResetTimer()
    97  	out, err := c.CombinedOutput()
    98  	if testing.Verbose() || err != nil {
    99  		fmt.Println(string(out))
   100  	}
   101  	if err != nil {
   102  		b.Log(c)
   103  		b.Fatal(err)
   104  	}
   105  	b.StopTimer()
   106  }
   107  
   108  func BenchmarkPgbenchExec(b *testing.B) {
   109  	defer log.Scope(b).Close(b)
   110  	b.Run("Cockroach", func(b *testing.B) {
   111  		s, _, _ := serverutils.StartServer(b, base.TestServerArgs{Insecure: true})
   112  		defer s.Stopper().Stop(context.Background())
   113  
   114  		pgURL, cleanupFn := sqlutils.PGUrl(
   115  			b, s.ServingSQLAddr(), "benchmarkCockroach", url.User(security.RootUser))
   116  		pgURL.RawQuery = "sslmode=disable"
   117  		defer cleanupFn()
   118  
   119  		execPgbench(b, pgURL)
   120  	})
   121  
   122  	b.Run("Postgres", func(b *testing.B) {
   123  		pgURL := url.URL{
   124  			Scheme:   "postgres",
   125  			Host:     "localhost:5432",
   126  			RawQuery: "sslmode=disable&dbname=postgres",
   127  		}
   128  		if conn, err := net.Dial("tcp", pgURL.Host); err != nil {
   129  			b.Skipf("unable to connect to postgres server on %s: %s", pgURL.Host, err)
   130  		} else {
   131  			conn.Close()
   132  		}
   133  		execPgbench(b, pgURL)
   134  	})
   135  }