Skip to content

Instantly share code, notes, and snippets.

@koseki
Created June 4, 2009 16:03
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 koseki/123686 to your computer and use it in GitHub Desktop.
Save koseki/123686 to your computer and use it in GitHub Desktop.
#! /usr/bin/env perl
use TokyoTyrant;
print "start tt ----------------------------------------------------------------\n";
system("./tt.sh start tch 1978");
system("./tt.sh start tct 1979");
sleep(1);
print "hash ----------------------------------------------------------------\n";
my $rdb = TokyoTyrant::RDB->new();
$rdb->open("127.0.0.1", 1978);
for(my $i = 0; $i < 10; $i++) {
my $key = int(rand() * 1000000);
my $value = int(rand() * 1000000);
my $ecode;
$rdb->put($key, $value);
$ecode = $rdb->ecode();
print "put($ecode) $key/$value\n";
if ($i == 5) {
$key = "NA";
}
$value = $rdb->get($key);
$ecode = $rdb->ecode();
print "get($ecode) $key/$value\n";
}
$rdb->close();
print "table ----------------------------------------------------------------\n";
my $rdb = TokyoTyrant::RDBTBL->new();
$rdb->open("127.0.0.1", 1979);
for(my $i = 0; $i < 10; $i++) {
my $key = int(rand() * 1000000);
my $value = int(rand() * 1000000);
my $ecode;
$rdb->put($key, { "value" => $value });
$ecode = $rdb->ecode();
print "put($ecode) $key/$value\n";
if ($i == 5) {
$key = "NA";
}
$value = $rdb->get($key);
$ecode = $rdb->ecode();
print "get($ecode) $key/$value->{value}\n";
}
$rdb->close();
print "stop tt ----------------------------------------------------------------\n";
system("./tt.sh stop tch 1978");
system("./tt.sh stop tct 1979");
#! /usr/bin/env ruby
require "rubygems"
require "tokyotyrant"
DIR = File.dirname(__FILE__)
TCH = {:host => "127.0.0.1", :port => 1978 }
TCT = {:host => "127.0.0.1", :port => 1979 }
TCH_TEST = true
TCT_TEST = true
puts "start tt ----------------------------------------------------------------"
system("#{DIR}/tt.sh start tch #{TCH[:port]}")
system("#{DIR}/tt.sh start tct #{TCT[:port]}")
sleep 1
puts "hash ----------------------------------------------------------------"
if TCH_TEST
conn = TokyoTyrant::RDB.new
conn.open(TCH[:host], TCH[:port])
10.times do |i|
key = rand(1000000).to_s
value = rand(1000000).to_s
conn.put(key, value)
ecode = conn.ecode
puts "put(#{ecode}) #{key}/#{value.inspect}"
key = "NA" if i == 5
value = conn.get(key)
ecode = conn.ecode
puts "get(#{ecode}) #{key}/#{value.inspect}"
end
conn.close
end
puts "table ----------------------------------------------------------------"
if TCT_TEST
conn = TokyoTyrant::RDBTBL.new
conn.open(TCT[:host], TCT[:port])
10.times do |i|
key = rand(1000000).to_s
value = rand(1000000).to_s
conn.put(key, {"value" => value})
ecode = conn.ecode
puts "put(#{ecode}) #{key}/#{value.inspect}"
key = "NA" if i == 5
value = conn.get(key)
ecode = conn.ecode
puts "get(#{ecode}) #{key}/#{value.inspect}"
end
conn.close
end
puts "stop tt ----------------------------------------------------------------"
system("#{DIR}/tt.sh stop tch #{TCH[:port]}")
system("#{DIR}/tt.sh stop tct #{TCT[:port]}")
#! /usr/bin/env ruby
require "rubygems"
require "tokyotyrant"
require "tracer"
DIR = File.dirname(__FILE__)
TCH = {:host => "127.0.0.1", :port => 1978 }
TCT = {:host => "127.0.0.1", :port => 1979 }
# 切断された接続でクエリーを実行する。ハッシュDBの場合。
def tch_closed_connection_test
puts "Hash DB ----------------------------------------------------------------"
system("#{DIR}/tt.sh start tch #{TCH[:port]}")
sleep 1
conn = TokyoTyrant::RDB.new
conn.open(TCH[:host], TCH[:port])
conn.put("a", "b")
system("#{DIR}/tt.sh stop tch #{TCH[:port]}")
puts "-- TT停止"
value = nil
Tracer.on do
value = conn.get("a")
end
puts "-- get通過"
p value
p conn.ecode
conn.close
puts "-- 終了"
end
# 切断された接続でクエリーを実行する。テーブルDBの場合。
def tct_closed_connection_test
puts "Table DB ----------------------------------------------------------------"
system("#{DIR}/tt.sh start tct #{TCT[:port]}")
sleep 1
conn = TokyoTyrant::RDBTBL.new
conn.open(TCT[:host], TCT[:port])
conn.put("a", {"b"=>"123"})
### stop tt ###
system("#{DIR}/tt.sh stop tct #{TCT[:port]}")
puts "-- TT停止"
value = nil
Tracer.on do
value = conn.get("a")
end
puts "-- get通過"
p value
p conn.ecode
conn.close
puts "-- 終了"
end
tch_closed_connection_test
tct_closed_connection_test
#! /usr/bin/env ruby
require "rubygems"
require "tokyotyrant"
DIR = File.dirname(__FILE__)
TCH = {:host => "127.0.0.1", :port => 1978 }
TCT = {:host => "127.0.0.1", :port => 1979 }
class TokyoTyrant::RDBQRY
def search2
return @rdb.misc("search", @args, TokyoTyrant::RDB::MONOULOG)
end
end
def test_qry_norec
system("#{DIR}/tt.sh start tct #{TCT[:port]}")
sleep 1
conn = TokyoTyrant::RDBTBL.new
conn.open(TCT[:host], TCT[:port])
query = TokyoTyrant::RDBQRY.new(conn)
query.addcond("value", TokyoTyrant::RDBQRY::QCSTREQ, "dummy")
p query.search2
system("#{DIR}/tt.sh stop tct #{TCT[:port]}")
query = TokyoTyrant::RDBQRY.new(conn)
query.addcond("value", TokyoTyrant::RDBQRY::QCSTREQ, "dummy")
p query.search2
p conn.ecode
end
test_qry_norec
#! /usr/bin/env ruby
require "rubygems"
require "tokyotyrant"
DIR = File.dirname(__FILE__)
TCH = {:host => "127.0.0.1", :port => 1978 }
TCT = {:host => "127.0.0.1", :port => 1979 }
system("#{DIR}/tt.sh start tct #{TCT[:port]}")
sleep 1
THREAD_NUM = 50
REPEAT = 1000
puts "-" * 80
STDOUT.sync = true
def do_something(conn)
if rand(3) == 0
unless conn.put(rand(10000).to_s, {"value" => "1"})
puts "put error(#{conn.ecode})"
end
else
result = conn.get(rand(10000).to_s)
if !result && conn.ecode != TokyoTyrant::RDBTBL::ENOREC
puts "get error(#{conn.ecode})"
elsif result && result.empty?
puts "*** EMPTY ***"
end
end
end
THREAD_NUM.times do
Thread.start do
begin
conn = TokyoTyrant::RDBTBL.new
conn.open(TCT[:host], TCT[:port])
REPEAT.times do |i|
do_something(conn)
end
rescue => e
p e
ensure
conn.close
end
end
end
Thread.list.each do |th|
th.join unless th == Thread.main
end
puts "-" * 80
system("tcrmgr inform -port #{TCT[:port]} #{TCT[:host]}")
puts "-" * 80
system("#{DIR}/tt.sh stop tct #{TCT[:port]}")
#! /bin/sh
#----------------------------------------------------------------
# Startup script for the server of Tokyo Tyrant
#----------------------------------------------------------------
# configuration variables
prog="ttservctl"
cmd="ttserver"
basedir=$(cd $(dirname $0);pwd)/casket
type=$2
port=$3
pidfile="$basedir/pid_$type"
#logfile="$basedir/log"
#ulogdir="$basedir/ulog"
#ulimsiz="256m"
#sid=1
#mhost="remotehost1"
#mport="1978"
#rtsfile="$basedir/rts"
dbname="$basedir/casket.$type#bnum=1000000"
maxcon="65535"
retval=0
# setting environment variables
LANG=C
LC_ALL=C
PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
export LANG LC_ALL PATH
# start the server
start(){
printf 'Starting the server of Tokyo Tyrant\n'
mkdir -p "$basedir"
if [ -z "$basedir" ] || [ -z "$port" ] || [ -z "$pidfile" ] || [ -z "$dbname" ] ; then
printf 'Invalid configuration\n'
retval=1
elif ! [ -d "$basedir" ] ; then
printf 'No such directory: %s\n' "$basedir"
retval=1
elif [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf 'Existing process: %d\n' "$pid"
retval=1
else
if [ -n "$maxcon" ] ; then
ulimit -n "$maxcon"
fi
cmd="$cmd -port $port -dmn -pid $pidfile"
if [ -n "$logfile" ] ; then
cmd="$cmd -log $logfile"
fi
if [ -n "$ulogdir" ] ; then
mkdir -p "$ulogdir"
cmd="$cmd -ulog $ulogdir"
fi
if [ -n "$ulimsiz" ] ; then
cmd="$cmd -ulim $ulimsiz"
fi
if [ -n "$sid" ] ; then
cmd="$cmd -sid $sid"
fi
if [ -n "$mhost" ] ; then
cmd="$cmd -mhost $mhost"
fi
if [ -n "$mport" ] ; then
cmd="$cmd -mport $mport"
fi
if [ -n "$rtsfile" ] ; then
cmd="$cmd -rts $rtsfile"
fi
printf "Executing: %s\n" "$cmd"
cmd="$cmd $dbname"
$cmd
if [ "$?" -eq 0 ] ; then
printf 'Done\n'
else
printf 'The server could not started\n'
retval=1
fi
fi
}
# stop the server
stop(){
printf 'Stopping the server of Tokyo Tyrant\n'
if [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf "Sending the terminal signal to the process: %s\n" "$pid"
kill -TERM "$pid"
c=0
while true ; do
sleep 0.1
if [ -f "$pidfile" ] ; then
c=`expr $c + 1`
if [ "$c" -ge 100 ] ; then
printf 'Hanging process: %d\n' "$pid"
retval=1
break
fi
else
printf 'Done\n'
break
fi
done
else
printf 'No process found\n'
retval=1
fi
}
# send HUP to the server for log rotation
hup(){
printf 'Sending HUP signal to the server of Tokyo Tyrant\n'
if [ -f "$pidfile" ] ; then
pid=`cat "$pidfile"`
printf "Sending the hangup signal to the process: %s\n" "$pid"
kill -HUP "$pid"
printf 'Done\n'
else
printf 'No process found\n'
retval=1
fi
}
# check permission
if [ -d "$basedir" ] && ! touch "$basedir/$$" >/dev/null 2>&1
then
printf 'Permission denied\n'
exit 1
fi
rm -f "$basedir/$$"
# dispatch the command
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
hup)
hup
;;
*)
printf 'Usage: %s {start|stop|restart|hup}\n' "$prog"
exit 1
;;
esac
# exit
exit "$retval"
# END OF FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment