Giter VIP home page Giter VIP logo

fttime's Introduction

fttime

fttime是非凸自研的高速时间戳中间件,高频场景中有大量获取时间戳的需求,fttime可以将单次获取时间戳的延时降低到1ns。其原理是由单独的时间戳写入进程负责将时间戳循环写入指定的共享内存,使用方需要获取当前时间戳时直接从共享内存中获取,对比使用 std::time::SystemTime获取时间戳快大约15~20倍(视平台情况),对比使用 chrono方法获取时间戳快大约60倍,适用于对于时间戳精度要求不高(测试误差在-900μs ~ +20μs),但是要求快速返回的场景。

CAUTION: this crate use unchecked_math unstable feature and unsafe code. Only use this crate in rust nightly channel.

使用

  1. 安装writer

    // 安装时间戳写入进程二进制文件
    cd fttime_writer
    cargo install --path ./
    
    // 启动时间戳写入进程
    nohup fttime_writer > /xxx/log/fttime_writer.log 2>&1 & 
    
  2. 安装monitor

    // 安装时间戳生成监控二进制文件
    cd fttime_monitor
    cargo install --path ./
    
    // 启动时间戳监控进程,获取不到时间戳时打印错误日志
    nohup fttime_monitor > /xxx/log/fttime_monitor.log 2>&1 & 
    
  3. 在项目中引入lib依赖

    [dependencies]
    fttime_reader = {git = "https://github.com/nonconvextech/fttime.git", tag="v0.1.0"}
  4. 读取时间戳

    use fttime_reader::{fast_now_nanos, shm_time_init};
    
    fn main(){
        // 需要在同一个系统中开启 fttime_writer
    
        // 使用前需要全局范围内初始化一次
        shm_time_init(); 
        let now_nanos = fast_now_nanos();
        // use now_nanos
        ...
    }

性能对比

对比标准库和chrono,测试结果为10000次的平均值

平台/类库 fttime std lib chrono
OS:macOS Monterey 12.6
cpu:Apple M1 Pro, 3.2 GHz
mem:32G
Rust:1.67.0-nightly
1 ns/iter 22 ns/iter 62 ns/iter
OS:Ubuntu Server 22.04 LTS
cpu:AMD EPYC 7T83 64-Core, 2.45GHz
mem:1.8T
Rust:1.67.0-nightly
2 ns/iter 35 ns/iter 131 ns/iter

fttime's People

Contributors

nonconvextech avatar

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.