Skip to content

Commit

Permalink
HW10 is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas authored and DimVlas committed Dec 25, 2024
1 parent 4454c88 commit a829e48
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 37 deletions.
Empty file removed hw10_program_optimization/.sync
Empty file.
16 changes: 16 additions & 0 deletions hw10_program_optimization/bench_new.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
goos: linux
goarch: amd64
pkg: github.com/DimVlas/otus_hw/hw10_program_optimization
cpu: AMD Ryzen 5 3500X 6-Core Processor
BenchmarkGetDomainStat-6 6 180000872 ns/op 29449422 B/op 723161 allocs/op
BenchmarkGetDomainStat-6 6 179475854 ns/op 29449080 B/op 723159 allocs/op
BenchmarkGetDomainStat-6 6 179211242 ns/op 29449562 B/op 723161 allocs/op
BenchmarkGetDomainStat-6 6 179568154 ns/op 29449693 B/op 723162 allocs/op
BenchmarkGetDomainStat-6 6 183618851 ns/op 29449421 B/op 723160 allocs/op
BenchmarkGetDomainStat-6 6 186144564 ns/op 29449717 B/op 723163 allocs/op
BenchmarkGetDomainStat-6 6 179541616 ns/op 29449549 B/op 723161 allocs/op
BenchmarkGetDomainStat-6 6 179762574 ns/op 29449098 B/op 723159 allocs/op
BenchmarkGetDomainStat-6 6 179707295 ns/op 29449813 B/op 723162 allocs/op
BenchmarkGetDomainStat-6 6 179366661 ns/op 29449816 B/op 723162 allocs/op
PASS
ok github.com/DimVlas/otus_hw/hw10_program_optimization 12.673s
16 changes: 16 additions & 0 deletions hw10_program_optimization/bench_old.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
goos: linux
goarch: amd64
pkg: github.com/DimVlas/otus_hw/hw10_program_optimization
cpu: AMD Ryzen 5 3500X 6-Core Processor
BenchmarkGetDomainStat-6 2 552224837 ns/op 316920320 B/op 2845406 allocs/op
BenchmarkGetDomainStat-6 2 554340655 ns/op 316939656 B/op 2845410 allocs/op
BenchmarkGetDomainStat-6 2 548643641 ns/op 316883512 B/op 2845403 allocs/op
BenchmarkGetDomainStat-6 2 541981412 ns/op 316864984 B/op 2845401 allocs/op
BenchmarkGetDomainStat-6 2 551823022 ns/op 316993964 B/op 2845409 allocs/op
BenchmarkGetDomainStat-6 2 556719799 ns/op 316884424 B/op 2845408 allocs/op
BenchmarkGetDomainStat-6 2 543636506 ns/op 316920640 B/op 2845407 allocs/op
BenchmarkGetDomainStat-6 2 553364894 ns/op 316957360 B/op 2845408 allocs/op
BenchmarkGetDomainStat-6 2 550641171 ns/op 316939488 B/op 2845408 allocs/op
BenchmarkGetDomainStat-6 2 555527000 ns/op 316957552 B/op 2845409 allocs/op
PASS
ok github.com/DimVlas/otus_hw/hw10_program_optimization 16.542s
15 changes: 15 additions & 0 deletions hw10_program_optimization/bench_stat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
goos: linux
goarch: amd64
pkg: github.com/DimVlas/otus_hw/hw10_program_optimization
cpu: AMD Ryzen 5 3500X 6-Core Processor
│ bench_old.txt │ bench_new.txt │
│ sec/op │ sec/op vs base │
GetDomainStat-6 552.0m ± 2% 179.6m ± 2% -67.46% (p=0.000 n=10)

│ bench_old.txt │ bench_new.txt │
│ B/op │ B/op vs base │
GetDomainStat-6 302.25Mi ± 0% 28.09Mi ± 0% -90.71% (p=0.000 n=10)

│ bench_old.txt │ bench_new.txt │
│ allocs/op │ allocs/op vs base │
GetDomainStat-6 2845.4k ± 0% 723.2k ± 0% -74.58% (p=0.000 n=10)
10 changes: 8 additions & 2 deletions hw10_program_optimization/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module github.com/fixme_my_friend/hw10_program_optimization
module github.com/DimVlas/otus_hw/hw10_program_optimization

go 1.22

require github.com/stretchr/testify v1.7.0
require (
github.com/mailru/easyjson v0.9.0
github.com/stretchr/testify v1.7.0
)

require (
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/perf v0.0.0-20241204221936-711ff2ab7231 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
8 changes: 8 additions & 0 deletions hw10_program_optimization/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 h1:xlwdaKcTNVW4PtpQb8aKA4Pjy0CdJHEqvFbAnvR5m2g=
github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794/go.mod h1:7e+I0LQFUI9AXWxOfsQROs9xPhoJtbsyWcjJqDd4KPY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/perf v0.0.0-20241204221936-711ff2ab7231 h1:LrlW0UtZ/i5iwthL3HvtiFwcGwgtopumPLfOwdutbzQ=
golang.org/x/perf v0.0.0-20241204221936-711ff2ab7231/go.mod h1:Ha9gd2gpRTtN8P+nXRQCFfD8JnfyIrp8vL362lG7AnU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
63 changes: 28 additions & 35 deletions hw10_program_optimization/stats.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package hw10programoptimization

import (
"encoding/json"
"fmt"
"bufio"
"io"
"regexp"
"strings"

easyjson "github.com/mailru/easyjson"
)

type User struct {
Expand All @@ -21,46 +21,39 @@ type User struct {
type DomainStat map[string]int

func GetDomainStat(r io.Reader, domain string) (DomainStat, error) {
u, err := getUsers(r)
if err != nil {
return nil, fmt.Errorf("get users error: %w", err)
}
return countDomains(u, domain)
return domainsStat(r, domain)
}

type users [100_000]User

func getUsers(r io.Reader) (result users, err error) {
content, err := io.ReadAll(r)
if err != nil {
return
}
func domainsStat(r io.Reader, domain string) (DomainStat, error) {
rd := bufio.NewReader(r)
var br bool
var user User
stat := make(DomainStat)

lines := strings.Split(string(content), "\n")
for i, line := range lines {
var user User
if err = json.Unmarshal([]byte(line), &user); err != nil {
return
for {
line, err := rd.ReadBytes('\n')
if err != nil {
switch err {
case io.EOF:
br = true
default:
return nil, err
}
}
result[i] = user
}
return
}

func countDomains(u users, domain string) (DomainStat, error) {
result := make(DomainStat)

for _, user := range u {
matched, err := regexp.Match("\\."+domain, []byte(user.Email))
if err != nil {
if err = easyjson.Unmarshal(line, &user); err != nil {
return nil, err
}

if matched {
num := result[strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])]
num++
result[strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])] = num
if strings.HasSuffix(user.Email, "."+domain) {
d := strings.ToLower(strings.SplitN(user.Email, "@", 2)[1])
stat[d]++
}

if br {
break
}
}
return result, nil

return stat, nil
}
127 changes: 127 additions & 0 deletions hw10_program_optimization/stats_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions hw10_program_optimization/stats_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
//go:build !bench
// +build !bench

package hw10programoptimization

import (
"archive/zip"
"bytes"
"log"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -37,3 +40,28 @@ func TestGetDomainStat(t *testing.T) {
require.Equal(t, DomainStat{}, result)
})
}

func BenchmarkGetDomainStat(b *testing.B) {
z, err := zip.OpenReader("testdata/users.dat.zip")
if err != nil {
log.Println(err)
return
}
defer z.Close()

b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
b.StopTimer()
r, err := z.File[0].Open()
if err != nil {
log.Println(err)
return
}
defer r.Close()

b.StartTimer()
_, _ = GetDomainStat(r, "biz")
}()
}
}

0 comments on commit a829e48

Please sign in to comment.