Shell: Add a "time" builtin to show how long a command took to run
This commit is contained in:
parent
f2b6e1b577
commit
3596522d23
Notes:
sideshowbarker
2024-07-19 12:04:57 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/3596522d236
1 changed files with 25 additions and 0 deletions
|
@ -25,6 +25,8 @@
|
|||
GlobalState g;
|
||||
static LineEditor editor;
|
||||
|
||||
static int run_command(const String&);
|
||||
|
||||
static String prompt()
|
||||
{
|
||||
if (g.uid == 0)
|
||||
|
@ -133,6 +135,25 @@ static int sh_history(int, char**)
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int sh_time(int argc, char** argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
printf("usage: time <command>\n");
|
||||
return 0;
|
||||
}
|
||||
StringBuilder builder;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
builder.append(argv[i]);
|
||||
if (i != argc - 1)
|
||||
builder.append(' ');
|
||||
}
|
||||
CElapsedTimer timer;
|
||||
timer.start();
|
||||
int exit_code = run_command(builder.to_string());
|
||||
printf("Time: %d ms\n", timer.elapsed());
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
static int sh_umask(int argc, char** argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
|
@ -392,6 +413,10 @@ static bool handle_builtin(int argc, char** argv, int& retval)
|
|||
retval = sh_popd(argc, argv);
|
||||
return true;
|
||||
}
|
||||
if (!strcmp(argv[0], "time")) {
|
||||
retval = sh_time(argc, argv);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue