Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample
45 changes: 45 additions & 0 deletions sample/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"time"
)

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)
}

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)
}

out := bufio.NewWriterSize(os.Stdout, 1024*512)

rand.Seed(time.Now().UnixNano())
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
total += 1
if target < rand.Float64() {
continue
}
emitted += 1
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)
}