Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:22

0001 // ./stamp_test.sh 
0002 
0003 #include <chrono>
0004 #include <iostream>
0005 #include <iomanip>
0006 
0007 #include "stamp.h"
0008 #include "NP.hh"
0009 
0010 
0011 /**
0012 test_reverse
0013 -------------
0014 
0015 1. creates uint64_t timestamp t0 using stamp::Now
0016 2. uses that integer to create std::chrono::time_point and gets the count 
0017 3. asserts that they match 
0018 
0019 Success of this test means can safely store time stamps at uint64_t values
0020 
0021 **/
0022 
0023 void test_reverse()
0024 {
0025     std::cout << std::endl << "test_reverse " << std::endl ; 
0026     uint64_t t0 = stamp::Now(); 
0027 
0028     using Clock = std::chrono::system_clock;
0029     using Unit  = std::chrono::microseconds  ; 
0030 
0031     std::chrono::time_point<Clock> t0_{Unit{t0}} ; 
0032     uint64_t t1 = std::chrono::duration_cast<Unit>(t0_.time_since_epoch()).count() ; 
0033 
0034     std::cout << "t0: " << t0 <<  std::endl ; 
0035     std::cout << "t1: " << t1 <<  std::endl ; 
0036 
0037     assert( t0 == t1 ) ; 
0038 }
0039 
0040 
0041 /**
0042 test_performance
0043 ----------------
0044 
0045 1. creates an array able to hold 25 million uint64_t values 
0046 2. fills the array with timestamps 
0047 3. saves the array to filepath $FOLD/tt.npy 
0048 
0049 On old laptop: counting and stamp recording to 25M takes roughly 1s (1M us)
0050 so 25 stamp records takes ~1us  
0051 (microsecond, 10-6 second, one millionth of a second)
0052  
0053 **/
0054 
0055 void test_performance()
0056 {
0057     static const int N = 1000000*25 ;  
0058     const char* ttpath = "$FOLD/tt.npy" ; 
0059 
0060     std::cout << std::endl << "test_performance : N " << N  << " : save stamps to ttpath " << ttpath << std::endl ; 
0061 
0062     NP* t = NP::Make<uint64_t>(N) ;  
0063     uint64_t* tt = t->values<uint64_t>(); 
0064     for(int i=0 ; i < N ; i++) tt[i] = stamp::Now(); 
0065 
0066     t->set_meta<std::string>("IDENTITY", U::GetEnv("IDENTITY", "no-IDENTITY") );     
0067     t->save(ttpath);  
0068 }
0069 
0070 /**
0071 test_format
0072 -------------
0073 
0074 1. create timestamp uint64_t 
0075 2. exercise stamp::Format using that timestamp and other values
0076 
0077 **/
0078 
0079 
0080 void test_format()
0081 {
0082     std::cout << std::endl << "test_format" << std::endl ; 
0083     uint64_t t0 = stamp::Now(); 
0084     std::cout << t0 << " : " << stamp::Format(t0) << " " << stamp::Format() << std::endl ; 
0085 
0086     std::cout << "stamp::Format()             " << stamp::Format() << std::endl; 
0087     std::cout << "stamp::Format(stamp::Now()) " << stamp::Format(stamp::Now()) << std::endl; 
0088     std::cout << "stamp::Format(0)            " << stamp::Format(0) << std::endl; 
0089     std::cout << "stamp::Format(1)            " << stamp::Format(1) << std::endl; 
0090     std::cout << "stamp::Format(1000)         " << stamp::Format(1000) << std::endl; 
0091     std::cout << "stamp::Format(1000000)      " << stamp::Format(1000000) << std::endl; 
0092     std::cout << "NOTE TIMEZONE LOCALIZATION OF THE ASSUMED UTC STAMPS" << std::endl ;  
0093 
0094     for(int i=0 ; i < 10 ; i++) 
0095     {
0096         uint64_t t = stamp::Now();
0097         std::cout << std::setw(5) << i << " : " << stamp::Format(t) << std::setw(9) << (t - t0) << std::endl ; 
0098     }
0099 }
0100 
0101    
0102 int main() 
0103 {
0104     test_reverse(); 
0105     test_performance(); 
0106     test_format(); 
0107     return 0 ; 
0108 }
0109