Skip to content

Commit

Permalink
lightweight stats
Browse files Browse the repository at this point in the history
  • Loading branch information
davinci26 committed Sep 10, 2018
1 parent ba5b49a commit 27a1aac
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 191 deletions.
9 changes: 2 additions & 7 deletions commands/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ $ iptb auto -count 5 -type <type>
Name: "start",
Usage: "starts nodes immediately",
},
cli.BoolFlag{
Name: "stats",
Usage: "Output statistics on the command execution",
},
},
Action: func(c *cli.Context) error {
flagRoot := c.GlobalString("IPTB_ROOT")
Expand All @@ -58,7 +54,6 @@ $ iptb auto -count 5 -type <type>
flagStart := c.Bool("start")
flagCount := c.Int("count")
flagForce := c.Bool("force")
flagStats := c.Bool("stats")

tb := testbed.NewTestbed(path.Join(flagRoot, "testbeds", flagTestbed))
if err := testbed.AlreadyInitCheck(tb.Dir(), flagForce); err != nil {
Expand Down Expand Up @@ -93,7 +88,7 @@ $ iptb auto -count 5 -type <type>
return err
}

if err := buildReport(results, "Initialize Nodes", flagStats); err != nil {
if err := buildReport(results, "text"); err != nil {
return err
}

Expand All @@ -107,7 +102,7 @@ $ iptb auto -count 5 -type <type>
return err
}

if err := buildReport(results, "Start nodes", flagStats); err != nil {
if err := buildReport(results, "text"); err != nil {
return err
}
}
Expand Down
110 changes: 4 additions & 106 deletions commands/connect.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package commands

import (
"bufio"
"context"
"fmt"
"os"
"path"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand All @@ -16,7 +12,6 @@ import (
"github.com/ipfs/iptb/testbed"
)

// TODO:Add explanation in the description for topology flag
var ConnectCmd = cli.Command{
Category: "CORE",
Name: "connect",
Expand All @@ -28,17 +23,12 @@ The connect command allows for connecting sets of nodes together.
Every node listed in the first set, will try to connect to every node
listed in the second set.
There are four variants of the command. It can accept no arugments,
a single argument, two arguments or a file. The no argument and single argument
expands out to the two argument usage. The file argument, parses the file and exapnds it
to the two argument usage. The file should have the following format:
- Node Origin:Connection 1 Connection 2 .... Connection n e.g. 0:1,2,3,4
- Lines starting with # are ignored (comment line), empty lines are also ignored
There are three variants of the command. It can accept no arugments,
a single argument, two arguments. The no argument and single argument
expands out to the two argument usage
$ iptb connect => iptb connect [0-C] [0-C]
$ iptb connect [n-m] => iptb connect [n-m] [n-m]
$ iptb connect [n-m] [i-k]
$ iptb connect -topology /directory/topology.txt
Sets of nodes can be expressed in the following ways
Expand All @@ -56,44 +46,18 @@ INPUT EXPANDED
Usage: "timeout on the command",
Value: "30s",
},
cli.StringFlag{
Name: "topology",
Usage: "specify a network topology file",
},
},
Action: func(c *cli.Context) error {
flagRoot := c.GlobalString("IPTB_ROOT")
flagTestbed := c.GlobalString("testbed")
flagTimeout := c.String("timeout")
flagTopology := c.String("topology")

timeout, err := time.ParseDuration(flagTimeout)
if err != nil {
return err
}
tb := testbed.NewTestbed(path.Join(flagRoot, "testbeds", flagTestbed))

// Case Topoloogy is specified
if len(flagTopology) != 0 {
nodes, err := tb.Nodes()
if err != nil {
return err
}
topologyGraph, err := parseTopology(flagTopology, len(nodes))
if err != nil {
return err
}
for _, connectionRow := range topologyGraph {
from := connectionRow[0]
to := connectionRow[1:]
err = connectNodes(tb, []int{from}, to, timeout)
if err != nil {
return err
}
}
return nil
}

// Case range is specified
args := c.Args()
switch c.NArg() {
Expand Down Expand Up @@ -156,71 +120,5 @@ func connectNodes(tb testbed.BasicTestbed, from, to []int, timeout time.Duration
}
}

return buildReport(results, "", false)
}

func parseTopology(fileDir string, numberOfNodes int) ([][]int, error) {

// Scan Input file Line by Line //
inFile, err := os.Open(fileDir)
defer inFile.Close()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)

// Store number of line to produce meaningful errors
lineNumber := 1
// Topology graph implemented as an Adjacency Matrix DS
// This intermediate variable it could be terminated to increase peformance
// This would decrease code readability.
var topology [][]int

for scanner.Scan() {
var destinations []string
var lineTokenized []string
line := scanner.Text()
// Check if the line is a comment or empty and skip it//
if len(line) == 0 || line[0] == '#' {
lineNumber++
continue
} else {
lineTokenized = strings.Split(line, ":")
// Check if the format is correct
if len(lineTokenized) == 1 {
return nil, errors.New("Line " + strconv.Itoa(lineNumber) + " does not follow the correct format")
}
destinations = strings.Split(lineTokenized[1], ",")
}
// Declare the topology in that line, the first element is the origin
var topologyLine []int
// Parse origin in the line
origin, err := strconv.Atoi(lineTokenized[0])
// Check if it can be casted to integer
if err != nil {
return nil, errors.New("Line " + strconv.Itoa(lineNumber) + " of connection graph, could not be parsed")
}
// Check if the node is out of range
if origin >= numberOfNodes {
return nil, errors.New("Origin node in line " + strconv.Itoa(lineNumber) + " out of range")
}
topologyLine = append(topologyLine, origin)
for _, destination := range destinations {
// Check if it can be casted to integer
target, err := strconv.Atoi(destination)
if err != nil {
return nil, errors.New("Check line " + strconv.Itoa(lineNumber) + " of connection graph, could not be parsed")
}
// Check if the node is out of range
if target >= numberOfNodes {
return nil, errors.New("Target node in line " + strconv.Itoa(lineNumber) + " out of range")
}
// Append destination to graph
topologyLine = append(topologyLine, target)
}
lineNumber++
topology = append(topology, topologyLine)
}
return topology, nil
return buildReport(results, "text")
}
7 changes: 1 addition & 6 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ var InitCmd = cli.Command{
Name: "terminator",
Hidden: true,
},
cli.BoolFlag{
Name: "stats",
Usage: "Output statistics on the command execution",
},
},
Before: func(c *cli.Context) error {
if present := isTerminatorPresent(c); present {
Expand All @@ -36,7 +32,6 @@ var InitCmd = cli.Command{
Action: func(c *cli.Context) error {
flagRoot := c.GlobalString("IPTB_ROOT")
flagTestbed := c.GlobalString("testbed")
flagStats := c.Bool("stats")

tb := testbed.NewTestbed(path.Join(flagRoot, "testbeds", flagTestbed))
nodes, err := tb.Nodes()
Expand Down Expand Up @@ -64,6 +59,6 @@ var InitCmd = cli.Command{
return err
}

return buildReport(results, "Initialize Nodes", flagStats)
return buildReport(results, "text")
},
}
2 changes: 1 addition & 1 deletion commands/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var LogsCmd = cli.Command{
return err
}

return buildReport(results, "", false)
return buildReport(results, "text")
},
}

