Skip to content

Instantly share code, notes, and snippets.

@abicky
Last active April 26, 2020 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abicky/1ca06e634d613c10ad068bfbde6d3278 to your computer and use it in GitHub Desktop.
Save abicky/1ca06e634d613c10ad068bfbde6d3278 to your computer and use it in GitHub Desktop.
redis-bench
require "time"
require "redis"
CLIENT_COUNT = ENV.fetch("CLIENT_COUNT", 1).to_i
REPEAT_COUNT = 10
STARTED_AT = Time.parse(ENV["STARTED_AT"])
client = Redis.new(host: ENV["REDIS_HOST"])
key = "shop_id:1:#{Date.today}"
count = client.get(key) || 0
puts "Wait until #{STARTED_AT}"
sleep 0.001 until Time.now > STARTED_AT
puts "Start (count = #{count})"
prng = Random.new(42)
total_time = 0
ths = Array.new(CLIENT_COUNT) do
sleep_times = REPEAT_COUNT.times.map { prng.rand }
Thread.new do
c = Redis.new(host: ENV["REDIS_HOST"])
REPEAT_COUNT.times do |i|
s = Process.clock_gettime(Process::CLOCK_MONOTONIC)
c.incrby(key, 1)
total_time += Process.clock_gettime(Process::CLOCK_MONOTONIC) - s
sleep sleep_times[i]
end
end
end
ths.each(&:join)
puts "Client count: #{CLIENT_COUNT}"
puts "Total query time: #{total_time.round(3)}"
puts "Finish (count = #{client.get(key) || 0})"
FROM ruby:2.6
WORKDIR /app
RUN mkdir vendor
COPY Gemfile Gemfile.lock /app/
RUN bundle install --no-cache -j4
COPY setup.rb countup.rb /app/
ENTRYPOINT ["bundle", "exec", "ruby"]
CMD ["countup.rb"]
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "redis"
GEM
remote: https://rubygems.org/
specs:
redis (4.1.3)
PLATFORMS
ruby
DEPENDENCIES
redis
BUNDLED WITH
1.7
#!/bin/bash
set -eo pipefail
REPOSITORY=redis-bench
TASK_FAMILY=redis-bench
for arg_name in REDIS_HOST STARTED_AT TASK_COUNT; do
if [ -z "${!arg_name}" ]; then
echo "\$$arg_name must be specified" >&2
exit 1
fi
done
dir=$(cd $(dirname $0) && pwd)
cd $dir
repo_uri=$((aws ecr describe-repositories --repository-names $REPOSITORY | jq -r '.repositories[] | .repositoryUri') || true)
if [ -z "$repo_uri" ]; then
echo "Create ECR repository '$REPOSITORY'"
repo_uri=$(aws ecr create-repository --repository-name $REPOSITORY | jq -r '.repository.repositoryUri')
fi
echo "Build logger docker image"
docker build . -t $REPOSITORY
echo "Push $repo_uri:latest"
docker tag $REPOSITORY:latest $repo_uri:latest
aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin $repo_uri
docker push $repo_uri:latest
AWS_PAGER="" aws ecs register-task-definition --cli-input-json "$(cat <<JSON
{
"family": "$TASK_FAMILY",
"containerDefinitions": [
{
"name": "main",
"image": "$repo_uri",
"essential": true,
"environment": [
{
"name": "REDIS_HOST",
"value": "$REDIS_HOST"
},
{
"name": "STARTED_AT",
"value": "$STARTED_AT"
},
{
"name": "CLIENT_COUNT",
"value": "4"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/$TASK_FAMILY",
"awslogs-region": "ap-northeast-1",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"cpu": "128",
"memory": "128"
}
JSON
)"
# Execute first the following command:
#
# aws ecs run-task --cluster $CLUSTER --task-definition $TASK_FAMILY --overrides '{
# "containerOverrides": [
# {
# "name": "main",
# "command": ["setup.rb"]
# }
# ]
# }'
launched_count=0
while [ "$launched_count" -lt "$TASK_COUNT" ]; do
task_count=$(($TASK_COUNT - $launched_count))
if [ $task_count -ge 10 ]; then
task_count=10
fi
launched_count=$(($launched_count + $task_count))
echo "Launch $launched_count/$TASK_COUNT tasks"
resp=$(aws ecs run-task --cluster $CLUSTER --task-definition $TASK_FAMILY --count $task_count)
if [ "$(echo $resp | jq '.failures | length')" -ne 0 ]; then
echo -e "$resp" >&2
exit 1
fi
done
require "date"
require "redis"
client = Redis.new(host: ENV["REDIS_HOST"])
1000.times do |i|
shop_id = i + 1
client.pipelined do
((Date.today - 365) .. Date.today).each do |ordered_on|
client.set("shop_id:#{shop_id}:#{ordered_on}", 0)
end
end
end
puts "Succeeded in creating items"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment