Home | History | Annotate | Download | only in perftest
      1 // Tencent is pleased to support the open source community by making RapidJSON available.
      2 //
      3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
      4 //
      5 // Licensed under the MIT License (the "License"); you may not use this file except
      6 // in compliance with the License. You may obtain a copy of the License at
      7 //
      8 // http://opensource.org/licenses/MIT
      9 //
     10 // Unless required by applicable law or agreed to in writing, software distributed
     11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
     13 // specific language governing permissions and limitations under the License.
     14 
     15 #include "perftest.h"
     16 
     17 // This file is for giving the performance characteristics of the platform (compiler/OS/CPU).
     18 
     19 #if TEST_PLATFORM
     20 
     21 #include <cmath>
     22 #include <fcntl.h>
     23 
     24 // Windows
     25 #ifdef _WIN32
     26 #include <windows.h>
     27 #endif
     28 
     29 // UNIX
     30 #if defined(unix) || defined(__unix__) || defined(__unix)
     31 #include <unistd.h>
     32 #ifdef _POSIX_MAPPED_FILES
     33 #include <sys/mman.h>
     34 #endif
     35 #endif
     36 
     37 class Platform : public PerfTest {
     38 public:
     39     virtual void SetUp() {
     40         PerfTest::SetUp();
     41 
     42         // temp buffer for testing
     43         temp_ = (char *)malloc(length_ + 1);
     44         memcpy(temp_, json_, length_);
     45         checkSum_ = CheckSum();
     46     }
     47 
     48     char CheckSum() {
     49         char c = 0;
     50         for (size_t i = 0; i < length_; ++i)
     51             c += temp_[i];
     52         return c;
     53     }
     54 
     55     virtual void TearDown() {
     56         PerfTest::TearDown();
     57         free(temp_);
     58     }
     59 
     60 protected:
     61     char *temp_;
     62     char checkSum_;
     63 };
     64 
     65 TEST_F(Platform, CheckSum) {
     66     for (int i = 0; i < kTrialCount; i++)
     67         EXPECT_EQ(checkSum_, CheckSum());
     68 }
     69 
     70 TEST_F(Platform, strlen) {
     71     for (int i = 0; i < kTrialCount; i++) {
     72         size_t l = strlen(json_);
     73         EXPECT_EQ(length_, l);
     74     }
     75 }
     76 
     77 TEST_F(Platform, memcmp) {
     78     for (int i = 0; i < kTrialCount; i++) {
     79         EXPECT_EQ(0, memcmp(temp_, json_, length_));
     80     }
     81 }
     82 
     83 TEST_F(Platform, pow) {
     84     double sum = 0;
     85     for (int i = 0; i < kTrialCount * kTrialCount; i++)
     86         sum += pow(10.0, i & 255);
     87     EXPECT_GT(sum, 0.0);
     88 }
     89 
     90 TEST_F(Platform, Whitespace_strlen) {
     91     for (int i = 0; i < kTrialCount; i++) {
     92         size_t l = strlen(whitespace_);
     93         EXPECT_GT(l, whitespace_length_);
     94     }
     95 }
     96 
     97 TEST_F(Platform, Whitespace_strspn) {
     98     for (int i = 0; i < kTrialCount; i++) {
     99         size_t l = strspn(whitespace_, " \n\r\t");
    100         EXPECT_EQ(whitespace_length_, l);
    101     }
    102 }
    103 
    104 TEST_F(Platform, fread) {
    105     for (int i = 0; i < kTrialCount; i++) {
    106         FILE *fp = fopen(filename_, "rb");
    107         ASSERT_EQ(length_, fread(temp_, 1, length_, fp));
    108         EXPECT_EQ(checkSum_, CheckSum());
    109         fclose(fp);
    110     }
    111 }
    112 
    113 #ifdef _MSC_VER
    114 TEST_F(Platform, read) {
    115     for (int i = 0; i < kTrialCount; i++) {
    116         int fd = _open(filename_, _O_BINARY | _O_RDONLY);
    117         ASSERT_NE(-1, fd);
    118         ASSERT_EQ(length_, _read(fd, temp_, length_));
    119         EXPECT_EQ(checkSum_, CheckSum());
    120         _close(fd);
    121     }
    122 }
    123 #else
    124 TEST_F(Platform, read) {
    125     for (int i = 0; i < kTrialCount; i++) {
    126         int fd = open(filename_, O_RDONLY);
    127         ASSERT_NE(-1, fd);
    128         ASSERT_EQ(length_, read(fd, temp_, length_));
    129         EXPECT_EQ(checkSum_, CheckSum());
    130         close(fd);
    131     }
    132 }
    133 #endif
    134 
    135 #ifdef _WIN32
    136 TEST_F(Platform, MapViewOfFile) {
    137     for (int i = 0; i < kTrialCount; i++) {
    138         HANDLE file = CreateFile(filename_, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    139         ASSERT_NE(INVALID_HANDLE_VALUE, file);
    140         HANDLE mapObject = CreateFileMapping(file, NULL, PAGE_READONLY, 0, length_, NULL);
    141         ASSERT_NE(INVALID_HANDLE_VALUE, mapObject);
    142         void *p = MapViewOfFile(mapObject, FILE_MAP_READ, 0, 0, length_);
    143         ASSERT_TRUE(p != NULL);
    144         EXPECT_EQ(checkSum_, CheckSum());
    145         ASSERT_TRUE(UnmapViewOfFile(p) == TRUE);
    146         ASSERT_TRUE(CloseHandle(mapObject) == TRUE);
    147         ASSERT_TRUE(CloseHandle(file) == TRUE);
    148     }
    149 }
    150 #endif
    151 
    152 #ifdef _POSIX_MAPPED_FILES
    153 TEST_F(Platform, mmap) {
    154     for (int i = 0; i < kTrialCount; i++) {
    155         int fd = open(filename_, O_RDONLY);
    156         ASSERT_NE(-1, fd);
    157         void *p = mmap(NULL, length_, PROT_READ, MAP_PRIVATE, fd, 0);
    158         ASSERT_TRUE(p != NULL);
    159         EXPECT_EQ(checkSum_, CheckSum());
    160         munmap(p, length_);
    161         close(fd);
    162     }
    163 }
    164 #endif
    165 
    166 #endif // TEST_PLATFORM
    167