如何在系统之间测试时间同步算法的实现?

时间:2014-03-17 19:38:53

标签: node.js algorithm timestamp distributed-computing clock-synchronization

我通过计算主服务器的偏移量实现了一个简单的时间同步算法。处理代码本身存在一些延迟,这会延迟时间戳。所以我想知道如何测试我的算法实际上是否在系统之间同步时间戳?

以下是我在nodejs

中进行时间同步的逻辑
var onSync = function (data) {
    var diff = Date.now() - data.t1 + ((Date.now() - data.t0)/2);
    offsets.unshift(diff);
    if (offsets.length > 10)
      offsets.pop();
    console.log("Order no ",data.ord,"The offset is ",offsets[0] ,"time in server was = ",data.t1 , "time in the slave = ", Date.now() );
  };

系统使用socket.io进行通信。我在服务器中使用了全局var,即 global ,每次获取请求时都会更新, global 值作为data.ord发送到客户端。

所以目前,我有一个主服务器和多个从服务器,它们可以为时间戳保留池。以下是我得到的输出:

主节点:

rahul@g3ck0:~/programs/dos_homework/hw2$ nodejs ob1.js 
Current server timestamp is  1395043602717 order no is  1
Current server timestamp is  1395043603263 order no is  2
Current server timestamp is  1395043603717 order no is  3
Current server timestamp is  1395043604264 order no is  4
Current server timestamp is  1395043604719 order no is  5
Current server timestamp is  1395043605265 order no is  6
Current server timestamp is  1395043605720 order no is  7
Current server timestamp is  1395043606267 order no is  8

奴隶1:

rahul@g3ck0:~/programs/dos_homework/hw2$ nodejs slave1.js
Order no  1 The offset is  2.5 time in server was =  1395043602718 time in the slave =  1395043602719
Order no  3 The offset is  2 time in server was =  1395043603717 time in the slave =  1395043603718
Order no  5 The offset is  1.5 time in server was =  1395043604719 time in the slave =  1395043604720
Order no  7 The offset is  0 time in server was =  1395043605720 time in the slave =  1395043605720

奴隶2:

rahul@g3ck0:~/programs/dos_homework/hw2$ nodejs slave2.js 
Order no  2 The offset is  6 time in server was =  1395043603263 time in the slave =  1395043603268
Order no  4 The offset is  2.5 time in server was =  1395043604264 time in the slave =  1395043604265
Order no  6 The offset is  2 time in server was =  1395043605265 time in the slave =  1395043605266
Order no  8 The offset is  2 time in server was =  1395043606267 time in the slave =  1395043606268

如你所见

  

偏移+时间戳(主)>时间戳(从)

但这种情况在一段时间内持续下降。 总之,我不确定,如果这是正确的做法。我很乐意接受你的投入 1.如何实现更好的算法? 2.我将如何测试?

1 个答案:

答案 0 :(得分:1)

您不太可能比让您的主机与http://en.wikipedia.org/wiki/Network_Time_Protocol同步更好。 Linux和Windows都可以被说服与NTP同步,并在必要时充当NTP服务器(在Windows AD上,一个DC是一个稍微奇怪配置的NTP服务器,以便保持其他机器足够接近Kerberos身份验证到工作)。

典型的计算机时钟并不准确。如果你比较一个具有同步功能的系统并在一个周末进行同步关闭,你应该会发现有明显的漂移同步关闭,但希望很少或没有同步开启。您还可以启动显着不同步的计算机,并查看它们需要多长时间才能同步。默认情况下,NTP不会同步超过一小时的任何内容。我发现这很烦人,因为我们遇到了VMWare设置问题导致机器闲置了几个小时,有时我们有机器出现在1970年,但我准备相信有一个很好的理由不同步极端差异,所以我不会#39 ; t旨在解决任何超过一小时的问题。