From eb0e11c6611253038f235fb0376410895de8f249 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Thu, 17 Jul 2014 08:13:47 -0400 Subject: [PATCH 1/3] convert sample to go --- sample/.gitignore | 1 + sample/main.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 sample/.gitignore create mode 100644 sample/main.go diff --git a/sample/.gitignore b/sample/.gitignore new file mode 100644 index 0000000..d64a3d9 --- /dev/null +++ b/sample/.gitignore @@ -0,0 +1 @@ +sample diff --git a/sample/main.go b/sample/main.go new file mode 100644 index 0000000..86b6043 --- /dev/null +++ b/sample/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "bufio" + "fmt" + "math/rand" + "os" + "strconv" + "time" +) + +func main() { + + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)") + os.Exit(1) + } + + target, err := strconv.ParseFloat(os.Args[1], 64) + if err != nil || target < 0.0 || target > 1.0 { + fmt.Fprintf(os.Stderr, "Unable to convert %q to a float between 0.0 and 1.0", os.Args[1]) + os.Exit(1) + } + + rand.Seed(time.Now().UnixNano()) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + if target < rand.Float64() { + continue + } + fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n' + } + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "reading standard input:", err) + os.Exit(2) + } +} From 12a0f28b2b1393a4c3012d8fca4a9f9a26882715 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Thu, 17 Jul 2014 08:15:34 -0400 Subject: [PATCH 2/3] count output --- sample/main.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/sample/main.go b/sample/main.go index 86b6043..d9f2b68 100644 --- a/sample/main.go +++ b/sample/main.go @@ -10,7 +10,8 @@ import ( ) func main() { - + var total int64 + var emitted int64 if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "invalid argument. use a fraction to sample between 0.0 (no sampling) and 1.0 (100% sampling)") os.Exit(1) @@ -25,13 +26,17 @@ func main() { rand.Seed(time.Now().UnixNano()) scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { + total += 1 if target < rand.Float64() { continue } + emitted += 1 fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n' } if err := scanner.Err(); err != nil { - fmt.Fprintln(os.Stderr, "reading standard input:", err) + fmt.Fprintln(os.Stderr, "Error reading standard input:", err) os.Exit(2) } + + fmt.Fprintf(os.Stderr, "Total of %d lines. Sampled to %d\n", total, emitted) } From 7a468a9b743361595df58c31d6dc11736145f2e9 Mon Sep 17 00:00:00 2001 From: Jehiah Czebotar Date: Thu, 17 Jul 2014 21:44:58 -0400 Subject: [PATCH 3/3] buffer and fix formatting --- sample/main.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sample/main.go b/sample/main.go index d9f2b68..069c499 100644 --- a/sample/main.go +++ b/sample/main.go @@ -23,6 +23,8 @@ func main() { os.Exit(1) } + out := bufio.NewWriterSize(os.Stdout, 1024*512) + rand.Seed(time.Now().UnixNano()) scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { @@ -31,12 +33,13 @@ func main() { continue } emitted += 1 - fmt.Printf("%q", scanner.Text()) // Println will add back the final '\n' + out.WriteString(scanner.Text()) + out.WriteString("\n") } if err := scanner.Err(); err != nil { fmt.Fprintln(os.Stderr, "Error reading standard input:", err) os.Exit(2) } - + out.Flush() fmt.Fprintf(os.Stderr, "Total of %d lines. Sampled to %d\n", total, emitted) }