Skip to content

Commit

Permalink
refactor: rework as fastant
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Aug 12, 2024
1 parent 0c5b096 commit d80edf2
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 135 deletions.
48 changes: 24 additions & 24 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: CI

on:
push:
branches: [ master ]
branches: [ main ]
pull_request:
branches: [ master ]
branches: [ main ]

env:
CARGO_TERM_COLOR: always
Expand All @@ -14,36 +14,36 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
features: ["atomic", "atomic,fallback-coarse"]
rust: [stable]
os: [ macos-latest, ubuntu-latest, windows-latest ]
features: [ "atomic", "atomic,fallback-coarse" ]
rust: [ stable ]
env:
RUST_BACKTRACE: 1
steps:
- uses: actions/checkout@v2
- name: Set up toolchains
uses: actions-rs/toolchain@v1
with:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Set up toolchains
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
override: true
components: rustfmt, clippy
- name: Check format
run: cargo fmt --all -- --check
- name: Build
run: cargo build --workspace --all-targets --features ${{ matrix.features }}
- name: Clippy
run: cargo clippy --workspace --all-targets --features ${{ matrix.features }} -- -D warnings
- name: Run tests
run: cargo test --workspace --all-targets --features ${{ matrix.features }} -- --nocapture
- name: Run benches
run: cargo bench --workspace --all-targets --features ${{ matrix.features }}
- name: Check format
run: cargo fmt --all -- --check
- name: Build
run: cargo build --workspace --all-targets --features ${{ matrix.features }}
- name: Clippy
run: cargo clippy --workspace --all-targets --features ${{ matrix.features }} -- -D warnings
- name: Run tests
run: cargo test --workspace --all-targets --features ${{ matrix.features }} -- --nocapture
- name: Run benches
run: cargo bench --workspace --all-targets --features ${{ matrix.features }}

