Created
April 1, 2022 08:04
-
-
Save kazeburo/204efec4fab4a781f887ffa3d08a69c1 to your computer and use it in GitHub Desktop.
Go ltsv parser benchmark. see https://github.com/kazeburo/ltsvparser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
my $file = $ARGV[0] || "demo.log"; | |
my $i = $ARGV[1] || 100000; | |
my $log = join "\t", ( | |
"time:08/Mar/2017:14:12:40 +0900", | |
"status:200", | |
"reqtime:0.030", | |
"host:10.20.30.40", | |
"req:GET /example/path HTTP/1.1", | |
"method:GET", | |
"size:941", | |
"ua:Mozilla/5.0 (Linux; Android 4.4.2; SO-01F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.90 Mobile Safari/537.36", | |
); | |
open(my $fh, ">>", "demo.log") or die $!; | |
while ($i>0) { | |
print $fh "$log\n"; | |
$i--; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"os" | |
"strconv" | |
"testing" | |
goltsv "github.com/Songmu/go-ltsv" | |
"github.com/buger/jsonparser" | |
"github.com/kazeburo/ltsvparser" | |
"github.com/najeira/ltsv" | |
) | |
type log struct { | |
Status int64 | |
ReqTime float64 | |
} | |
func BenchmarkLtsv(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
f, _ := os.Open("demo.log") | |
lr := ltsv.NewReader(f) | |
for { | |
m, err := lr.Read() | |
if err != nil { | |
break | |
} | |
l := log{} | |
l.Status, _ = strconv.ParseInt(m["status"], 10, 64) | |
l.ReqTime, _ = strconv.ParseFloat(m["reqtime"], 64) | |
} | |
} | |
} | |
func BenchmarkGoLtsv(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
f, _ := os.Open("demo.log") | |
bs := bufio.NewScanner(f) | |
for bs.Scan() { | |
l := log{} | |
goltsv.Unmarshal(bs.Bytes(), &l) | |
} | |
} | |
} | |
var status = []byte("status") | |
var reqtime = []byte("reqtime") | |
func BenchmarkLtsvParser(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
f, _ := os.Open("demo.log") | |
bs := bufio.NewScanner(f) | |
for bs.Scan() { | |
l := log{} | |
err := ltsvparser.Each( | |
bs.Bytes(), | |
func(idx int, v []byte) error { | |
switch idx { | |
case 0: | |
// status | |
l.Status, _ = jsonparser.ParseInt(v) | |
case 1: | |
// reqtime | |
l.ReqTime, _ = jsonparser.ParseFloat(v) | |
return ltsvparser.Cancel | |
} | |
return nil | |
}, | |
status, | |
reqtime, | |
) | |
if err != nil { | |
panic(err) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment