-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
petrjahoda
committed
Jan 7, 2021
0 parents
commit 7bc0058
Showing
10 changed files
with
559 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/driver/sqlserver" | ||
"gorm.io/gorm" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func readBenchmark(databaseType string) { | ||
start := time.Now() | ||
databases := createDatabasesMap() | ||
readData(databaseType, databases) | ||
fmt.Println(databaseType + ": read benchmarks took " + time.Since(start).String()) | ||
} | ||
|
||
func readData(databaseType string, databases map[string]string) { | ||
var database *gorm.DB | ||
var err error | ||
switch databaseType { | ||
case "postgres": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "timescale": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mysql": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mariadb": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "percona": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "sqlserver": | ||
{ | ||
database, err = gorm.Open(sqlserver.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
} | ||
sqlDB, _ := database.DB() | ||
defer sqlDB.Close() | ||
if err != nil { | ||
fmt.Println("Problem opening database: " + err.Error()) | ||
return | ||
} | ||
for i := 0; i < 2000; i++ { | ||
var sum int | ||
_ = database.Table("benchmark_data").Select("sum(data)").Row().Scan(&sum) | ||
var avg int | ||
_ = database.Table("benchmark_data").Select("avg(data)").Row().Scan(&avg) | ||
} | ||
} | ||
|
||
func writeBenchmark(databaseType string) { | ||
start := time.Now() | ||
databases := createDatabasesMap() | ||
createDatabaseAndTable(databaseType, databases) | ||
writeData(databaseType, databases) | ||
fmt.Println(databaseType + ": write benchmarks took " + time.Since(start).String()) | ||
} | ||
|
||
func createDatabasesMap() map[string]string { | ||
databases := make(map[string]string, 6) | ||
databases["postgres"] = "user=postgres password=password dbname=benchmark host=localhost port=5433 sslmode=disable" | ||
databases["mariadb"] = "root:password@tcp(localhost:3307)/benchmark?charset=utf8&parseTime=True&loc=Local" | ||
databases["mysql"] = "root:password@tcp(localhost:3306)/benchmark?charset=utf8&parseTime=True&loc=Local" | ||
databases["timescale"] = "user=postgres password=password dbname=benchmark host=localhost port=5434 sslmode=disable" | ||
databases["percona"] = "root:password@tcp(localhost:3308)/benchmark?charset=utf8&parseTime=True&loc=Local" | ||
databases["sqlserver"] = "sqlserver://sa:passw0rd.@localhost:1433?database=benchmark" | ||
return databases | ||
} | ||
|
||
func writeData(databaseType string, databases map[string]string) { | ||
var database *gorm.DB | ||
var err error | ||
switch databaseType { | ||
case "postgres": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "timescale": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mysql": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mariadb": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "percona": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "sqlserver": | ||
{ | ||
database, err = gorm.Open(sqlserver.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
} | ||
sqlDB, _ := database.DB() | ||
defer sqlDB.Close() | ||
if err != nil { | ||
fmt.Println("Problem opening database: " + err.Error()) | ||
return | ||
} | ||
for i := 0; i < 10000; i++ { | ||
var benchmarkData BenchmarkData | ||
benchmarkData.Time = time.Now() | ||
benchmarkData.Data = rand.Intn(100-0) + 0 | ||
database.Save(&benchmarkData) | ||
} | ||
} | ||
|
||
|
||
|
||
type BenchmarkData struct { | ||
gorm.Model | ||
Data int | ||
Time time.Time | ||
} | ||
|
||
func createDatabaseAndTable(databaseType string, databases map[string]string) { | ||
var database *gorm.DB | ||
var err error | ||
switch databaseType { | ||
case "postgres": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "timescale": | ||
{ | ||
database, err = gorm.Open(postgres.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mysql": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "mariadb": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "percona": | ||
{ | ||
database, err = gorm.Open(mysql.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
case "sqlserver": | ||
{ | ||
database, err = gorm.Open(sqlserver.Open(databases[databaseType]), &gorm.Config{}) | ||
} | ||
} | ||
sqlDB, _ := database.DB() | ||
defer sqlDB.Close() | ||
if err != nil { | ||
fmt.Println("Problem opening database: "+err.Error()) | ||
return | ||
} | ||
fmt.Println(databaseType + " connected") | ||
|
||
if !database.Migrator().HasTable(&BenchmarkData{}) { | ||
err := database.Migrator().CreateTable(&BenchmarkData{}) | ||
if err != nil { | ||
fmt.Println("Cannot create table: " + err.Error()) | ||
return | ||
} | ||
} else { | ||
err := database.Migrator().AutoMigrate(&BenchmarkData{}) | ||
if err != nil { | ||
fmt.Println("Cannot update table: " + err.Error()) | ||
return | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module medium_database_benchmark | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/kardianos/service v1.2.0 | ||
gorm.io/driver/mysql v1.0.3 | ||
gorm.io/driver/postgres v1.0.6 | ||
gorm.io/driver/sqlserver v1.0.5 | ||
gorm.io/gorm v1.20.9 | ||
) |
Oops, something went wrong.