WEB开发网
开发学院数据库Oracle Oracle中资源的共享与竞争及问题定位 阅读

Oracle中资源的共享与竞争及问题定位

 2007-05-13 12:29:55 来源:WEB开发网   
核心提示: C 语言的例子 :#define TV_START 0#define TV_END 1int how_long(int cmd, char *res);struct CMD_TIME{int times;/* times occured within specified package n

C 语言的例子 :

#define TV_START 0
#define TV_END 1
int how_long(int cmd, char *res);
struct CMD_TIME{
int times;
/* times occured within specified package number */
struct timeval time;
/* total time consumed by the cmd */
};
void foo()
{
int id;
how_long(TV_START, NULL);
EXEC SQL WHENEVER SQLERROR CONTINUE;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
EXEC SQL select user_id into :id from users where name='slimzhao';2;
how_long(TV_END, time_consume);
puts(time_consume);
}
int how_long(int cmd, char *res)
/* return value: -1 error, 0 sucess , res: 20 bytes is enough */
{
static struct timeval before, after;
if(cmd == TV_START) {
gettimeofday(&before, NULL);
return 0;
} else if(cmd == TV_END) {
gettimeofday(&after, NULL);
if(res) {
if(after.tv_usec > before.tv_usec) {
sprintf(res, "%ld %ld", after.tv_sec - before.tv_sec,
after.tv_usec - before.tv_usec);
} else {
sprintf(res, "%ld %ld",
after.tv_sec - before.tv_sec - 1,
1000000 + after.tv_usec - before.tv_usec);
}
}
return 0;
} else {
return -1;
}
}

下面是一个 PHP 的例子( 为简化起见 , 程序的错误检查被忽略)

<?
include "<path_to_file>/how_long.inc";
how_long(TV_START, $timestr);
$conn = OCILogon("username", "password", "dblink");
$stmt = OCIParse($conn, "select ID from users where name='slimzhao'");
OCIDefineByName($stmt, ID, $id);
OCIExecute($stmt);
OCIFetch($stmt);
OCIFreeStatement($stmt);
OCILogoff($conn);
how_long(TV_END, $timestr);
echo " 用户 ID: $id , 该操作消耗时间 :$timestr<br>";
?>
其中 how_long 函数的 PHP 版本如下 :
<?
# 作者 : slimzhao@21cn.com
# 当前维护人 : slimzhao@21cn.com
# 创建日期 : 2001.12.04 00:18:00
# 目的 , 在一个操作之前或之后调用该函数的不同版本 , 将得到一个记载了该操作
# 耗费时间的字符串 , 该函数本身的开销不计入其中 .
define("TV_START", 0);
define("TV_END", 1);
function how_long($operation, &$str)
# 返回值 : 0-- 成功 , -1-- 传递了非法的参数 .
{
global $before_SQL, $after_SQL;
if($operation == TV_START) {
$before_SQL = gettimeofday();
return 0;
} else if($operation == TV_END) {
$after_SQL = gettimeofday();
if($before_SQL["usec"] > $after_SQL["usec"]) {
$str = ($after_SQL["sec"] - $before_SQL["sec"] - 1)." 秒 ".
($after_SQL["usec"] + 1000*1000 -$before_SQL["usec"])." 微秒 ";
} else {
$str = ($after_SQL["sec"] - $before_SQL["sec"])." 秒 ".
($after_SQL["usec"]-$before_SQL["usec"])." 微秒 ";
}
} else {
return -1;
}
}
?>

上一页  1 2 3 4 5 6 7  下一页

Tags:Oracle 资源 共享

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接