Skip to content

performances vs PgSQL #23061

Closed
Closed
@Fale

Description

@Fale

I've written the following code snippet:

package main

import (
	"database/sql"
	"fmt"
	"log"
	"time"

	_ "github.com/lib/pq"
)

const (
	iterations = 500
)

type conn struct {
	db   *sql.DB
	name string
}

func main() {
	p := connection("host=127.0.0.1 port=5432 user=fale password=fale sslmode=disable", "PgSQL")
	defer p.db.Close()
	c := connection("host=127.0.0.1 port=26257 user=root sslmode=disable", "CockroachDB")
	defer c.db.Close()

	// Schemas and DB
	p.schema()
	c.database()
	// Table
	p.table()
	c.table()
	// Insert
	p.inserts()
	c.inserts()
	// Select
	p.selects()
	c.selects()
	// Deletes
	p.deletes()
	c.deletes()
}

func connection(dbinfo string, name string) conn {
	var err error
	c := conn{name: name}
	c.db, err = sql.Open("postgres", dbinfo)
	if err != nil {
		log.Fatalln(err)
	}
	return c
}

func (c *conn) schema() {
	log.Printf("# Ensure schema is present - %s", c.name)
	_, err := c.db.Exec("CREATE SCHEMA IF NOT EXISTS test")
	if err != nil {
		log.Fatalln(err)
	}
}

func (c *conn) database() {
	log.Printf("# Ensure database is present - %s", c.name)
	_, err := c.db.Exec("CREATE DATABASE IF NOT EXISTS test")
	if err != nil {
		log.Fatalln(err)
	}
}

func (c *conn) table() {
	log.Printf("# Ensure table is present and empty - %s", c.name)
	_, err := c.db.Exec("CREATE TABLE IF NOT EXISTS test.accounts (id INT PRIMARY KEY, balance DECIMAL);")
	if err != nil {
		log.Fatalln(err)
	}
	_, err = c.db.Exec("TRUNCATE test.accounts;")
	if err != nil {
		log.Fatalln(err)
	}
}

func (c *conn) inserts() {
	exec(c, "inserts", "INSERT INTO test.accounts VALUES(%[1]d,%[1]d);")
}

func (c *conn) selects() {
	exec(c, "selects", "SELECT * FROM test.accounts WHERE id = %d;")
}

func (c *conn) deletes() {
	exec(c, "deletes", "DELETE FROM test.accounts WHERE id = %d;")
}

func exec(c *conn, name string, query string) {
	start := time.Now()

	var i int64
	for i = 0; i < iterations; i++ {
		stmt := fmt.Sprintf(query, i)
		_, err := c.db.Exec(stmt)
		if err != nil {
			panic(err)
		}
	}
	elapsed := time.Since(start)
	log.Printf("%d %s in %s took %s, with an average of %dns/op", i, name, c.name, elapsed, int64(elapsed/time.Nanosecond)/i)
}

I installed PgSQL from my distro (Fedora 27).

$ psql --version
psql (PostgreSQL) 9.6.6

I installed cockroach following https://www.cockroachlabs.com/docs/stable/start-a-local-cluster.html (v1.1.5) and I tried to perform some benchmarks.

2018/02/25 09:25:40 # Ensure schema is present - PgSQL
2018/02/25 09:25:40 # Ensure database is present - CockroachDB
2018/02/25 09:25:40 # Ensure table is present and empty - PgSQL
2018/02/25 09:25:40 # Ensure table is present and empty - CockroachDB
2018/02/25 09:25:42 1000 inserts in PgSQL took 1.287776052s, with an average of 1287776ns/op
2018/02/25 09:25:48 1000 inserts in CockroachDB took 5.860344296s, with an average of 5860344ns/op
2018/02/25 09:25:48 1000 selects in PgSQL took 81.824155ms, with an average of 81824ns/op
2018/02/25 09:25:49 1000 selects in CockroachDB took 933.020106ms, with an average of 933020ns/op
2018/02/25 09:25:50 1000 deletes in PgSQL took 1.325356857s, with an average of 1325356ns/op
2018/02/25 09:25:56 1000 deletes in CockroachDB took 5.810851181s, with an average of 5810851ns/op
fale@x250:~/go/src/github.com/fale/pg-benchmark $ go build && ./pg-benchmark 
2018/02/25 09:30:08 # Ensure schema is present - PgSQL
2018/02/25 09:30:08 # Ensure database is present - CockroachDB
2018/02/25 09:30:08 # Ensure table is present and empty - PgSQL
2018/02/25 09:30:08 # Ensure table is present and empty - CockroachDB
2018/02/25 09:30:09 1000 inserts in PgSQL took 1.31913866s, with an average of 1319138ns/op
2018/02/25 09:30:15 1000 inserts in CockroachDB took 5.720786054s, with an average of 5720786ns/op
2018/02/25 09:30:15 1000 selects in PgSQL took 78.829973ms, with an average of 78829ns/op
2018/02/25 09:30:16 1000 selects in CockroachDB took 878.112876ms, with an average of 878112ns/op
2018/02/25 09:30:18 1000 delete in PgSQL took 2.113067103s, with an average of 2113067ns/op
2018/02/25 09:30:23 1000 delete in CockroachDB took 5.10793473s, with an average of 5107934ns/op
fale@x250:~/go/src/github.com/fale/pg-benchmark $ go build && ./pg-benchmark 
2018/02/25 09:36:20 # Ensure schema is present - PgSQL
2018/02/25 09:36:20 # Ensure database is present - CockroachDB
2018/02/25 09:36:20 # Ensure table is present and empty - PgSQL
2018/02/25 09:36:20 # Ensure table is present and empty - CockroachDB
2018/02/25 09:36:21 500 inserts in PgSQL took 645.701328ms, with an average of 1291402ns/op
2018/02/25 09:36:24 500 inserts in CockroachDB took 3.277119748s, with an average of 6554239ns/op
2018/02/25 09:36:25 500 selects in PgSQL took 42.114179ms, with an average of 84228ns/op
2018/02/25 09:36:25 500 selects in CockroachDB took 422.683737ms, with an average of 845367ns/op
2018/02/25 09:36:26 500 deletes in PgSQL took 650.923468ms, with an average of 1301846ns/op
2018/02/25 09:36:28 500 deletes in CockroachDB took 2.545088971s, with an average of 5090177ns/op

