Skip to content

Commit

Permalink
nix(feat): Add postgrest-loadtest
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Apr 18, 2021
1 parent 68cf0de commit 06d2163
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
cachix use postgrest
- run:
name: Install testing scripts
command: nix-env -f default.nix -iA tests memory withTools
command: nix-env -f default.nix -iA tests memory withTools loadtest
- run:
name: Run coverage (io tests and spec tests against PostgreSQL 13)
command: postgrest-coverage
Expand Down Expand Up @@ -214,6 +214,10 @@ jobs:
name: Check the spec tests for idempotence
command: postgrest-test-spec-idempotence
when: always
- run:
name: Run load test
command: postgrest-loadtest-against main
when: always
- run:
name: Run memory tests
command: postgrest-test-memory
Expand Down
4 changes: 4 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,8 @@ rec {
inherit docker;
postgrest = postgrestStatic;
};

# Load testing tools.
loadtest =
pkgs.callPackage nix/tools/loadtest.nix { inherit withTools; };
}
106 changes: 106 additions & 0 deletions nix/tools/loadtest.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{ buildToolbox
, checkedShellScript
, vegeta
, withTools
}:
let
runner =
checkedShellScript
{
name = "postgrest-loadtest-runner";
docs = "Run vegeta. Assume PostgREST to be running.";
args = [
"ARG_LEFTOVERS([additional vegeta arguments])"
"ARG_USE_ENV([PGRST_SERVER_UNIX_SOCKET], [], [Unix socket to connect to running PostgREST instance])"
];
}
''
# ARG_USE_ENV only adds defaults or docs for environment variables
# We manually implement a required check here
# See also: https://github.com/matejak/argbash/issues/80
PGRST_SERVER_UNIX_SOCKET="''${PGRST_SERVER_UNIX_SOCKET:?PGRST_SERVER_UNIX_SOCKET is required}"
${vegeta}/bin/vegeta -cpus 1 attack \
-unix-socket "$PGRST_SERVER_UNIX_SOCKET" \
-max-workers 1 \
-workers 1 \
-rate 0 \
-duration 5s \
"''${_arg_leftovers[@]}"
'';

loadtest =
checkedShellScript
{
name = "postgrest-loadtest";
docs = "Run the vegeta loadtests with PostgREST.";
args = [
"ARG_OPTIONAL_SINGLE([testfile], [t], [File to load tests from], [./test/loadtest.http])"
"ARG_LEFTOVERS([additional vegeta arguments])"
];
inRootDir = true;
}
''
export PGRST_DB_POOL="1"
export PGRST_LOG_LEVEL="crit"
export PGRST_DB_CONFIG="false"
${withTools.latest} \
${withTools.pgrst} \
${runner} -targets "$_arg_testfile" "''${_arg_leftovers[@]}" \
| ${vegeta}/bin/vegeta report -type=text
'';

loadtestAgainst =
checkedShellScript
{
name = "postgrest-loadtest-against";
docs =
''
Run the vegeta loadtest twice:
- once on the <target> branch
- once in the current worktree
'';
args = [
"ARG_POSITIONAL_SINGLE([target], [Commit-ish reference to compare with])"
"ARG_LEFTOVERS([additional vegeta arguments])"
];
inRootDir = true;
}
''
cat << EOF
Running loadtest on "$_arg_target"...
EOF
# Runs the test files from the current working tree
# to make sure both tests are run with the same files.
${withTools.git} "$_arg_target" ${loadtest} --testfile "$PWD/test/loadtest.http" "''${_arg_leftovers[@]}"
cat << EOF
Done running on "$_arg_target".
EOF
cat << EOF
Running loadtest on HEAD...
EOF
${loadtest} --testfile "$PWD/test/loadtest.http" "''${_arg_leftovers[@]}"
cat << EOF
Done running on HEAD.
EOF
'';

in
buildToolbox {
name = "postgrest-loadtest";
tools = [ loadtest loadtestAgainst ];
}
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ let
toolboxes =
[
postgrest.devtools
postgrest.loadtest
postgrest.nixpkgsTools
postgrest.style
postgrest.tests
Expand Down
1 change: 1 addition & 0 deletions test/loadtest.http
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET http://postgrest/orders?select=name#no-headers

0 comments on commit 06d2163

Please sign in to comment.