remove pilosactl and add version/build to root cmd

This commit is contained in:
Matt Jaffee 2017-03-03 15:24:39 -06:00
parent 64f007547e
commit a47f93329e
3 changed files with 20 additions and 260 deletions

View file

@ -1,258 +0,0 @@
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"strings"
"time"
"github.com/pilosa/pilosa"
)
var (
// ErrUnknownCommand is returned when specifying an unknown command.
ErrUnknownCommand = errors.New("unknown command")
// ErrPathRequired is returned when executing a command without a required path.
ErrPathRequired = errors.New("path required")
Version string
BuildTime string
)
func init() {
if Version == "" {
Version = "v0.0.0"
}
if BuildTime == "" {
BuildTime = "not recorded"
}
}
func main() {
m := NewMain()
fmt.Fprintf(m.Stderr, "Pilosactl %s, build time %s\n", Version, BuildTime)
// Parse command line arguments.
if err := m.ParseFlags(os.Args[1:]); err == flag.ErrHelp {
os.Exit(2)
} else if err != nil {
fmt.Fprintln(m.Stderr, err)
os.Exit(2)
}
// Execute the program.
if err := m.Run(); err != nil {
fmt.Fprintln(m.Stderr, err)
os.Exit(1)
}
}
// Main represents the main program execution.
type Main struct {
// Subcommand to execute.
Cmd Command
// Standard input/output
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewMain returns a new instance of Main.
func NewMain() *Main {
return &Main{
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// Usage returns the usage message to be printed.
func (m *Main) Usage() string {
return strings.TrimSpace(`
Pilosactl is a tool for interacting with a pilosa server.
Usage:
pilosactl command [arguments]
The commands are:
Use the "-h" flag with any command for more information.
`)
}
// Run executes the main program execution.
func (m *Main) Run() error { return m.Cmd.Run(context.Background()) }
// ParseFlags parses command line flags from args.
func (m *Main) ParseFlags(args []string) error {
var command string
if len(args) > 0 {
command = args[0]
args = args[1:]
}
switch command {
case "", "help", "-h":
fmt.Fprintln(m.Stderr, m.Usage())
fmt.Fprintln(m.Stderr, "")
return flag.ErrHelp
default:
return ErrUnknownCommand
}
// Parse command's flags.
if err := m.Cmd.ParseFlags(args); err == flag.ErrHelp {
fmt.Fprintln(m.Stderr, m.Cmd.Usage())
fmt.Fprintln(m.Stderr, "")
return err
} else if err != nil {
return err
}
return nil
}
// Command represents an executable subcommand.
type Command interface {
Usage() string
ParseFlags(args []string) error
Run(context.Context) error
}
// BenchCommand represents a command for benchmarking database operations.
type BenchCommand struct {
// Destination host and port.
Host string
// Name of the database & frame to execute against.
Database string
Frame string
// Type of operation and number to execute.
Op string
N int
// Standard input/output
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewBenchCommand returns a new instance of BenchCommand.
func NewBenchCommand(stdin io.Reader, stdout, stderr io.Writer) *BenchCommand {
return &BenchCommand{
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
}
}
// ParseFlags parses command line flags from args.
func (cmd *BenchCommand) ParseFlags(args []string) error {
fs := flag.NewFlagSet("pilosactl", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
fs.StringVar(&cmd.Host, "host", "localhost:15000", "host:port")
fs.StringVar(&cmd.Database, "d", "", "database")
fs.StringVar(&cmd.Frame, "f", "", "frame")
fs.StringVar(&cmd.Op, "op", "", "operation")
fs.IntVar(&cmd.N, "n", 0, "op count")
if err := fs.Parse(args); err != nil {
return err
}
return nil
}
// Usage returns the usage message to be printed.
func (cmd *BenchCommand) Usage() string {
return strings.TrimSpace(`
usage: pilosactl bench [args]
Executes a benchmark for a given operation against the database.
The following flags are allowed:
-host HOSTPORT
hostname and port of running pilosa server
-d DATABASE
database to execute operation against
-f FRAME
frame to execute operation against
-op OP
name of operation to execute
-n COUNT
number of iterations to execute
The following operations are available:
set-bit
Sets a single random bit on the frame
`)
}
// Run executes the main program execution.
func (cmd *BenchCommand) Run(ctx context.Context) error {
// Create a client to the server.
client, err := pilosa.NewClient(cmd.Host)
if err != nil {
return err
}
switch cmd.Op {
case "set-bit":
return cmd.runSetBit(ctx, client)
case "":
return errors.New("op required")
default:
return fmt.Errorf("unknown bench op: %q", cmd.Op)
}
}
// runSetBit executes a benchmark of random SetBit() operations.
func (cmd *BenchCommand) runSetBit(ctx context.Context, client *pilosa.Client) error {
if cmd.N == 0 {
return errors.New("operation count required")
} else if cmd.Database == "" {
return pilosa.ErrDatabaseRequired
} else if cmd.Frame == "" {
return pilosa.ErrFrameRequired
}
const maxBitmapID = 1000
const maxProfileID = 100000
startTime := time.Now()
// Execute operation continuously.
for i := 0; i < cmd.N; i++ {
bitmapID := rand.Intn(maxBitmapID)
profileID := rand.Intn(maxProfileID)
q := fmt.Sprintf(`SetBit(id=%d, frame="%s", profileID=%d)`, bitmapID, cmd.Frame, profileID)
if _, err := client.ExecuteQuery(ctx, cmd.Database, q, true); err != nil {
return err
}
}
// Print results.
elapsed := time.Since(startTime)
fmt.Fprintf(cmd.Stdout, "Executed %d operations in %s (%0.3f op/sec)\n", cmd.N, elapsed, float64(cmd.N)/elapsed.Seconds())
return nil
}

View file

@ -1 +0,0 @@
package main_test

View file

@ -2,13 +2,32 @@ package cmd
import "github.com/spf13/cobra"
var (
Version string
BuildTime string
)
var RootCmd = &cobra.Command{
Use: "pilosa",
Short: "pilosa - A Distributed In-memory Binary Bitmap Index",
// TODO - is documentation actually there?
Long: `Pilosa is a fast index to turbocharge your database.
This binary contains Pilosa itself, as well as common
tools for administering pilosa, importing/exporting data,
backing up, and more. Complete documentation is available
at http://pilosa.com/docs`, // TODO - is documentation actually there?
at http://pilosa.com/docs
`,
}
func init() {
if Version == "" {
Version = "v0.0.0"
}
if BuildTime == "" {
BuildTime = "not recorded"
}
RootCmd.Long = RootCmd.Long + "Version: " + Version + "\nBuild Time: " + BuildTime + "\n"
}