I then tried to change the cockroach execution by adding --cache=25% --max-sql-memory=25% to every instance but the results are better but still very far from pgsql ones:

fale@x250:~/go/src/github.com/fale/pg-benchmark $ go build && ./pg-benchmark 
2018/02/25 09:47:22 # Ensure schema is present - PgSQL
2018/02/25 09:47:22 # Ensure database is present - CockroachDB
2018/02/25 09:47:22 # Ensure table is present and empty - PgSQL
2018/02/25 09:47:22 # Ensure table is present and empty - CockroachDB
2018/02/25 09:47:50 500 inserts in PgSQL took 750.454098ms, with an average of 1500908ns/op
2018/02/25 09:47:53 500 inserts in CockroachDB took 2.250529374s, with an average of 4501058ns/op
2018/02/25 09:47:53 500 selects in PgSQL took 61.181155ms, with an average of 122362ns/op
2018/02/25 09:47:53 500 selects in CockroachDB took 190.542076ms, with an average of 381084ns/op
2018/02/25 09:47:54 500 deletes in PgSQL took 686.743754ms, with an average of 1373487ns/op
2018/02/25 09:47:57 500 deletes in CockroachDB took 3.095964455s, with an average of 6191928ns/op
fale@x250:~/go/src/github.com/fale/pg-benchmark $ go build && ./pg-benchmark 
2018/02/25 09:48:16 # Ensure schema is present - PgSQL
2018/02/25 09:48:16 # Ensure database is present - CockroachDB
2018/02/25 09:48:16 # Ensure table is present and empty - PgSQL
2018/02/25 09:48:16 # Ensure table is present and empty - CockroachDB
2018/02/25 09:48:17 500 inserts in PgSQL took 650.64161ms, with an average of 1301283ns/op
2018/02/25 09:48:20 500 inserts in CockroachDB took 3.058602056s, with an average of 6117204ns/op
2018/02/25 09:48:20 500 selects in PgSQL took 42.282658ms, with an average of 84565ns/op
2018/02/25 09:48:21 500 selects in CockroachDB took 175.840808ms, with an average of 351681ns/op
2018/02/25 09:48:21 500 deletes in PgSQL took 662.712318ms, with an average of 1325424ns/op
2018/02/25 09:48:23 500 deletes in CockroachDB took 2.2847881s, with an average of 4569576ns/op
fale@x250:~/go/src/github.com/fale/pg-benchmark $ go build && ./pg-benchmark 
2018/02/25 09:48:39 # Ensure schema is present - PgSQL
2018/02/25 09:48:39 # Ensure database is present - CockroachDB
2018/02/25 09:48:39 # Ensure table is present and empty - PgSQL
2018/02/25 09:48:39 # Ensure table is present and empty - CockroachDB
2018/02/25 09:48:40 500 inserts in PgSQL took 662.140623ms, with an average of 1324281ns/op
2018/02/25 09:48:43 500 inserts in CockroachDB took 3.056347724s, with an average of 6112695ns/op
2018/02/25 09:48:43 500 selects in PgSQL took 48.732729ms, with an average of 97465ns/op
2018/02/25 09:48:43 500 selects in CockroachDB took 185.466359ms, with an average of 370932ns/op
2018/02/25 09:48:44 500 deletes in PgSQL took 655.312292ms, with an average of 1310624ns/op
2018/02/25 09:48:46 500 deletes in CockroachDB took 2.282639324s, with an average of 4565278ns/op

Surely my test is not a perfect match with a real workload (I write first, read second, delete third without mixed ops, for instance), also I would expect some kind of performance differences (since cockroach is distributed/multinode) mainly on inserts/deletes, but I was not expecting such performance gap. I wonder if I did something that makes the test "wrong" or if this performance gap is known and is probably the same I could expect on a real workload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-performancePerf of queries or internals. Solution not expected to change functional behavior.O-communityOriginated from the communityX-wontfixClosed as we're not going to fix it, even though it's a legitimate issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions