ITPub博客

首页 > 数据库 > MySQL > 测试软件:sysbench-oltp(2)

测试软件:sysbench-oltp(2)

原创 MySQL 作者:javenzhen 时间:2015-08-02 08:35:22 0 删除 编辑
1、在上一篇中查看了common.lua脚本的相关内容,下面会介绍oltp.lua脚本
2、oltp.lua
  1. function thread_init(thread_id)
  2.    set_vars()

  3.    if (((db_driver == "mysql") or (db_driver == "attachsql")) and mysql_table_engine == "myisam") then
  4.       begin_query = "LOCK TABLES sbtest WRITE"
  5.       commit_query = "UNLOCK TABLES"
  6.    else
  7.       begin_query = "BEGIN"
  8.       commit_query = "COMMIT"
  9.    end

  10. end
2.1、在线程初始化的时候,如果数据库是mysql且表的存储引擎为myisam的时候,需要先锁表,在commit之后unlock
  1. for i=1, oltp_point_selects do
  2.       rs = db_query("SELECT c FROM ".. table_name .." WHERE id=" .. sb_rand(1, oltp_table_size))
  3.    end

  4.    for i=1, oltp_simple_ranges do
  5.       range_start = sb_rand(1, oltp_table_size)
  6.       rs = db_query("SELECT c FROM ".. table_name .." WHERE id BETWEEN " .. range_start .. " AND " .. range_start .. "+" .. oltp_range_size - 1)
  7.    end
  8.   
  9.    for i=1, oltp_sum_ranges do
  10.       range_start = sb_rand(1, oltp_table_size)
  11.       rs = db_query("SELECT SUM(K) FROM ".. table_name .." WHERE id BETWEEN " .. range_start .. " AND " .. range_start .. "+" .. oltp_range_size - 1)
  12.    end
  13.    
  14.    for i=1, oltp_order_ranges do
  15.       range_start = sb_rand(1, oltp_table_size)
  16.       rs = db_query("SELECT c FROM ".. table_name .." WHERE id BETWEEN " .. range_start .. " AND " .. range_start .. "+" .. oltp_range_size - 1 .. " ORDER BY c")
  17.    end

   for i=1, oltp_distinct_ranges do
      range_start = sb_rand(1, oltp_table_size)
      rs = db_query("SELECT DISTINCT c FROM ".. table_name .." WHERE id BETWEEN " .. range_start .. " AND " .. range_start .. "+" .. oltp_range_size - 1 .. " ORDER BY c")
   end

2.2、第1个测试语句select,是主键等值查询;
2.3、第2个测试语句select,是主键范围查询;
2.4、第3个测试语句select,是主键范围查询,并为列k计算求和;
2.5、第4个测试语句select,是主键范围查询,且为列c排序;
2.6、第5个测试语句select,是主键范围查询,且找出c列的distinct值,并排序
2.7、下面代码显示的是,如果oltp应用不是只读的,就会有DML操作,这里就不一个个介绍了,代码比较简单。
  1. if not oltp_read_only then

  2.    for i=1, oltp_index_updates do
  3.       rs = db_query("UPDATE " .. table_name .. " SET k=k+1 WHERE id=" .. sb_rand(1, oltp_table_size))
  4.    end

  5.    for i=1, oltp_non_index_updates do
  6.       c_val = sb_rand_str("###########-###########-###########-###########-###########-###########-###########-###########-###########-###########")
  7.       query = "UPDATE " .. table_name .. " SET c='" .. c_val .. "' WHERE id=" .. sb_rand(1, oltp_table_size)
  8.       rs = db_query(query)
  9.       if rs then
  10.         print(query)
  11.       end
  12.    end

  13.    i = sb_rand(1, oltp_table_size)

  14.    rs = db_query("DELETE FROM " .. table_name .. " WHERE id=" .. i)
  15.    
  16.    c_val = sb_rand_str([[
  17. ###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])
  18.    pad_val = sb_rand_str([[
  19. ###########-###########-###########-###########-###########]])

  20.    rs = db_query("INSERT INTO " .. table_name .. " (id, k, c, pad) VALUES " .. string.format("(%d, %d, '%s', '%s')",i, sb_rand(1, oltp_table_size) , c_val
  21. , pad_val))

  22.    end -- oltp_read_only

  23.    if not oltp_skip_trx then
  24.       db_query(commit_query)
  25.    end

  26. end

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12219480/viewspace-1759130/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-01-27

  • 博文量
    41
  • 访问量
    124941