Last active
June 9, 2019 10:09
-
-
Save shal/8909905194cba0dff9d295fb8701d91c to your computer and use it in GitHub Desktop.
Peatio SQL Benchmarks
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 | |
// Author: Ali Shanaakh <hi@shal.dev> | |
// Peatio SQL queries benchmark. | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"time" | |
_ "github.com/go-sql-driver/mysql" | |
"github.com/jmoiron/sqlx" | |
) | |
var ( | |
NumOfTransactions = 10000 | |
) | |
const ( | |
Query = `SELECT orders.* FROM orders WHERE id < 100` | |
) | |
func connect() (*sqlx.DB, error) { | |
host := os.Getenv("HOST") | |
user := os.Getenv("USER") | |
password := os.Getenv("PASSWORD") | |
database := os.Getenv("DATABASE") | |
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", user, password, host, database) | |
db, err := sqlx.Open("mysql", dataSourceName) | |
if err != nil { | |
panic(err) | |
} | |
db.SetMaxOpenConns(50) | |
db.SetMaxIdleConns(20) | |
return db, nil | |
} | |
func main() { | |
start := time.Now() | |
db, err := connect() | |
if err != nil { | |
panic(err) | |
} | |
defer func() { | |
if err := db.Close(); err != nil { | |
panic(err) | |
} | |
}() | |
for i := 0; i < NumOfTransactions; i++ { | |
db.MustExec(Query) | |
} | |
fmt.Printf("SQL: %.3f tx/sec", float64(NumOfTransactions) / time.Since(start).Seconds()) | |
} |
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
#!/usr/bin/env ruby | |
# Author: Ali Shanaakh hi@shal.dev | |
# Active Record vs Pure MySQL2. | |
require 'mysql2' | |
NUM_OF_TRANSACTIONS = ARGV[0] || 10_000 | |
module Bench | |
class ActiveRecord | |
attr_accessor :execution_time | |
def benchmark! | |
@start = Time.now | |
NUM_OF_TRANSACTIONS.times do |_| | |
::Order.where("id > 20 ", 10) | |
end | |
@finish = Time.now | |
@execution_time = @finish - @start | |
end | |
end | |
class Mysql2 | |
attr_accessor :execution_time | |
def initialize | |
@client = ::Mysql2::Client.new( | |
:host => ENV.fetch('DATABASE_HOST', '127.0.0.1'), | |
:username => ENV.fetch('DATABASE_USER', 'root'), | |
:port => 3306, | |
:password => ENV['DATABASE_PASS'], | |
:database => ENV.fetch('DATABASE_NAME', 'peatio_development') | |
) | |
end | |
def benchmark! | |
@start = Time.now | |
NUM_OF_TRANSACTIONS.times do |_| | |
@client.query("SELECT * FROM orders WHERE id > #{10}") | |
end | |
@finish = Time.now | |
@execution_time = @finish - @start | |
end | |
end | |
end | |
ar_bench = Bench::ActiveRecord.new | |
my_bench = Bench::Mysql2.new | |
ar_bench.benchmark! | |
my_bench.benchmark! | |
puts ar_bench.execution_time | |
puts my_bench.execution_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment