1 files changed, 25 insertions(+), 30 deletions(-)

M main.go
M main.go +25 -30
@@ 51,7 51,6 @@ func (c *Client) connect() selenium.WebD
 				logger.Println("Error connecting to", c.URL, err)
 				time.Sleep(30 * time.Second)
 			} else {
-				logger.Println("Connected to", c.URL)
 				c.startTime = time.Now()
 				break
 			}

          
@@ 62,7 61,8 @@ func (c *Client) connect() selenium.WebD
 }
 
 //
-// Download url re-using the previous Selenium session if count < 100
+// Download url re-using the previous Selenium session if it ran for less than
+// 10 minutes.
 //
 func (c *Client) Get(url string) (duration time.Duration, err error) {
 	// Re-connect every 10 minutes

          
@@ 132,39 132,18 @@ func ScanLines(input io.Reader) <-chan s
 }
 
 //
-// All the element sent to the returned channel will be written to `output`
-//
-func CSVWriter(output io.Writer, workers int) chan<- Result {
-	var c = make(chan Result, workers)
-
-	go func() {
-		w := csv.NewWriter(output)
-
-		for o := range c {
-			var e = ""
-			if o.Error != nil {
-				logger.Println("Error for", o.URL, ":", o.Error)
-				e = o.Error.Error()
-			}
-			w.Write([]string{o.URL, e, o.Duration.String()})
-			w.Flush()
-		}
-	}()
-
-	return c
-}
-
-//
 // Exits program if there's any error parsing the arguments
 //
 func parse_arguments() (
 	url string,
 	capabilities selenium.Capabilities,
 	workers int,
+	progress bool,
 ) {
 	var args struct {
 		Workers      uint              `short:"w" default:"4" description:"Number of workers"`
 		Capabilities map[string]string `short:"c" default:"browserName:firefox" description:"Selenium capabilities"`
+		Progress     bool              `short:"p" description:"Display progress on stderr"`
 		Arg          struct {
 			URL string `description:"URL to the Selenium server"`
 		} `positional-args:"yes" required:"yes"`

          
@@ 183,6 162,7 @@ func parse_arguments() (
 
 	url = args.Arg.URL
 	workers = int(args.Workers)
+	progress = args.Progress
 	capabilities = make(selenium.Capabilities, len(args.Capabilities))
 	for key, value := range args.Capabilities {
 		capabilities[key] = value

          
@@ 192,20 172,26 @@ func parse_arguments() (
 }
 
 func main() {
-	var url, capabilities, workers = parse_arguments()
+	var url, capabilities, workers, progress = parse_arguments()
 
 	var input = ScanLines(os.Stdin)
 	// Buffer results to avoid blocking workers
-	var output = make(chan Result, workers)
+	var output = make(chan Result, 10)
 	var wg sync.WaitGroup
 
 	logger.Println("Using", workers, "workers with", url)
 
-	wg.Add(int(workers))
-	for i := 0; i < int(workers); i++ {
+	wg.Add(workers)
+	for i := 0; i < workers; i++ {
 		go worker(capabilities, url, &wg, input, output)
 	}
 
+	// Wait for all worker to finish, then close output to end the program
+	go func() {
+		wg.Wait()
+		close(output)
+	}()
+
 	var writer = csv.NewWriter(os.Stdout)
 
 	for o := range output {

          
@@ 214,9 200,18 @@ func main() {
 			logger.Println("Error for", o.URL, ":", o.Error)
 			e = o.Error.Error()
 		}
+		if progress {
+			if e == "" {
+				os.Stderr.Write([]byte{'.'})
+			} else {
+				os.Stderr.Write([]byte{'x'})
+			}
+		}
 		writer.Write([]string{o.URL, e, o.Duration.String()})
 		writer.Flush()
 	}
 
-	wg.Wait()
+	if progress {
+		os.Stderr.Write([]byte{'\n'})
+	}
 }