\ir ./begin.sql

CREATE OR REPLACE FUNCTION test_perf(num integer = 200000) RETURNS void
LANGUAGE plpgsql
SET search_path FROM CURRENT
AS $$
DECLARE
  v integer;
  ts timestamptz;
BEGIN
  ts := clock_timestamp();
  num := 16000; -- since we have limit of ids generated per second
  SELECT count(DISTINCT snowflake_gen()) FROM generate_series(1, num) INTO v;
  IF v <> num THEN
    RAISE EXCEPTION 'Invalid number of snowflake_gen() distinct values: % (expected %)', v, num;
  END IF;
  RAISE NOTICE 'snowflake_gen(): % reqs/s', round(v / EXTRACT(SECONDS FROM clock_timestamp() - ts));
END;
$$;

SELECT test_perf() \gset

DROP FUNCTION test_perf(integer);

\ir ./rollback.sql
