#!/bin/bash

dropdb test
createdb test

export PGDATABASE=test

psql -c '
create table j (id bigserial, j jsonb);
create index on j using gin (j);
alter index j_j_idx set (fastupdate = off);
'
psql -c '
alter system set synchronous_commit to off;
'
pg_ctl reload

for g in `seq 1 100`; do
  for f in `seq 1 10`; do
    echo "trying with $f clients"
    psql -c 'truncate j'
    pgbench -n -f <(echo 'insert into j (j) select jsonb_object_agg(x::text, left(md5(random()::text),5)) from generate_series(1,10) f(x);') -T 60 -c $f -j $f | grep tps
  done
done
