2012年5月4日金曜日

Macのsandboxを使ってspiderを動かしてみたメモ

■概要
以前にmysql(sql), cassandra, memcache, mysql(handlersocket)でパフォーマンス計測を行った。
spiderでどれくらい変わるのか気になったので測ってみた。
MacのMySQLでHandlerSocketのベンチマークをとった

■注意
spiderは異なるサーバーにスケールすることでパフォーマンスを高める仕組みなので、
1台のMacにMySQL::sandboxでスケールしたところで遅くなることは承知の上です。

本当は、spider onlyとspider + handlersocketの比較がしたかったんだけど、
spider + handlersocketがエラーで比較できず。。。残念。


■準備
MySQL::sandboxに4node立ち上げて、spiderノードとする。
3306portのmysqlにspider masterとなるテーブルを作成する。

・spiderやsandboxのインストールはこちら
Macにmysql5.5.14とspiderとhandlersocketを入れる

■準備作業

# sandbox 4node立ち上げて、テーブルをインストール
./use_all 'use spider_test;
CREATE TABLE `user` (
    `user_id` INT(10) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NULL DEFAULT NULL,
    `birthday` DATE NULL DEFAULT NULL,
    `memo` VARCHAR(255) NULL DEFAULT NULL,
    PRIMARY KEY (`user_id`)
);'


# 3306mysqlにspiderテーブルをインストール
use spider_test;
CREATE TABLE `user` (
  `user_id` INT(10) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NULL DEFAULT NULL,
  `birthday` DATE NULL DEFAULT NULL,
  `memo` VARCHAR(255) NULL DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=SPIDER DEFAULT CHARSET=utf8
PARTITION BY HASH(user_id) (
  PARTITION p1 COMMENT 'user "msandbox", password "msandbox",  host "127.0.0.1", port "13915", table "user",',
  PARTITION p2 COMMENT 'user "msandbox", password "msandbox",  host "127.0.0.1", port "13916", table "user",',
  PARTITION p3 COMMENT 'user "msandbox", password "msandbox",  host "127.0.0.1", port "13917", table "user",',
  PARTITION p4 COMMENT 'user "msandbox", password "msandbox",  host "127.0.0.1", port "13918", table "user",'
);

# 3306mysqlのspiderテーブルからinsert
insert into user (name) values('name1');
insert into user (name) values('name2');
insert into user (name) values('name3');


# sandboxに分散されているか確認
./use_all 'use spider_test;
select * from user;
'
# server: 1: 
# server: 2: 
user_id name birthday memo
1 name1 NULL NULL
# server: 3: 
user_id name birthday memo
2 name2 NULL NULL
# server: 4: 
user_id name birthday memo
3 name3 NULL NULL



■ベンチマークをとるrubyソース

#!/usr/bin/ruby
# -*- encoding: utf-8 -*-
require 'rubygems'
require 'active_record'
require 'benchmark'
 
# ---------------------------------------
# init
# ---------------------------------------
# mysql :
#   mysql
#   use Test; truncate table user;
# memcacha :
#   ps aux | grep mem
#   kill -TERM xxxx
#   memcached -d
# cassandra 
#   cassandra-cli
#   use Test;
#   truncate Users;
 
# ---------------------------------------
# Insert / Select 性能
# ---------------------------------------
count = 10
p 'count:' + count.to_s
 
# ---------------------------------------
# mysql
ActiveRecord::Base.establish_connection(
  :adapter => 'mysql2',
  :host => 'localhost',
  :username => 'root',
  :password => '',
  :database => 'spider_test'
)
class User < ActiveRecord::Base
  self.table_name = 'user'
end
 
# active record 利用
Benchmark.bm { |rep|
  rep.report("mysql(o/r) insert ") {
    for num in 1..count
      name = 'name' + num.to_s
      User.create(:name => name)
    end
  }
}
Benchmark.bm { |rep|
  rep.report("mysql(o/r) select") {
    for num in 1..count
      id = rand(count).to_i + 1
      User.find(id)
    end
  }
}
# すべて削除
User.connection.execute('truncate table user')
 
# SQL利用
Benchmark.bm { |rep|
  rep.report("mysql(sql) insert ") {
    for num in 1..count
      name = 'name' + num.to_s
      User.connection.execute(sprintf('insert into user (user_id, name) values(%s, "%s");', num.to_s, name))
    end
  }
}
Benchmark.bm { |rep|
  rep.report("mysql(sql) select") {
    for num in 1..count
      id = rand(count).to_i + 1
      User.connection.execute(sprintf('select user_id, name from user where user_id = %s;', num.to_s))
    end
  }
}
# すべて削除
User.connection.execute('truncate table user')


■結果

# 結果
"count:100000"
      user     system      total        real
mysql(o/r) insert 101.200000   7.280000 108.480000 (341.227877)
      user     system      total        real
mysql(o/r) select 71.910000   3.450000  75.360000 (294.436478)
      user     system      total        real
mysql(sql) insert   9.000000   2.600000  11.600000 (235.837247)
      user     system      total        real
mysql(sql) select  8.750000   2.700000  11.450000 (245.967628)


# (参考)前回(spiderでなくinnodb)の結果
"count:100000"
      user     system      total        real
mysql(o/r) insert  74.320000   5.670000  79.990000 (110.039184)
      user     system      total        real
mysql(o/r) select 50.610000   2.650000  53.260000 ( 65.465168)
      user     system      total        real
mysql(sql) insert   4.430000   1.530000   5.960000 ( 29.590552)
      user     system      total        real
mysql(sql) select  5.470000   1.780000   7.250000 ( 13.550391)
      user     system      total        real
cassandra insert  47.180000   3.140000  50.320000 ( 61.347904)
      user     system      total        real
cassandra select  69.150000   3.470000  72.620000 (111.568860)
      user     system      total        real
memcache insert  14.930000   2.280000  17.210000 ( 17.935214)
      user     system      total        real
memcache select  16.710000   2.080000  18.790000 ( 19.264883)
      user     system      total        real
handlersocket insert   1.430000   1.510000   2.940000 ( 26.721995)
      user     system      total        real
handlersocket select   1.320000   1.520000   2.840000 (  7.867557)
やっぱ1台じゃ駄目だね。

0 件のコメント:

コメントを投稿