From a1fecd2e88bdbdedc416a0c45341c0d548a0336a Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 7 Feb 2020 11:05:14 +0300 Subject: [PATCH] cli: add script for comparing storage dumps It uses jq to normalize and compare json dumps. --- scripts/compare-dumps | 63 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 scripts/compare-dumps diff --git a/scripts/compare-dumps b/scripts/compare-dumps new file mode 100755 index 000000000..da41dcd11 --- /dev/null +++ b/scripts/compare-dumps @@ -0,0 +1,63 @@ +#!/bin/sh + +ARG1=$1 +ARG2=$2 +if [ -z "$ARG1" ] || [ -z "$ARG2" ]; then + echo one of the arguments is empty + exit 1 +fi + + +compare() { + # replace replaces storage operation from "Changed" to "Added" + # normalize replaces performs replace and sorts keys in lexicographic order + # next we normalize every json file + # and finally compare them as a whole + jq --argfile x "$1" --argfile y "$2" \ + -n 'def replace: map(if (.state == "Changed") then (.state="Added") else . end); + def normalize: .storage = (.storage | replace | sort_by(.key)); + ($x | map(normalize)) as $x + | ($y | map(normalize)) as $y + | $x | range(length) | . as $i | select($x[$i] != $y[$i]) | $x[$i].block | halt_error(1)' +} + +if [ -f "$ARG1" ] && [ -f "$ARG2" ]; then + compare "$ARG1" "$ARG2" + if [ $? -ne 0 ]; then + echo failed + exit 1 + fi + + exit 0 +fi + +if [ ! -d "$ARG1" ] || [ ! -d "$ARG2" ]; then + echo both arguments must have the same type and exist + exit 1 +fi + +FIRST=$3 +if [ -z "$FIRST" ]; then + FIRST=1 +fi + +LAST=$4 +if [ -z "$LAST" ]; then + LAST=40 # 40_00000 block +fi + +# directories contain 100k blocks +for i in `seq $FIRST $LAST`; do + dir=BlockStorage_${i}00000 + echo Processing directory $dir + + # files are grouped by 1k blocks + for j in `seq $(((i-1)*100 + 1)) $((i*100))`; do + file=dump-block-${j}000.json + compare "$ARG1/$dir/$file" "$ARG2/$dir/$file" + if [ $? -ne 0 ]; then + echo failed on file $dir/$file + exit 1 + fi + done +done