Benchmark Foreign Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/mysql" | |
) | |
type Post struct { | |
ID int64 | |
AuthorID int64 | |
} | |
type Author struct { | |
ID int64 | |
} | |
type ForeignKeyPost struct { | |
ID int64 | |
AuthorID int64 | |
} | |
var conn *gorm.DB | |
func init() { | |
var err error | |
conn, err = gorm.Open("mysql", "root:@tcp(localhost:3306)/fk_dev?parseTime=true") | |
if err != nil { | |
panic(err) | |
} | |
conn.AutoMigrate(&Post{}). | |
AutoMigrate(&Author{}). | |
AutoMigrate(&ForeignKeyPost{}) | |
conn.Model(&ForeignKeyPost{}).AddForeignKey("author_id", "authors(id)", "RESTRICT", "RESTRICT") | |
conn.Create(&Author{ID: 1}) | |
} | |
func BenchmarkBaseline(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
if err := conn.Create(&Post{AuthorID: 1}).Error; err != nil { | |
panic(err) | |
} | |
} | |
} | |
func BenchmarkForeignKey(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
if err := conn.Create(&ForeignKeyPost{AuthorID: 1}).Error; err != nil { | |
panic(err) | |
} | |
} | |
} |
Author
draveness
commented
Jun 2, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment