Commit 58901501 authored by Rune Memborg's avatar Rune Memborg

Tracing average times for each fuzzy time call

parent 71a10406
Generate dates
1000000 date elements
get fuzzy time
JS 0.518 seconds
JS 0.501 seconds
get WASM fuzzy time
WASM 0 milliseconds
WASM 12.281 seconds
/* tslint:disable */
export function fuzzy_time(arg0: number, arg1: number): any;
export function fuzzy_time(arg0: number, arg1: number): string;
/* tslint:disable */
import * as wasm from './fuzzy_time_bg';
const stack = [];
const slab = [{ obj: undefined }, { obj: null }, { obj: true }, { obj: false }];
function getObject(idx) {
if ((idx & 1) === 1) {
return stack[idx >> 1];
} else {
const val = slab[idx >> 1];
return val.obj;
}
}
let slab_next = slab.length;
function dropRef(idx) {
idx = idx >> 1;
if (idx < 4) return;
let obj = slab[idx];
obj.cnt -= 1;
if (obj.cnt > 0) return;
// If we hit 0 then free up our space in the slab
slab[idx] = slab_next;
slab_next = idx;
}
function takeObject(idx) {
const ret = getObject(idx);
dropRef(idx);
return ret;
}
/**
* @param {number} arg0
* @param {number} arg1
* @returns {any}
*/
export function fuzzy_time(arg0, arg1) {
return takeObject(wasm.fuzzy_time(arg0, arg1));
}
function addHeapObject(obj) {
if (slab_next === slab.length) slab.push(slab.length + 1);
const idx = slab_next;
const next = slab[idx];
slab_next = next;
slab[idx] = { obj, cnt: 1 };
return idx << 1;
}
const TextDecoder = typeof self === 'object' && self.TextDecoder
? self.TextDecoder
: require('util').TextDecoder;
......@@ -75,7 +19,36 @@ function getStringFromWasm(ptr, len) {
return cachedDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}
export function __wbindgen_json_parse(ptr, len) {
return addHeapObject(JSON.parse(getStringFromWasm(ptr, len)));
let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
if (cachedGlobalArgumentPtr === null) {
cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
}
return cachedGlobalArgumentPtr;
}
let cachegetUint32Memory = null;
function getUint32Memory() {
if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
}
return cachegetUint32Memory;
}
/**
* @param {number} arg0
* @param {number} arg1
* @returns {string}
*/
export function fuzzy_time(arg0, arg1) {
const retptr = globalArgumentPtr();
wasm.fuzzy_time(retptr, arg0, arg1);
const mem = getUint32Memory();
const rustptr = mem[retptr / 4];
const rustlen = mem[retptr / 4 + 1];
const realRet = getStringFromWasm(rustptr, rustlen).slice();
wasm.__wbindgen_free(rustptr, rustlen * 1);
return realRet;
}
No preview for this file type
......@@ -43,42 +43,61 @@ function getRandomDateBetween(dateMin, dateMax) {
return new Date(Math.floor(Math.random() * (max - min + 1)) + min);
}
function getAvg(arr) {
var sum = arr.reduce(function(a, b) {
return a + b;
});
return sum / arr.length;
}
function init() {
var dates = [];
var avgJSTimes = [];
var avgTimes = [];
var max = new Date();
var min = new Date(2001, 1, 1);
var maxCount = 1000000;
console.log("Generate dates");
for (var i = 0; i < 1000000; i++) {
for (var i = 0; i < maxCount; i++) {
dates.push(getRandomDateBetween(min, max));
}
console.log(dates.length, "date elements");
console.log(maxCount, "date elements");
console.log("get fuzzy time");
var start = new Date();
console.time("js.fuzzy_time");
dates.forEach(function(date) {
var s = new Date();
fuzzy_time(date.getTime() / 1000);
var e = new Date();
avgJSTimes.push(e.getTime() - s.getTime());
});
var end = new Date();
console.timeEnd("js.fuzzy_time");
var diff = end.getTime() - start.getTime();
var avg = getAvg(avgJSTimes);
console.log("JS", diff / 1000, "seconds");
console.log("JS average", avg, "millisecond");
console.log("get WASM fuzzy time");
start = new Date();
rust.then(m => {
console.time("wasm.fuzzy_time");
dates.forEach(function(date) {
var f = m.fuzzy_time(date.getTime() / 1000, now);
var s = new Date();
m.fuzzy_time(date.getTime() / 1000, now);
var e = new Date();
avgTimes.push(e.getTime() - s.getTime());
});
});
end = new Date();
console.timeEnd("wasm.fuzzy_time");
diff = end.getTime() - start.getTime();
var avg = getAvg(avgTimes);
console.log("WASM", diff, "milliseconds");
console.log("WASM average", avg, "millisecond");
});
}
init();
#[macro_use]
extern crate serde_derive;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
......@@ -12,55 +9,33 @@ const T_WEEK: i64 = T_DAY * 7;
const T_MONTH: i64 = T_DAY * 30;
const T_YEAR: i64 = T_MONTH * 12;
#[derive(Serialize)]
pub enum TimeType {
Year,
Month,
Week,
Day,
Hour,
Minute,
Second,
Now
}
#[derive(Serialize)]
pub struct Fuzzy {
age: i32,
time_type: TimeType
}
#[wasm_bindgen]
pub fn fuzzy_time(then: i32, now: i32) -> JsValue {
pub fn fuzzy_time(then: i32, now: i32) -> String {
let now = now as i64;
let diff = now - (then as i64);
let mut fuzzy = fuzzy_struct(T_SECOND, diff, TimeType::Now);
let mut fuzzy = fuzzy_struct(T_SECOND, diff, 0);
if diff >= T_YEAR {
fuzzy = fuzzy_struct(T_YEAR, diff, TimeType::Year);
fuzzy = fuzzy_struct(T_YEAR, diff, 7);
} else if diff >= T_MONTH {
fuzzy = fuzzy_struct(T_MONTH, diff, TimeType::Month);
fuzzy = fuzzy_struct(T_MONTH, diff, 6);
} else if diff >= T_WEEK {
fuzzy = fuzzy_struct(T_WEEK, diff, TimeType::Week);
fuzzy = fuzzy_struct(T_WEEK, diff, 5);
} else if diff >= T_DAY {
fuzzy = fuzzy_struct(T_DAY, diff, TimeType::Day);
fuzzy = fuzzy_struct(T_DAY, diff, 4);
} else if diff >= T_HOUR {
fuzzy = fuzzy_struct(T_HOUR, diff, TimeType::Hour);
fuzzy = fuzzy_struct(T_HOUR, diff, 3);
} else if diff >= T_MINUTE {
fuzzy = fuzzy_struct(T_MINUTE, diff, TimeType::Minute);
fuzzy = fuzzy_struct(T_MINUTE, diff, 2);
} else if diff >= T_SECOND {
fuzzy = fuzzy_struct(T_SECOND, diff, TimeType::Second);
fuzzy = fuzzy_struct(T_SECOND, diff, 1);
}
JsValue::from_serde(&fuzzy).unwrap()
fuzzy
}
fn fuzzy_struct(time_ref: i64, diff: i64, time_type: TimeType) -> Fuzzy {
Fuzzy {
age: (diff / time_ref) as i32,
time_type: time_type
}
fn fuzzy_struct(time_ref: i64, diff: i64, time_type: i32) -> String {
format!("age:{age},type:{type_ref}", age = (diff/time_ref), type_ref = time_type)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment