Giter Site home page Giter Site logo

Comments (7)

hjr265 avatar hjr265 commented on May 7, 2024

@kisielk I think it's possible to avoid the use of fmt altogether. By using io.WriteString and strconv.Itoa. Since the format verbs are either %s or %d. The buildCommonLogLine could then take a writer and write to it directly, instead of returning a string.

Although, if using fmt once per request is really okay, then yeah.. It is possible by moving a few things around may be.

from handlers.

hjr265 avatar hjr265 commented on May 7, 2024

I can make this change and send you a pull request if that's okay..

from handlers.

kisielk avatar kisielk commented on May 7, 2024

Go for it

On Tuesday, May 27, 2014, Mahmud Ridwan [email protected] wrote:

I can make this change and send you a pull request if that's okay..


Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-44257743
.

-K

from handlers.

kisielk avatar kisielk commented on May 7, 2024

However, please try benchmarking it to make sure the modified version is actually faster and does fewer allocations than the fmt version.

from handlers.

hjr265 avatar hjr265 commented on May 7, 2024

I wrote a simple benchmark func

func BenchmarkWriteLog(b *testing.B) {
    loc, err := time.LoadLocation("Europe/Warsaw")
    if err != nil {
        b.Fatalf(err.Error())
    }
    ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)

    req := newRequest("GET", "http://example.com")
    req.RemoteAddr = "192.168.100.5"

    b.ResetTimer()

    buf := &bytes.Buffer{}
    for i := 0; i < b.N; i++ {
        buf.Reset()
        writeLog(buf, req, ts, http.StatusUnauthorized, 500)
    }
}

For current implementation, I get this:

BenchmarkWriteLog     500000          3351 ns/op

For the modified implementation (with no fmt at all), I get this:

BenchmarkWriteLog    1000000          1394 ns/op

from handlers.

kisielk avatar kisielk commented on May 7, 2024

That certainly looks promising. How about memory usage? There's a benchmark
flag to measure allocations as well. I expect it will be lower too.

On Wednesday, May 28, 2014, Mahmud Ridwan [email protected] wrote:

I wrote a simple benchmark function

func BenchmarkWriteLog(b *testing.B) {
loc, err := time.LoadLocation("Europe/Warsaw")
if err != nil {
b.Fatalf(err.Error())
}
ts := time.Date(1983, 05, 26, 3, 30, 45, 0, loc)

req := newRequest("GET", "http://example.com")
req.RemoteAddr = "192.168.100.5"

buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
    buf.Reset()
    writeLog(buf, req, ts, http.StatusUnauthorized, 500)
}}

For current implementation, I get this:

BenchmarkWriteLog 500000 3351 ns/op

For the modified implementation (with no fmt at all), I get this:

BenchmarkWriteLog 1000000 1394 ns/op


Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-44383295
.

-K

from handlers.

hjr265 avatar hjr265 commented on May 7, 2024

Ran $ go test -bench . -benchmem

For current implementation:

BenchmarkWriteLog     500000          3287 ns/op         338 B/op         11 allocs/op

For the modified implementation (#13; with no fmt at all):

BenchmarkWriteLog    1000000          1412 ns/op         162 B/op          5 allocs/op

from handlers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.