diff --git a/.gitignore b/.gitignore index ae3c172..28a280c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.idea /bin/ diff --git a/README.md b/README.md index a00d84e..3861c52 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,20 @@ JIM (Jerbe's Instant Messaging) is a lightweight chat system with the following 4) Because we need to manage chat messages, when sending a message, the message is first stored in the database and then subscribed and pushed after being successfully stored. 5) Supports getting the latest X messages from each room and traversing historical messages. +## Related Unit Tests +### Test Sending Chat Record Speed +```bash + Testing sending speed, 5750 concurrent executions in 10 seconds. Each operation took 2151984 nanoseconds, approximately 2.151984 milliseconds. It doesn't feel fast enough, and there are quite a few allocations. Further optimization is needed. Limited by the machine configuration, it's unclear how it would perform on a high-end configuration. + + > /usr/local/opt/go/libexec/bin/go test -c -o /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test github.com/jerbe/jim/handler #gosetup + > /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkChatSendMessageParallel\E$ -test.run ^$ -test.benchmem -test.benchtime=10s + > goos: darwin + > goarch: amd64 + > pkg: github.com/jerbe/jim/handler + > cpu: Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz + > BenchmarkChatSendMessageParallel-4 5750 2151984 ns/op 469167 B/op 3090 allocs/op +``` + ## Design Diagrams @@ -73,6 +87,7 @@ Here are the planned features: - [x] Cannot log in if disabled - [x] Cannot log in if deleted - [ ] User Logout +- [ ] Recover Account - [x] Account Information - [ ] Password modification - [ ] Avatar modification @@ -126,7 +141,8 @@ Here are the planned features: - [ ] Voice chat - [ ] Video chat - [ ] Mark messages as read - - [x] Cannot chat if blocked by a friend + - [ ] Retract Chat Content +- [x] Cannot chat if blocked by a friend - [x] Cannot chat if deleted by a friend - [x] Group Chat - [x] Send plain text @@ -138,6 +154,7 @@ Here are the planned features: - [ ] Send location - [ ] Voice chat - [ ] Mark messages as read + - [ ] Retract Chat Content - [x] Mute all - [x] Mute specific group members - [x] World Channel Chat diff --git a/README_zh.md b/README_zh.md index 70363c0..2833ce2 100644 --- a/README_zh.md +++ b/README_zh.md @@ -34,6 +34,21 @@ JIM(Jerbe's Instant Messaging) 是一个轻量的聊天系统, 3)在多服务实例的情况下,由于用户建立WS链接的服务器是随机的,所以我们在每个服务实例里使用`websocket.manager`包的管理器进行链接统一管理,订阅到的数据最终也有管理器进行分发到不同的用户链接上。 4)因为我们需要管理聊天消息,所以发消息时,消息是先入库,成功后再进行订阅推送。 5)支持获取每个房间的最近X条消息,及历史消息遍历。 + +## 相关单测 +### 测试发送聊天记录速度 +```bash + 测试发送速度,10s执行了5750个并发,每个op耗时2151984纳秒,约等于2.151984毫秒感觉不够快,allocs还挺多的,得继续优化.受限于机器配置,不知道高配置怎么样 + + > /usr/local/opt/go/libexec/bin/go test -c -o /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test github.com/jerbe/jim/handler #gosetup + > /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkChatSendMessageParallel\E$ -test.run ^$ -test.benchmem -test.benchtime=10s + > goos: darwin + > goarch: amd64 + > pkg: github.com/jerbe/jim/handler + > cpu: Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz + > BenchmarkChatSendMessageParallel-4 5750 2151984 ns/op 469167 B/op 3090 allocs/op +``` + ## 设计图纸 ##### 消息发送基本架构图 @@ -65,6 +80,7 @@ JIM(Jerbe's Instant Messaging) 是一个轻量的聊天系统, - [x] 被禁用无法登录 - [x] 被删除无法登录 - [ ] 用户登出 +- [ ] 找回账户 - [x] 账户信息 - [ ] 密码修改 - [ ] 头型修改 @@ -118,6 +134,7 @@ JIM(Jerbe's Instant Messaging) 是一个轻量的聊天系统, - [ ] 语音聊天 - [ ] 视频聊天 - [ ] 私聊消息已读 + - [ ] 撤回聊天内容 - [x] 被好友拉黑无法对话 - [x] 被好友删除无法对话 - [x] 群聊 @@ -130,6 +147,7 @@ JIM(Jerbe's Instant Messaging) 是一个轻量的聊天系统, - [ ] 语音聊天 - [ ] 视频聊天 - [ ] 消息已读 + - [ ] 撤回聊天内容 - [x] 全体禁言 - [x] 某群成员禁言 - [x] 世界频道聊天 diff --git a/before.sh b/before.sh index 9d1459b..6c949fe 100755 --- a/before.sh +++ b/before.sh @@ -1,6 +1,7 @@ #bin/bash # mkdir ./bin echo "删除已编译文件" +rm -rf ./*\.log rm -rf ./bin/* echo "复制配置文件" diff --git a/handler/chat_test.go b/handler/chat_test.go index 1e27e0d..f8ea3e5 100644 --- a/handler/chat_test.go +++ b/handler/chat_test.go @@ -108,7 +108,7 @@ func BenchmarkChatSendMessageUseApi(b *testing.B) { //r := gin.New() jsonData := `{ "action_id":"随机字符串", - "receiver_id":2, + "target_id":2, "session_type":1, "type":1, "body":{"text":"test"} @@ -196,7 +196,7 @@ func BenchmarkChatSendMessage(b *testing.B) { r := gin.New() jsonData := `{ "action_id":"随机字符串", - "receiver_id":"2", + "target_id":"2", "session_type":1, "type":1, "body":{"text":"test"} @@ -259,7 +259,7 @@ func BenchmarkChatSendMessageParallel(b *testing.B) { r := gin.New() jsonData := `{ "action_id":"随机字符串", - "receiver_id":2, + "target_id":2, "session_type":1, "type":1, "body":{"text":"test"} @@ -307,6 +307,7 @@ func BenchmarkChatSendMessageParallel(b *testing.B) { CheckAuthMiddleware()(ctx) SendChatMessageHandler(ctx) + b.StopTimer() go func(writer io.Writer, reader io.ReadCloser) { defer reader.Close() _, err = io.Copy(writer, reader) @@ -319,7 +320,7 @@ func BenchmarkChatSendMessageParallel(b *testing.B) { log.Println("134", err) } }(file, w.Result().Body) - + b.StartTimer() } }) diff --git a/handler/handler_test.go b/handler/handler_test.go index c0038a0..ec8e236 100644 --- a/handler/handler_test.go +++ b/handler/handler_test.go @@ -4,8 +4,8 @@ import ( "github.com/jerbe/jcache" "github.com/jerbe/jim/config" "github.com/jerbe/jim/database" - "github.com/jerbe/jim/log" "github.com/jerbe/jim/pubsub" + "log" "testing" ) @@ -16,23 +16,25 @@ import ( */ func TestMain(t *testing.M) { - log.Info().Msg("服务初始化中...") - defer log.Warn().Msg("服务已关闭") + log.Println("服务初始化中...") + defer log.Println("服务已关闭...") // 加载配置 + log.Println("加载配置中...") cfg, err := config.Init() if err != nil { - log.Fatal().Err(err).Msg("加载配置文件失败") + log.Println("加载配置文件失败") } + log.Println("配置推收模块('pubsub')...") err = pubsub.Init(cfg) if err != nil { - log.Fatal().Err(err).Msg("初始化推收模块('pubsub')失败") + log.Fatalln("初始化推收模块('pubsub')失败") } // 配置数据库 if _, err = database.Init(cfg); err != nil { - log.Fatal().Err(err).Msg("初始化数据模块('database')失败") + log.Fatalln("初始化数据模块('database')失败") } jcacheCfg := jcache.Config{ @@ -47,7 +49,7 @@ func TestMain(t *testing.M) { } err = jcache.Init(&jcacheCfg) if err != nil { - log.Fatal().Err(err).Msg("初始化缓存模块('cache')失败") + log.Fatalln("初始化缓存模块('cache')失败") } t.Run() } diff --git a/pubsub/chat.go b/pubsub/chat.go index 3af784a..73dab0c 100644 --- a/pubsub/chat.go +++ b/pubsub/chat.go @@ -19,7 +19,7 @@ var chatMessagePool = &sync.Pool{ var chatMessageBodyPool = &sync.Pool{ New: func() any { - return new(*ChatMessageBody) + return new(ChatMessageBody) }, }