Expand Down
2 changes: 1 addition & 1 deletion commands/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ var RestartCmd = cli.Command{
return err
}

return buildReport(results, "", false)
return buildReport(results, "text")
},
}
22 changes: 17 additions & 5 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ var RunCmd = cli.Command{
Name: "terminator",
Hidden: true,
},
cli.BoolFlag{
Name: "stats",
Usage: "Output statistics on the command execution",
cli.StringFlag{
Name: "encoding",
Usage: "Specify the output format, current options JSON and text",
Value: "text",
},
},
Before: func(c *cli.Context) error {
Expand All @@ -37,8 +38,19 @@ var RunCmd = cli.Command{
Action: func(c *cli.Context) error {
flagRoot := c.GlobalString("IPTB_ROOT")
flagTestbed := c.GlobalString("testbed")
flagStats := c.Bool("stats")
flagFormat := c.String("encoding")
// Compare everything to lower to make it case insentive
flagFormatLwr := strings.ToLower(flagFormat)

// Parse output format
switch flagFormatLwr {
case "text":
// input is correct
case "json":
// input is correct
default:
NewUsageError("the output encoding provided is not parsable")
}
tb := testbed.NewTestbed(path.Join(flagRoot, "testbeds", flagTestbed))
nodes, err := tb.Nodes()
if err != nil {
Expand All @@ -65,6 +77,6 @@ var RunCmd = cli.Command{
return err
}

return buildReport(results, strings.Join(args[:], " "), flagStats)
return buildReport(results, flagFormatLwr)
},
}
2 changes: 1 addition & 1 deletion commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ var StartCmd = cli.Command{
return err
}

return buildReport(results, "", false)
return buildReport(results, "text")
},
}
2 changes: 1 addition & 1 deletion commands/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ var StopCmd = cli.Command{
return err
}

return buildReport(results, "", false)
return buildReport(results, "text")
},
}
Loading

0 comments on commit 27a1aac

Please sign in to comment.