Skip to content

Commit

Permalink
Add .timer command.
Browse files Browse the repository at this point in the history
  • Loading branch information
tzzed committed Feb 11, 2022
1 parent d8a0bff commit a521b49
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/genji/shell/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/cockroachdb/errors"

"github.com/genjidb/genji"
"github.com/genjidb/genji/cmd/genji/dbutil"
"github.com/genjidb/genji/cmd/genji/doc"
Expand Down Expand Up @@ -84,6 +85,12 @@ var commands = []command{
DisplayName: ".import",
Description: "Import data from a file. Only supported type is 'csv'",
},
{
Name: ".timer",
Options: "[on|off]",
DisplayName: ".timer",
Description: "Display the execution time after each query or hide it.",
},
}

func getUsage(cmdName string) string {
Expand Down
39 changes: 37 additions & 2 deletions cmd/genji/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import (
"strings"
"sync"
"syscall"
"time"

"github.com/agnivade/levenshtein"
"github.com/c-bata/go-prompt"
"github.com/cockroachdb/errors"
"go.uber.org/multierr"
"golang.org/x/sync/errgroup"

"github.com/genjidb/genji"
"github.com/genjidb/genji/cmd/genji/dbutil"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/internal/sql/parser"
"github.com/genjidb/genji/types"
"go.uber.org/multierr"
"golang.org/x/sync/errgroup"
)

const (
Expand All @@ -45,6 +47,8 @@ type Shell struct {
livePrefix string
multiLine bool

showTime bool

history []string

cmdSuggestions []prompt.Suggest
Expand Down Expand Up @@ -173,6 +177,8 @@ func (sh *Shell) runExecutor(ctx context.Context, promptExecCh chan string) erro
case <-ctx.Done():
return ctx.Err()
case input := <-promptExecCh:
showTime := sh.showTime
start := time.Now().UTC()
err := sh.executeInput(sh.getExecContext(ctx), input)
// if the context has been canceled
// there is no way to tell at this point
Expand All @@ -189,12 +195,34 @@ func (sh *Shell) runExecutor(ctx context.Context, promptExecCh chan string) erro
}
if err != nil {
fmt.Println(err)
continue
}

// even if showtime is true, ensure it's a query, and it was executed.
if showTime && !strings.HasPrefix(input, ".") && strings.HasSuffix(input, ";") {
displayTime(start)
}
case promptExecCh <- "":
}
}
}

// displayTime shows execution time of a query.
func displayTime(start time.Time) {
unit := "s"
multiplier := 1.0
precision := 3
completed := time.Since(start)
// under a second print in ms unit.
if completed.Seconds() < 1 {
unit = "ms"
multiplier = 1000.
precision = 0
}

fmt.Printf("Time: %.*f%s\n", precision, completed.Seconds()*multiplier, unit)
}

// runPrompt is a stateless function that displays a prompt to the user.
// User input is sent to the execCh channel which must deal with parsing and error handling.
// Once the execution of the user input is done by the reader of the channel, it must
Expand Down Expand Up @@ -415,6 +443,13 @@ func (sh *Shell) runCommand(ctx context.Context, in string) error {
in = strings.TrimSuffix(in, ";")
cmd := strings.Fields(in)
switch cmd[0] {
case ".timer":
if len(cmd) != 2 || (cmd[1] != "on" && cmd[1] != "off") {
return fmt.Errorf(getUsage(".timer"))
}

sh.showTime = cmd[1] == "on"
return nil
case ".help", "help":
return runHelpCmd()
case ".tables":
Expand Down

0 comments on commit a521b49

Please sign in to comment.