Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjahoda committed Jan 7, 2021
0 parents commit 7bc0058
Show file tree
Hide file tree
Showing 10 changed files with 559 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/medium_database_benchmark.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added README.md
Empty file.
183 changes: 183 additions & 0 deletions database.go
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
}
}
}
11 changes: 11 additions & 0 deletions go.mod
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
)
Loading

0 comments on commit 7bc0058

Please sign in to comment.