Git Product home page Git Product logo

Comments (7)

garyburd avatar garyburd commented on September 27, 2024

Make it easy for me. Send a pointer to the Redis related code and whatever "raw" is.

from redigo.

Icedroid avatar Icedroid commented on September 27, 2024

op code:

package models

import (
    "errors"
    "strconv"
    "time"
    "log"
    "github.com/garyburd/redigo/redis"
)

var (
    Objects map[string]*Object

    RedisPool      *redis.Pool
    server         = "127.0.0.1:6379"
    objectHashName = "object"
)

type Object struct {
    ObjectId   string
    Score      int64
    PlayerName string
}

func init() {
    Objects = make(map[string]*Object)
    Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
    Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}

    RedisPool = &redis.Pool{
        MaxIdle:     5,
        MaxActive:   10000,
        IdleTimeout: time.Duration(5) * time.Second,
        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            _, err := c.Do("PING")
            return err
        },
        Dial: func() (redis.Conn, error) {
            c, err := redis.Dial("tcp", server)
            if err != nil {
                log.Printf("Dail redis server %s %v", server, err)
                return nil, err
            }
            if _, err := c.Do("PING"); err != nil {
                c.Close()
                return nil, err
            }
            return c, err
        },
    }
}

type Object struct {
    ObjectId   string
    Score      int64
    PlayerName string
}

//this the raw
func GetAll() map[string]*Object {
    return Objects
}

//get data from redis hash list
func GetObject() (object *Object, err error) {
    conn := GetRedis()
    defer conn.Close()
    object = new(Object)
    reply, err := redis.Values(conn.Do("HGETALL", objectHashName))
    if nil != err {
        log.Printf("set object to redis hash get error %s", err)
        return
    }
    err = redis.ScanStruct(reply, object)
    return
}

func GetRedis() redis.Conn {
    //  c, err := redis.Dial("tcp", server)
    //  if err != nil {
    //      log.Printf("Dail master redis server %s %v", server, err)
    //      return nil
    //  }
    //  return c
    return RedisPool.Get()
}

beego original method will run models.GetAll()

$ wrk -t12 -c400 -d30s http://127.0.0.1:8081/object 
Running 30s test @ http://127.0.0.1:8081/object
  12 threads and 400 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.39s   375.09ms   1.65s    92.90%
    Req/Sec     1.35k     2.56k   14.22k    88.50%
  479750 requests in 30.01s, 161.05MB read
Requests/sec:  15987.51
Transfer/sec:      5.37MB

get data from redis hash list use redigo will run models.GetObject()

$ wrk -t12 -c400 -d30s http://127.0.0.1:8081/get
Running 30s test @ http://127.0.0.1:8081/get
12 threads and 400 connections
Thread Stats   Avg      Stdev     Max   +/- Stdev
  Latency    87.87ms   88.84ms 740.17ms   93.32%
  Req/Sec   417.59    129.96     1.55k    80.07%
149451 requests in 30.00s, 33.35MB read
Requests/sec:   4981.43
Transfer/sec:      1.11MB

from redigo.

garyburd avatar garyburd commented on September 27, 2024

Because GetAll and GetObject do different things as evidenced by the return values, I don't understand what it is you are trying to compare.

GetAll is expected to be faster GetObject. GetAll simply returns a value. The GetAll function is probably inlined. GetObject fetches a value from the Redis server running in another process.

Any Redis client will be slow compared to a simple inlined function. The use of any database server over the network will be slow compared to a simple inlined function.

from redigo.

Icedroid avatar Icedroid commented on September 27, 2024

But Is decrease too much, only have Requests/sec: 4981.43. Use redis GET command the same thought output. So small throughput I can't run redigo code in my product machine.

from redigo.

garyburd avatar garyburd commented on September 27, 2024

Removing the PING will improve performance.

from redigo.

Icedroid avatar Icedroid commented on September 27, 2024

You mean remove PING in Dial? PING in TestOnBorrow can be remove?

from redigo.

garyburd avatar garyburd commented on September 27, 2024

I am referring to the PING in TestOnBorrow.

Based on the snippet of code pasted here, I would not PING on every call to TestOnBorrow.

from redigo.

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.