build-wasm:
runs-on: ubuntu-latest
env:
RUST_BACKTRACE: 1
steps:
- uses: actions/checkout@v2
- name: Set up toolchains
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack test --node
- uses: actions/checkout@v4
- name: Set up toolchains
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack test --node
21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
[package]
name = "minstant"
version = "0.1.7"
authors = ["The TiKV Authors"]
name = "fastant"
version = "0.1.0"
authors = ["FastLabs Developers"]
edition = "2021"
license = "MIT"
description = "A drop-in replacement for `std::time::Instant` that measures time with high performance and high accuracy powered by TSC"
homepage = "https://github.com/tikv/minstant"
repository = "https://github.com/tikv/minstant"
documentation = "https://docs.rs/minstant"
description = "A drop-in replacement for `std::time::Instant` that measures time with high performance and high accuracy powered by Time Stamp Counter (TSC)."
homepage = "https://github.com/fast/fastant"
repository = "https://github.com/fast/fastant"
documentation = "https://docs.rs/fastant"
readme = "README.md"
keywords = ["TSC", "clock", "rdtsc", "timing", "nanosecond"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
ctor = "0.1.20"
ctor = "0.2"
coarsetime = { version = "0.1", optional = true }
web-time = "1.0"

Expand All @@ -24,8 +25,8 @@ atomic = []
fallback-coarse = ["coarsetime"]

[dev-dependencies]
criterion = "0.3"
quanta = "0.9"
criterion = "0.5"
quanta = "0.12"
rand = "0.8"
wasm-bindgen-test = "0.3"
getrandom = { version = "0.2", features = ["js"] }
Expand Down
48 changes: 15 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,37 @@
# minstant
[![Actions Status](https://github.com/tikv/minstant/workflows/CI/badge.svg)](https://github.com/tikv/minstant/actions)
[![Build Status](https://travis-ci.org/tikv/minstant.svg?branch=master)](https://travis-ci.org/tikv/minstant)
[![Documentation](https://docs.rs/minstant/badge.svg)](https://docs.rs/minstant/)
[![Crates.io](https://img.shields.io/crates/v/minstant.svg)](https://crates.io/crates/minstant)
[![LICENSE](https://img.shields.io/github/license/tikv/minstant.svg)](https://github.com/tikv/minstant/blob/master/LICENSE)
# Fastant

A drop-in replacement for [`std::time::Instant`](https://doc.rust-lang.org/std/time/struct.Instant.html) that measures time with high performance and high accuracy powered by [TSC](https://en.wikipedia.org/wiki/Time_Stamp_Counter).
A drop-in replacement for [`std::time::Instant`](https://doc.rust-lang.org/std/time/struct.Instant.html) that measures time with high performance and high accuracy powered by [Time Stamp Counter (TSC)](https://en.wikipedia.org/wiki/Time_Stamp_Counter).

[![Actions Status](https://github.com/fast/fastant/workflows/CI/badge.svg)](https://github.com/fast/fastant/actions)
[![Documentation](https://docs.rs/fastant/badge.svg)](https://docs.rs/fastant/)
[![Crates.io](https://img.shields.io/crates/v/fastant.svg)](https://crates.io/crates/fastant)
[![LICENSE](https://img.shields.io/github/license/fast/fastant.svg)](LICENSE)

## Usage

```toml
[dependencies]
minstant = "0.1"
fastant = "0.1"
```

```rust
let start = minstant::Instant::now();

// Code snipppet to measure

let duration: std::time::Duration = start.elapsed();
fn main() {
let start = fastant::Instant::now();
let duration: std::time::Duration = start.elapsed();
}
```


## Motivation

This library is used by a high performance tracing library [`minitrace-rust`](https://github.com/tikv/minitrace-rust). The main purpose is to use [TSC](https://en.wikipedia.org/wiki/Time_Stamp_Counter) on x86 processors to measure time at high speed without losing much accuracy.
This library is used by a high performance tracing library [`fastrace`](https://github.com/fast/fastrace). The main purpose is to use [Time Stamp Counter (TSC)](https://en.wikipedia.org/wiki/Time_Stamp_Counter) on x86 processors to measure time at high speed without losing much accuracy.

## Platform Support

Currently, only the Linux on `x86` or `x86_64` is backed by [TSC](https://en.wikipedia.org/wiki/Time_Stamp_Counter). On other platforms, `minstant` falls back to `std::time`. If TSC is unstable, it will also fall back to `std::time`.
Currently, only the Linux on `x86` or `x86_64` is backed by Time Stamp Counter (TSC). On other platforms, Fastant falls back to `std::time`. If TSC is unstable, it will also fall back to `std::time`.

If speed is privileged over accuracy when fallback occurs, you can use `fallback-coarse` feature to use coarse time:

```toml
[dependencies]
minstant = { version = "0.1", features = ["fallback-coarse"] }
```

## Benchmark

Benchmark platform is `Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz` on CentOS 7.

```sh
> cargo criterion

Instant::now()/minstant time: [10.449 ns 10.514 ns 10.619 ns]
Instant::now()/quanta time: [31.467 ns 31.628 ns 31.822 ns]
Instant::now()/std time: [26.831 ns 26.924 ns 27.016 ns]
minstant::Anchor::new() time: [46.987 ns 47.243 ns 47.498 ns]
minstant::Instant::as_unix_nanos() time: [15.287 ns 15.318 ns 15.350 ns]
fastant = { version = "0.1", features = ["fallback-coarse"] }
```

![Benchmark](benchmark.jpeg)
20 changes: 10 additions & 10 deletions benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn bench_nows(c: &mut Criterion) {
// The first call will take some time for calibartion
fn bench_now(c: &mut Criterion) {
// The first call will take some time for calibration
quanta::Instant::now();

let mut group = c.benchmark_group("Instant::now()");
group.bench_function("minstant", |b| {
b.iter(minstant::Instant::now);
group.bench_function("fastant", |b| {
b.iter(fastant::Instant::now);
});
group.bench_function("quanta", |b| {
b.iter(quanta::Instant::now);
Expand All @@ -18,19 +18,19 @@ fn bench_nows(c: &mut Criterion) {
}

fn bench_anchor_new(c: &mut Criterion) {
c.bench_function("minstant::Anchor::new()", |b| {
b.iter(minstant::Anchor::new);
c.bench_function("fastant::Anchor::new()", |b| {
b.iter(fastant::Anchor::new);
});
}

fn bench_as_unix_nanos(c: &mut Criterion) {
let anchor = minstant::Anchor::new();
c.bench_function("minstant::Instant::as_unix_nanos()", |b| {
let anchor = fastant::Anchor::new();
c.bench_function("fastant::Instant::as_unix_nanos()", |b| {
b.iter(|| {
black_box(minstant::Instant::now().as_unix_nanos(&anchor));
black_box(fastant::Instant::now().as_unix_nanos(&anchor));
});
});
}

criterion_group!(benches, bench_nows, bench_anchor_new, bench_as_unix_nanos);
criterion_group!(benches, bench_now, bench_anchor_new, bench_as_unix_nanos);
criterion_main!(benches);
Binary file removed benchmark.jpeg
Binary file not shown.
12 changes: 12 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
comment_width = 120
edition = "2021"
format_code_in_doc_comments = true
group_imports = "StdExternalCrate"
imports_granularity = "Item"
normalize_comments = true
overflow_delimited_expr = true
reorder_imports = true
trailing_comma = "Vertical"
version = "Two"
where_single_line = true
wrap_comments = true
Loading

0 comments on commit d80edf2

Please sign in to comment.