diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..298bed9b --- /dev/null +++ b/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2013 The github.com/go-redis/redis Authors. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..50fdc55a --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +all: testdeps + go test ./... + go test ./... -short -race + go vet + +testdeps: testdata/redis/src/redis-server + +bench: testdeps + go test ./... -test.run=NONE -test.bench=. -test.benchmem + +.PHONY: all test testdeps bench + +testdata/redis: + mkdir -p $@ + wget -qO- https://github.com/antirez/redis/archive/unstable.tar.gz | tar xvz --strip-components=1 -C $@ + +testdata/redis/src/redis-server: testdata/redis + sed -i 's/libjemalloc.a/libjemalloc.a -lrt/g' $ +} + +func ExampleClient() { + err := client.Set("key", "value", 0).Err() + if err != nil { + panic(err) + } + + val, err := client.Get("key").Result() + if err != nil { + panic(err) + } + fmt.Println("key", val) + + val2, err := client.Get("key2").Result() + if err == redis.Nil { + fmt.Println("key2 does not exists") + } else if err != nil { + panic(err) + } else { + fmt.Println("key2", val2) + } + // Output: key value + // key2 does not exists +} +``` + +## Howto + +Please go through [examples](https://godoc.org/github.com/go-redis/redis#pkg-examples) to get an idea how to use this package. + +## Look and feel + +Some corner cases: + + SET key value EX 10 NX + set, err := client.SetNX("key", "value", 10*time.Second).Result() + + SORT list LIMIT 0 2 ASC + vals, err := client.Sort("list", redis.Sort{Offset: 0, Count: 2, Order: "ASC"}).Result() + + ZRANGEBYSCORE zset -inf +inf WITHSCORES LIMIT 0 2 + vals, err := client.ZRangeByScoreWithScores("zset", redis.ZRangeBy{ + Min: "-inf", + Max: "+inf", + Offset: 0, + Count: 2, + }).Result() + + ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3 AGGREGATE SUM + vals, err := client.ZInterStore("out", redis.ZStore{Weights: []int64{2, 3}}, "zset1", "zset2").Result() + + EVAL "return {KEYS[1],ARGV[1]}" 1 "key" "hello" + vals, err := client.Eval("return {KEYS[1],ARGV[1]}", []string{"key"}, "hello").Result() + +## Benchmark + +go-redis vs redigo: + +``` +BenchmarkSetGoRedis10Conns64Bytes-4 200000 7621 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis100Conns64Bytes-4 200000 7554 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis10Conns1KB-4 200000 7697 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis100Conns1KB-4 200000 7688 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis10Conns10KB-4 200000 9214 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis100Conns10KB-4 200000 9181 ns/op 210 B/op 6 allocs/op +BenchmarkSetGoRedis10Conns1MB-4 2000 583242 ns/op 2337 B/op 6 allocs/op +BenchmarkSetGoRedis100Conns1MB-4 2000 583089 ns/op 2338 B/op 6 allocs/op +BenchmarkSetRedigo10Conns64Bytes-4 200000 7576 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo100Conns64Bytes-4 200000 7782 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo10Conns1KB-4 200000 7958 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo100Conns1KB-4 200000 7725 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo10Conns10KB-4 100000 18442 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo100Conns10KB-4 100000 18818 ns/op 208 B/op 7 allocs/op +BenchmarkSetRedigo10Conns1MB-4 2000 668829 ns/op 226 B/op 7 allocs/op +BenchmarkSetRedigo100Conns1MB-4 2000 679542 ns/op 226 B/op 7 allocs/op +``` + +Redis Cluster: + +``` +BenchmarkRedisPing-4 200000 6983 ns/op 116 B/op 4 allocs/op +BenchmarkRedisClusterPing-4 100000 11535 ns/op 117 B/op 4 allocs/op +``` + +## See also + +- [Golang PostgreSQL ORM](https://github.com/go-pg/pg) +- [Golang msgpack](https://github.com/vmihailenco/msgpack) +- [Golang message task queue](https://github.com/go-msgqueue/msgqueue) diff --git a/bench/client.cpp b/bench/client.cpp deleted file mode 100755 index 7b769cdd..00000000 --- a/bench/client.cpp +++ /dev/null @@ -1,200 +0,0 @@ -#pragma once -#include "all.h" - -#include "xTcpClient.h" -#include "xTcpconnection.h" -#include "xThreadPool.h" -#include "xCurrentThread.h" -#include "xLog.h" - - -class client; - -class session:noncopyable -{ -public: - session(xEventLoop *loop, - const char *ip,uint16_t port,client *owner) - :cli(loop,nullptr), - ip(ip), - port(port), - owner(owner), - bytesRead(0), - bytesWritten(0), - messagesRead(0) - { - cli.setConnectionCallback(std::bind(&session::connCallBack, this, std::placeholders::_1, std::placeholders::_2)); - cli.setMessageCallback(std::bind(&session::readCallBack, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); - cli.setConnectionErrorCallBack(std::bind(&session::connErrorCallBack, this)); - } - - void start() - { - cli.connect(ip,port); - } - void stop() - { - cli.disconnect(); - } - - - int64_t getBytesRead() { return bytesRead; } - int64_t getMessagesRead() { return messagesRead; } - -private: - - void connCallBack(const xTcpconnectionPtr& conn, void *data); - void connErrorCallBack() - { - //LOG_WARN<<"tcp connect failure"; - } - - void readCallBack(const xTcpconnectionPtr& conn, xBuffer* buf, void *data) - { - ++messagesRead; - bytesRead += buf->readableBytes(); - bytesWritten += buf->readableBytes(); - conn->send(buf); - buf->retrieveAll(); - } - - xTcpClient cli; - const char *ip; - uint16_t port; - client * owner; - int64_t bytesRead; - int64_t bytesWritten; - int64_t messagesRead; - -}; - -class client:noncopyable -{ -public: - client(xEventLoop *loop,const char *ip,uint16_t port,int blockSize,int sessionCount, - int timeOut,int threadCount) - :loop(loop), - threadPool(loop), - sessionCount(sessionCount), - timeOut(timeOut) - { - loop->runAfter(timeOut,nullptr,false, std::bind(&client::handlerTimeout, this,std::placeholders::_1)); - if(threadCount > 1) - { - threadPool.setThreadNum(threadCount); - } - threadPool.start(); - - for(int i = 0; i < blockSize; i ++) - { - message.push_back(static_cast(i % 128)); - } - - for(int i = 0 ; i < sessionCount; i ++) - { - std::shared_ptr vsession (new session(threadPool.getNextLoop(),ip,port,this)); - vsession->start(); - sessions.push_back(vsession); - numConencted++; - } - } - - void onConnect() - { - if (numConencted == sessionCount) - { - LOG_WARN << "all connected"; - } - } - - - void onDisconnect(const xTcpconnectionPtr& conn) - { - numConencted--; - if (numConencted == 0) - { - LOG_WARN << "all disconnected"; - - int64_t totalBytesRead = 0; - int64_t totalMessagesRead = 0; - for(auto it = sessions.begin(); it != sessions.end(); ++it) - { - totalBytesRead += (*it)->getBytesRead(); - totalMessagesRead += (*it)->getMessagesRead(); - } - LOG_WARN << totalBytesRead << " total bytes read"; - LOG_WARN << totalMessagesRead << " total messages read"; - LOG_WARN << static_cast(totalBytesRead) / static_cast(totalMessagesRead)<< " average message size"; - LOG_WARN << static_cast(totalBytesRead) / (timeOut * 1024 * 1024) << " MiB/s throughput"; - conn->getLoop()->queueInLoop(std::bind(&client::quit, this)); - } - } - - - const std::string &getMessage()const - { - return message; - } -private: - void quit() - { - loop->queueInLoop(std::bind(&xEventLoop::quit,loop)); - - } - - void handlerTimeout(void * data) - { - LOG_WARN << "stop"; - std::for_each(sessions.begin(),sessions.end(),std::mem_fn(&session::stop)); - } - - xEventLoop *loop; - xThreadPool threadPool; - int sessionCount; - int timeOut; - std::vector> sessions; - std::string message; - std::atomic numConencted; -}; - - -void session::connCallBack(const xTcpconnectionPtr & conn,void * data) -{ - if (conn->connected()) - { - conn->send(owner->getMessage()); - owner->onConnect(); - } - else - { - owner->onDisconnect(conn); - } -} - - -int main(int argc,char * argv[]) -{ - if (argc != 7) - { - fprintf(stderr, "Usage: client "); - fprintf(stderr, "