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

     1  // Copyright 2018 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 sql
    12  
    13  import (
    14  	"context"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/base"
    18  	"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
    19  )
    20  
    21  func BenchmarkSequenceIncrement(b *testing.B) {
    22  	cluster := serverutils.StartTestCluster(b, 3, base.TestClusterArgs{})
    23  	defer cluster.Stopper().Stop(context.Background())
    24  
    25  	sqlDB := cluster.ServerConn(0)
    26  
    27  	if _, err := sqlDB.Exec(`
    28  		CREATE DATABASE test;
    29  		USE test;
    30  		CREATE SEQUENCE seq;
    31  		CREATE TABLE tbl (
    32  			id INT PRIMARY KEY DEFAULT nextval('seq'),
    33  			foo text
    34  		);
    35  	`); err != nil {
    36  		b.Fatal(err)
    37  	}
    38  
    39  	b.ResetTimer()
    40  	for n := 0; n < b.N; n++ {
    41  		if _, err := sqlDB.Exec("INSERT INTO tbl (foo) VALUES ('foo')"); err != nil {
    42  			b.Fatal(err)
    43  		}
    44  	}
    45  	b.StopTimer()
    46  }
    47  
    48  func BenchmarkUniqueRowID(b *testing.B) {
    49  	cluster := serverutils.StartTestCluster(b, 3, base.TestClusterArgs{})
    50  	defer cluster.Stopper().Stop(context.Background())
    51  
    52  	sqlDB := cluster.ServerConn(0)
    53  
    54  	if _, err := sqlDB.Exec(`
    55  		CREATE DATABASE test;
    56  		USE test;
    57  		CREATE TABLE tbl (
    58  			id INT PRIMARY KEY DEFAULT unique_rowid(),
    59  			foo text
    60  		);
    61  	`); err != nil {
    62  		b.Fatal(err)
    63  	}
    64  
    65  	b.ResetTimer()
    66  	for n := 0; n < b.N; n++ {
    67  		if _, err := sqlDB.Exec("INSERT INTO tbl (foo) VALUES ('foo')"); err != nil {
    68  			b.Fatal(err)
    69  		}
    70  	}
    71  	b.StopTimer()
    72  }