“网络读”改为“网络写+本地读”的优化分析

来源:互联网 发布:淘宝热区代码 编辑:程序博客网 时间:2024/06/04 19:44

由于网游经常采用写时把数据实时更新到本地服, 读时直接读本地服的数据, 所以产生一个疑问。这种方案是否真能优化, 达到什么样的条件会优化? 为此,特做以下探讨(写方案没有所谓的优化,不做探讨)。

原始版读方案的流程:  GetAPI  = GetAPI_Net
优化版读方案的流程: GetAPI' = SetAPI_Net + GetAPI_Local

设GetAPI_Net为走网络层读操作, SetAPI_Net为走网络层写操作,GetAPI_Local为本地读操作, 游戏时间为t, 玩家读操作频率为GetP, 玩家写操作频率为SetP, w为原始版流程游戏作用输出, w'为优化版流程游戏作用输出。

则有  w = w'
即 GetAPI_Net * GetP * t =  (SetAPI_Net * SetP * t + GetAPI_Local * GetP * t)
=> SetAPI_Net = (GetP / SetP) * GetAPI_Net - GetAPI_Local * GetP / SetP

当网络稳定时, GetAPI_Net, GetAPI_Local都为常数.设其值分别为C1, C2,  GetP / SetP为x
则有
SetAPI_Net = x* C1- x*C2

读操作时, 原始版服务器开销
SeverCost =  GetAPI_Net = C1
读操作时, 优化版服务器开销
SeverCost' =  SetAPI_Net= x* C1- x*C2

今SeverCost > SeverCost', 则有
C1 - x*C1 + x*C2 > 0
=> x > C1/(C1-C2)
即GetP / SetP > GetAPI_Net/(GetAPI_Net-GetAPI_Local)

当GetP / SetP 比例超过某个常数时 , 优化方案确实让服务器存在优化效果。