Added sb_rand_str()
This commit is contained in:
@ -125,6 +125,7 @@ static int sb_lua_db_free_results(lua_State *);
|
||||
static int sb_lua_rand(lua_State *);
|
||||
static int sb_lua_rand_uniq(lua_State *);
|
||||
static int sb_lua_rnd(lua_State *);
|
||||
static int sb_lua_rand_str(lua_State *);
|
||||
|
||||
/* Get a per-state interpreter context */
|
||||
static sb_lua_ctxt_t *sb_lua_get_context(lua_State *);
|
||||
@ -352,6 +353,9 @@ lua_State *sb_lua_new_state(const char *scriptname)
|
||||
|
||||
lua_pushcfunction(state, sb_lua_rnd);
|
||||
lua_setglobal(state, "sb_rnd");
|
||||
|
||||
lua_pushcfunction(state, sb_lua_rand_str);
|
||||
lua_setglobal(state, "sb_rand_str");
|
||||
|
||||
lua_pushcfunction(state, sb_lua_db_connect);
|
||||
lua_setglobal(state, "db_connect");
|
||||
@ -853,6 +857,19 @@ int sb_lua_rnd(lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sb_lua_rand_str(lua_State *L)
|
||||
{
|
||||
const char *fmt = luaL_checkstring(L, -1);
|
||||
char *buf = strdup(fmt);
|
||||
|
||||
sb_rand_str(fmt, buf);
|
||||
lua_pushstring(L, buf);
|
||||
|
||||
free(buf);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Get a per-state interpreter context */
|
||||
|
||||
sb_lua_ctxt_t *sb_lua_get_context(lua_State *L)
|
||||
|
||||
@ -801,3 +801,20 @@ int sb_rand_uniq(int a, int b)
|
||||
}
|
||||
|
||||
|
||||
/* Generate random string */
|
||||
|
||||
|
||||
void sb_rand_str(const char *fmt, char *buf)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i=0; fmt[i] != '\0'; i++)
|
||||
{
|
||||
if (fmt[i] == '#')
|
||||
buf[i] = sb_rand_uniform('0', '9');
|
||||
else if (fmt[i] == '@')
|
||||
buf[i] = sb_rand_uniform('a', 'z');
|
||||
else
|
||||
buf[i] = fmt[i];
|
||||
}
|
||||
}
|
||||
|
||||
@ -204,5 +204,6 @@ int sb_rand_uniform(int, int);
|
||||
int sb_rand_gaussian(int, int);
|
||||
int sb_rand_special(int, int);
|
||||
int sb_rand_uniq(int a, int b);
|
||||
void sb_rand_str(const char *, char *);
|
||||
|
||||
#endif
|
||||
|
||||
@ -82,9 +82,10 @@ function event(thread_id)
|
||||
params[1] = sb_rand_uniq(1, oltp_table_size)
|
||||
end
|
||||
params[2]= sb_rand(1, oltp_table_size)
|
||||
params[3] = sb_rnd() .. '-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd() .. '-' ..
|
||||
sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()
|
||||
params[4] = sb_rnd()..'-'.. sb_rnd()..'-'.. sb_rnd()..'-'.. sb_rnd()..'-'.. sb_rnd()
|
||||
params[3] = sb_rand_str([[
|
||||
###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])
|
||||
params[4] = sb_rand_str([[
|
||||
###########-###########-###########-###########-###########]])
|
||||
rs = db_execute(stmt)
|
||||
end
|
||||
|
||||
|
||||
@ -170,8 +170,8 @@ function event(thread_id)
|
||||
update_idx_params[1] = sb_rand(1, oltp_table_size)
|
||||
rs = db_execute(update_idx_stmt)
|
||||
|
||||
update_nonidx_params[1] = sb_rnd() .. '-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd() .. '-' ..
|
||||
sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()
|
||||
update_nonidx_params[1] = sb_rand_str([[
|
||||
###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])
|
||||
update_nonidx_params[2] = sb_rand(1, oltp_table_size)
|
||||
rs = db_execute(update_nonidx_stmt)
|
||||
|
||||
|
||||
@ -85,7 +85,7 @@ function thread_init(thread_id)
|
||||
set_vars()
|
||||
|
||||
stmt = db_prepare([[
|
||||
UPDATE %s set c=? where id=?
|
||||
UPDATE sbtest set c=? where id=?
|
||||
]])
|
||||
params = {"", 0}
|
||||
db_bind_param(stmt, params)
|
||||
@ -93,8 +93,8 @@ end
|
||||
|
||||
function event(thread_id)
|
||||
local rs
|
||||
params[1] = sb_rnd() .. '-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd() .. '-' ..
|
||||
sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()..'-'..sb_rnd()
|
||||
params[1] = sb_rand_str([[
|
||||
###########-###########-###########-###########-###########-###########-###########-###########-###########-###########]])
|
||||
params[2] = sb_rand(1, oltp_table_size)
|
||||
rs = db_execute(stmt)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user