Add the ".timer" command to the CLI. (CVS 4522)

FossilOrigin-Name: 56680360d3b14a66a077ebb735f4594ed524a4bb
This commit is contained in:
drh 2007-11-02 12:53:03 +00:00
parent 7b12d46146
commit 3b1a9881e7
3 changed files with 68 additions and 9 deletions

View File

@ -1,5 +1,5 @@
C Fix\sbuilding\sof\ssqlite3_analyzer\son\sMac.\s(CVS\s4521)
D 2007-11-02T12:52:40
C Add\sthe\s".timer"\scommand\sto\sthe\sCLI.\s(CVS\s4522)
D 2007-11-02T12:53:04
F Makefile.in 30c7e3ba426ddb253b8ef037d1873425da6009a8
F Makefile.linux-gcc 65241babba6faf1152bf86574477baab19190499
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -130,7 +130,7 @@ F src/printf.c 96c8d55315a13fc53cb3754cb15046f3ff891ea2
F src/random.c 4a22746501bf36b0a088c66e38dde5daba6a35da
F src/select.c 4706a6115da1bdc09a2be5991168a6cc2c0df267
F src/server.c 087b92a39d883e3fa113cae259d64e4c7438bc96
F src/shell.c 82089379833e361ba8a2ae65316a2173785300c0
F src/shell.c 0b9dd90afc34f28b8786638155d32f6248d0bf0a
F src/sqlite.h.in 430a26215c164a2d4236dcbce4730a704b455271
F src/sqlite3ext.h a93f59cdee3638dc0c9c086f80df743a4e68c3cb
F src/sqliteInt.h 7fd5cfa357d7aefe22cd2bcdfabcca4e7d5ab5b2
@ -584,7 +584,7 @@ F www/tclsqlite.tcl 8be95ee6dba05eabcd27a9d91331c803f2ce2130
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl fc46eae081251c3c181bd79c5faef8195d7991a5
P 6340ca5eee3d398a9ef4f37a442efad37c9bf547
R 0015f0586bb0a17a8ebee567349ffb1c
P a616b6cb646a35a68bebc7d013c13185a9a6f47d
R 3c475471a85720fe0d75e54f60c99bbc
U drh
Z 8e366ad6caa4aac98afe5c1c8f0199c2
Z c5d9d02a9879c0e073ce1cae0bcfc7b6

View File

@ -1 +1 @@
a616b6cb646a35a68bebc7d013c13185a9a6f47d
56680360d3b14a66a077ebb735f4594ed524a4bb

View File

@ -12,7 +12,7 @@
** This file contains code to implement the "sqlite" command line
** utility for accessing SQLite databases.
**
** $Id: shell.c,v 1.167 2007/09/07 01:12:32 drh Exp $
** $Id: shell.c,v 1.168 2007/11/02 12:53:04 drh Exp $
*/
#include <stdlib.h>
#include <string.h>
@ -61,6 +61,53 @@
extern int isatty();
#endif
#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__)
#include <sys/time.h>
#include <sys/resource.h>
/* Saved resource information for the beginning of an operation */
static struct rusage sBegin;
/* True if the timer is enabled */
static int enableTimer = 0;
/*
** Begin timing an operation
*/
static void beginTimer(void){
if( enableTimer ){
getrusage(RUSAGE_SELF, &sBegin);
}
}
/* Return the difference of two time_structs in microseconds */
static int timeDiff(struct timeval *pStart, struct timeval *pEnd){
return (pEnd->tv_usec - pStart->tv_usec) +
1000000*(pEnd->tv_sec - pStart->tv_sec);
}
/*
** Print the timing results.
*/
static void endTimer(void){
if( enableTimer ){
struct rusage sEnd;
getrusage(RUSAGE_SELF, &sEnd);
printf("CPU Time: user %f sys %f\n",
0.000001*timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
0.000001*timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
}
}
#define BEGIN_TIMER beginTimer()
#define END_TIMER endTimer()
#define HAS_TIMER 1
#else
#define BEGIN_TIMER
#define END_TIMER
#define HAS_TIMER 0
#endif
/*
** If the following flag is set, then command execution stops
** at an error if we are not interactive.
@ -884,6 +931,9 @@ static char zHelp[] =
".show Show the current values for various settings\n"
".tables ?PATTERN? List names of tables matching a LIKE pattern\n"
".timeout MS Try opening locked tables for MS milliseconds\n"
#if HAS_TIMER
".timer ON|OFF Turn the CPU timer measurement on or off\n"
#endif
".width NUM NUM ... Set column widths for \"column\" mode\n"
;
@ -1515,10 +1565,16 @@ static int do_meta_command(char *zLine, struct callback_data *p){
sqlite3_free_table(azResult);
}else
if( c=='t' && n>1 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg>=2 ){
open_db(p);
sqlite3_busy_timeout(p->db, atoi(azArg[1]));
}else
#if HAS_TIMER
if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg>1 ){
enableTimer = booleanValue(azArg[1]);
}else
#endif
if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
int j;
@ -1528,6 +1584,7 @@ static int do_meta_command(char *zLine, struct callback_data *p){
}
}else
{
fprintf(stderr, "unknown command or invalid arguments: "
" \"%s\". Enter \".help\" for help\n", azArg[0]);
@ -1660,7 +1717,9 @@ static int process_input(struct callback_data *p, FILE *in){
&& sqlite3_complete(zSql) ){
p->cnt = 0;
open_db(p);
BEGIN_TIMER;
rc = sqlite3_exec(p->db, zSql, callback, p, &zErrMsg);
END_TIMER;
if( rc || zErrMsg ){
char zPrefix[100];
if( in!=0 || !stdin_is_interactive ){