Last active
October 14, 2020 07:42
-
-
Save draveness/8e714b61a1d20c836fd9ac8fa6553cdd to your computer and use it in GitHub Desktop.
Benchmark Foreign Key
This file contains hidden or 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