# Simple serial document creation benchmark for couchdb
#  (c) 2008 - Bas Westerbaan <bas@fsfe.org>    GPL v. 3

import sys
import time
import couchdb
import threading

class Counter(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self._count = 0

    def count(self):
        self._count += 1
        
    def run(self):
        while True:
            c_i = self._count
            t_i = time.time()
            time.sleep(2)
            t_f = time.time()
            c_f = self._count

            print "%s per sec" % (float(c_f - c_i) / (t_f - t_i))

dbname = sys.argv[1] if len(sys.argv) >= 2 else 'test'
uri = sys.argv[2] if len(sys.argv) >= 3 else 'http://localhost:5984'

serv = couchdb.Server(uri)

try: serv[dbname].resource.delete()
except couchdb.ResourceNotFound: pass

db = serv.create(dbname)

c = Counter()
c.start()
while True:
    db.create({})
    c.count()
