github.com/reggieriser/pop@v4.13.1+incompatible/benchmarks_test.go (about)

     1  package pop
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/gobuffalo/nulls"
     9  )
    10  
    11  func Benchmark_Create_Pop(b *testing.B) {
    12  	transaction(func(tx *Connection) {
    13  		for n := 0; n < b.N; n++ {
    14  			u := &User{
    15  				Name: nulls.NewString("Mark Bates"),
    16  			}
    17  			tx.Create(u)
    18  		}
    19  	})
    20  }
    21  
    22  func Benchmark_Create_Raw(b *testing.B) {
    23  	transaction(func(tx *Connection) {
    24  		for n := 0; n < b.N; n++ {
    25  			u := &User{
    26  				Name: nulls.NewString("Mark Bates"),
    27  			}
    28  			q := "INSERT INTO users (alive, bio, birth_date, created_at, name, price, updated_at) VALUES (:alive, :bio, :birth_date, :created_at, :name, :price, :updated_at)"
    29  			tx.Store.NamedExec(q, u)
    30  		}
    31  	})
    32  }
    33  
    34  func Benchmark_Update(b *testing.B) {
    35  	transaction(func(tx *Connection) {
    36  		u := &User{
    37  			Name: nulls.NewString("Mark Bates"),
    38  		}
    39  		tx.Create(u)
    40  		for n := 0; n < b.N; n++ {
    41  			tx.Update(u)
    42  		}
    43  	})
    44  }
    45  
    46  func Benchmark_Find_Pop(b *testing.B) {
    47  	transaction(func(tx *Connection) {
    48  		u := &User{
    49  			Name: nulls.NewString("Mark Bates"),
    50  		}
    51  		tx.Create(u)
    52  		for n := 0; n < b.N; n++ {
    53  			tx.Find(u, u.ID)
    54  		}
    55  	})
    56  }
    57  
    58  func Benchmark_Find_Raw(b *testing.B) {
    59  	transaction(func(tx *Connection) {
    60  		u := &User{
    61  			Name: nulls.NewString("Mark Bates"),
    62  		}
    63  		tx.Create(u)
    64  		for n := 0; n < b.N; n++ {
    65  			tx.Store.Get(u, "select * from users where id = ?", u.ID)
    66  		}
    67  	})
    68  }
    69  
    70  func Benchmark_translateOne(b *testing.B) {
    71  	q := "select * from users where id = ? and name = ? and email = ? and a = ? and b = ? and c = ? and d = ? and e = ? and f = ?"
    72  	for n := 0; n < b.N; n++ {
    73  		translateOne(q)
    74  	}
    75  }
    76  
    77  func Benchmark_translateTwo(b *testing.B) {
    78  	q := "select * from users where id = ? and name = ? and email = ? and a = ? and b = ? and c = ? and d = ? and e = ? and f = ?"
    79  	for n := 0; n < b.N; n++ {
    80  		translateTwo(q)
    81  	}
    82  }
    83  
    84  func translateOne(sql string) string {
    85  	curr := 1
    86  	out := make([]byte, 0, len(sql))
    87  	for i := 0; i < len(sql); i++ {
    88  		if sql[i] == '?' {
    89  			str := "$" + strconv.Itoa(curr)
    90  			for _, char := range str {
    91  				out = append(out, byte(char))
    92  			}
    93  			curr++
    94  		} else {
    95  			out = append(out, sql[i])
    96  		}
    97  	}
    98  	return string(out)
    99  }
   100  
   101  func translateTwo(sql string) string {
   102  	curr := 1
   103  	csql := ""
   104  	for i := 0; i < len(sql); i++ {
   105  		x := sql[i]
   106  		if x == '?' {
   107  			csql = fmt.Sprintf("%s$%d", csql, curr)
   108  			curr++
   109  		} else {
   110  			csql += string(x)
   111  		}
   112  	}
   113  	return csql
   114  }