Commit 5d67648d authored by Yuanle Song's avatar Yuanle Song
Browse files

added psycopg2 and libpq results

parents
Loading
Loading
Loading
Loading
Loading

README.org

0 → 100644
+41 −0
Original line number Diff line number Diff line
* COMMENT -*- mode: org -*-
#+Date: 2016-12-16
Time-stamp: <2016-12-16>

* database binding benchmark
I want to know the efficiency of the db binding for different language,
libraries and database servers.

- table:
  CREATE TABLE foo
  (id integer primary key,
   bar integer);
  INSERT INTO foo (id, bar) VALUES (1, 1);

- run many times:
  SELECT * FROM foo
  UPDATE foo SET bar=bar+1 WHERE id=1

* benchmark results
** postgres 9.4, cpython 2.7, psycopg2==2.6.1
http://initd.org/psycopg/docs/usage.html
0.35
28.77
--
0.38
26.15
--
0.37
26.35
** postgres 9.4, C, libpq5
https://www.postgresql.org/docs/9.4/static/libpq.html
0.33
26.6
--
0.32
26.5
--
0.30
26.2

//side note: why adding -shared in LDFLAGS will just segfault the program?

c-libpq5/Makefile

0 → 100644
+3 −0
Original line number Diff line number Diff line
CFLAGS = -std=c99 -O2 -fPIC -pedantic -Wall -Wextra -I/usr/include/postgresql
LDFLAGS = -lpq
bench: bench.c

c-libpq5/bench

0 → 100755
+9.45 KiB

File added.

No diff preview for this file type.

c-libpq5/bench.c

0 → 100644
+85 −0
Original line number Diff line number Diff line
/**
 * run simple select and update benchmark.
 */
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <libpq-fe.h>

#define TIMES 3000

static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

void
select_test(PGconn *conn)
{
	PGresult *res;
	for (int i = 0; i < TIMES; ++i) {
		res = PQexec(conn, "SELECT id, bar FROM foo");
		if (PQresultStatus(res) != PGRES_TUPLES_OK)
		{
			fprintf(stderr, "SELECT command failed: %s",
				PQerrorMessage(conn));
			PQclear(res);
			exit_nicely(conn);
		}
		PQclear(res);
	}
}

void
update_test(PGconn *conn)
{
	PGresult *res;
	for (int i = 0; i < TIMES; ++i) {
		res = PQexec(conn, "UPDATE foo SET bar=bar+1 WHERE id=1");
		if (PQresultStatus(res) != PGRES_COMMAND_OK)
		{
			fprintf(stderr, "UPDATE command failed: %s",
				PQerrorMessage(conn));
			PQclear(res);
			exit_nicely(conn);
		}
		PQclear(res);
	}
}

double diff_microseconds(struct timeval *start, struct timeval *end)
{
	return (double)((end->tv_sec * 1000000 + end->tv_usec) -
			(start->tv_sec * 1000000 + start->tv_usec));
}

int
main()
{
	struct timeval start, end;
	const char *conninfo = "host=localhost dbname=t1 user=t1 password=fNfwREMqO69TB9YqE+/OzF5/k+s=";
	PGconn *conn;

	conn = PQconnectdb(conninfo);
	if (PQstatus(conn) != CONNECTION_OK)
	{
		fprintf(stderr, "Connection to database failed: %s",
			PQerrorMessage(conn));
		exit_nicely(conn);
	}

	gettimeofday(&start, NULL);
	select_test(conn);
	gettimeofday(&end, NULL);
	printf("%.3f\n", diff_microseconds(&start, &end) / 1000000.0);

	gettimeofday(&start, NULL);
	update_test(conn);
	gettimeofday(&end, NULL);
	printf("%.3f\n", diff_microseconds(&start, &end) / 1000000.0);

	PQfinish(conn);
	return 0;
}
+50 −0
Original line number Diff line number Diff line
#!/usr/bin/env python
# coding=utf-8

"""
run db benchmark
"""

from contextlib import contextmanager
import logging

import psycopg2

from yygame.debug import trace_time

logging.basicConfig(format='%(levelname)-8s %(message)s', level=logging.DEBUG)
logger = logging.getLogger('')


@trace_time()
def select_test(conn, times=3000):
    cur = conn.cursor()
    for i in range(times):
        cur.execute(u"""\
SELECT id, bar FROM foo
""")
        cur.fetchone()


@trace_time()
def update_test(conn, times=3000):
    cur = conn.cursor()
    for i in range(times):
        cur.execute(u"""\
UPDATE foo SET bar=bar+1 WHERE id=1
""")
        conn.commit()


def main():
    conn = psycopg2.connect(host="localhost",
                            database="t1",
                            user="t1",
                            password="fNfwREMqO69TB9YqE+/OzF5/k+s=")
    logger.info("running benchmark...")
    select_test(conn)
    update_test(conn)


if __name__ == '__main__':
    main()
Loading