Skip to content

Instantly share code, notes, and snippets.

@yutakahashi114
Created March 26, 2021 11:41
Show Gist options
  • Save yutakahashi114/8d6a53eb285e0c639554e6808187fb27 to your computer and use it in GitHub Desktop.
Save yutakahashi114/8d6a53eb285e0c639554e6808187fb27 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"testing"
"xorm.io/core"
"xorm.io/xorm"
)
func base(b *testing.B, insertCount int, fn func(xorm.Interface, users) error) {
us := make(users, 0, insertCount)
for i := 0; i < insertCount; i++ {
us = append(us, user{
FirstName: fmt.Sprintf("太郎+%v", i),
LastName: fmt.Sprintf("テスト+%v", i),
FirstNameKana: fmt.Sprintf("タロウ+%v", i),
LastNameKana: fmt.Sprintf("テスト+%v", i),
})
}
engine, err := xorm.NewEngine("postgres", "postgres://user:password@db/database?sslmode=disable")
if err != nil {
panic(err)
}
defer engine.Close()
engine.SetMapper(core.GonicMapper{})
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StartTimer()
_, err := engine.Transaction(func(tx *xorm.Session) (interface{}, error) {
err := fn(tx, us)
return nil, err
})
if err != nil {
panic(err)
}
b.StopTimer()
engine.Unscoped().Where("id > ?", 0).Delete(user{})
}
}
func Benchmark_insert_1(b *testing.B) {
base(b, 1, insert)
}
func Benchmark_insert_10(b *testing.B) {
base(b, 10, insert)
}
func Benchmark_insert_100(b *testing.B) {
base(b, 100, insert)
}
func Benchmark_insert_1000(b *testing.B) {
base(b, 1000, insert)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment