mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-11-30 02:47:56 +08:00
add test sample for gson.
This commit is contained in:
parent
aaa7ffe1d6
commit
0ad006d887
5
app/gson/test1/Makefile
Normal file
5
app/gson/test1/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ./Makefile.in
|
||||
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
|
||||
EXTLIBS += -L/usr/local/lib -liconv
|
||||
endif
|
||||
PROG = json
|
115
app/gson/test1/Makefile.in
Normal file
115
app/gson/test1/Makefile.in
Normal file
@ -0,0 +1,115 @@
|
||||
CC = g++
|
||||
|
||||
CFLAGS = -c -g -W -Wall -Wcast-qual -Wcast-align \
|
||||
-Wno-long-long \
|
||||
-Wpointer-arith -Werror -Wshadow -O3 \
|
||||
-D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS -D_USE_FAST_MACRO
|
||||
|
||||
###########################################################
|
||||
#Check system:
|
||||
# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX
|
||||
SYSLIB = -lpthread -lz
|
||||
CHECKSYSRES = @echo "Unknow system type!";exit 1
|
||||
UNIXNAME = $(shell uname -sm)
|
||||
OSTYPE = $(shell uname -p)
|
||||
RPATH = linux64
|
||||
|
||||
ifeq ($(CC),)
|
||||
CC = gcc
|
||||
endif
|
||||
|
||||
# For FreeBSD
|
||||
ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD)
|
||||
ifeq ($(findstring gcc, $(CC)), gcc)
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
endif
|
||||
CFLAGS += -DFREEBSD -D_REENTRANT
|
||||
SYSLIB = -lcrypt -lpthread -lz
|
||||
RPATH = freebsd
|
||||
endif
|
||||
|
||||
# For Darwin
|
||||
ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin)
|
||||
CFLAGS += -DMACOSX -Wno-invalid-source-encoding \
|
||||
-Wno-extended-offsetof
|
||||
UNIXTYPE = MACOSX
|
||||
SYSLIB += -liconv -rdynamic
|
||||
endif
|
||||
|
||||
#Path for Linux
|
||||
ifeq ($(findstring Linux, $(UNIXNAME)), Linux)
|
||||
ifeq ($CC, "gcc")
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
endif
|
||||
ifeq ($(findstring i686, $(OSTYPE)), i686)
|
||||
RPATH = linux32
|
||||
endif
|
||||
ifeq ($(findstring x86_64, $(OSTYPE)), x86_64)
|
||||
RPATH = linux64
|
||||
endif
|
||||
CFLAGS += -DLINUX2 -D_REENTRANT
|
||||
SYSLIB += -lcrypt
|
||||
endif
|
||||
|
||||
#Path for SunOS
|
||||
ifeq ($(findstring SunOS, $(UNIXNAME)), SunOS)
|
||||
ifeq ($(findstring 86, $(UNIXNAME)), 86)
|
||||
SYSLIB += -lsocket -lnsl -lrt
|
||||
endif
|
||||
ifeq ($(findstring sun4u, $(UNIXNAME)), sun4u)
|
||||
SYSLIB += -lsocket -lnsl -lrt
|
||||
endif
|
||||
ifeq ($CC, "gcc")
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
endif
|
||||
CFLAGS += -DSUNOS5 -D_REENTRANT
|
||||
RPATH = sunos_x86
|
||||
endif
|
||||
|
||||
#Path for HP-UX
|
||||
ifeq ($(findstring HP-UX, $(UNIXNAME)), HP-UX)
|
||||
ifeq ($CC, "gcc")
|
||||
CFLAGS += -Wstrict-prototypes
|
||||
endif
|
||||
CFLAGS += -DHP_UX -DHPUX11
|
||||
PLAT_NAME=hp-ux
|
||||
endif
|
||||
|
||||
#Find system type.
|
||||
ifneq ($(SYSPATH),)
|
||||
CHECKSYSRES = @echo "System is $(shell uname -sm)"
|
||||
endif
|
||||
###########################################################
|
||||
|
||||
CFLAGS += -I../../../lib_acl/include -I../../../lib_protocol/include -I../../../lib_acl_cpp/include
|
||||
EXTLIBS =
|
||||
LDFLAGS = -L../../../lib_acl_cpp/lib -l_acl_cpp -L../../../lib_protocol/lib -l_protocol -L../../../lib_acl/lib -l_acl \
|
||||
$(EXTLIBS) $(SYSLIB)
|
||||
|
||||
COMPILE = $(CC) $(CFLAGS)
|
||||
LINK = $(CC) $(OBJ) $(LDFLAGS)
|
||||
###########################################################
|
||||
OBJ_PATH = .
|
||||
|
||||
#Project's objs
|
||||
SRC = $(wildcard *.cpp)
|
||||
OBJ = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(notdir $(SRC)))
|
||||
|
||||
$(OBJ_PATH)/%.o: %.cpp
|
||||
$(COMPILE) $< -o $@
|
||||
|
||||
.PHONY = all clean
|
||||
all: RM $(OBJ)
|
||||
$(LINK) -o $(PROG)
|
||||
@echo ""
|
||||
@echo "All ok! Output:$(PROG)"
|
||||
@echo ""
|
||||
RM:
|
||||
rm -f $(PROG)
|
||||
clean:
|
||||
rm -f $(PROG)
|
||||
rm -f $(OBJ)
|
||||
install:
|
||||
cp $(PROG) ../../../dist/master/libexec/$(RPATH)/
|
||||
cp $(PROG).cf ../../../dist/master/conf/service/
|
||||
###########################################################
|
255
app/gson/test1/main.cpp
Normal file
255
app/gson/test1/main.cpp
Normal file
@ -0,0 +1,255 @@
|
||||
#include "stdafx.h"
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <stdio.h>
|
||||
#include <iostream>
|
||||
#include <time.h>
|
||||
#include "struct.h"
|
||||
#include "gson.h"
|
||||
#include "util.h"
|
||||
|
||||
static void print_msg(message& msg)
|
||||
{
|
||||
printf("type: %d\r\n", msg.type_);
|
||||
printf("cmd: %s\r\n", msg.cmd_.c_str());
|
||||
|
||||
size_t i = 0;
|
||||
for (auto cit : msg.data_)
|
||||
{
|
||||
printf(">>username: %s, domain: %s, age: %d, male: %s\r\n",
|
||||
cit.username_.c_str(), cit.domain_.c_str(),
|
||||
cit.age_, cit.male_ ? "true" : "false");
|
||||
if (++i >= 10)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_msg1(message1& msg)
|
||||
{
|
||||
printf("type: %d\r\n", msg.type_);
|
||||
printf("cmd: %s\r\n", msg.cmd_.c_str());
|
||||
|
||||
size_t i = 0;
|
||||
for (auto cit : msg.data_)
|
||||
{
|
||||
printf(">>username: %s, domain: %s, age: %d, male: %s\r\n",
|
||||
cit->username_, cit->domain_,
|
||||
cit->age_, cit->male_ ? "true" : "false");
|
||||
if (++i >= 10)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void test1(void)
|
||||
{
|
||||
message msg;
|
||||
|
||||
msg.type_ = 1;
|
||||
msg.cmd_ = "add";
|
||||
|
||||
user user = {"zsx1", "263.net1", 11, true};
|
||||
msg.data_.emplace_back(user);
|
||||
|
||||
user = {"zsx2", "263.net2", 12, true};
|
||||
msg.data_.emplace_back(user);
|
||||
|
||||
msg.data_.emplace_back("zsx3", "263.net3", 13, true);
|
||||
msg.data_.emplace_back("zsx2", "263.net2", 12, false);
|
||||
|
||||
acl::json json;
|
||||
acl::json_node& node = acl::gson(json, msg);
|
||||
printf("%s\r\n", node.to_string().c_str());
|
||||
|
||||
printf("------------------------------------------------------\r\n");
|
||||
printf("%s\r\n", json.to_string().c_str());
|
||||
printf("%s\r\n", node.to_string().c_str());
|
||||
printf("------------------------------------------------------\r\n");
|
||||
|
||||
message msg1;
|
||||
acl::json json1;
|
||||
json1.update(node.to_string());
|
||||
std::pair<bool, std::string> ret = acl::gson(json1.get_root(), msg1);
|
||||
if (ret.first == false)
|
||||
printf("error: %s\r\n", ret.second.c_str());
|
||||
else
|
||||
{
|
||||
printf("---- ok ----\r\n");
|
||||
print_msg(msg);
|
||||
}
|
||||
}
|
||||
|
||||
static void benchmark(int max)
|
||||
{
|
||||
message msg;
|
||||
msg.type_ = 10;
|
||||
msg.cmd_ = "add";
|
||||
|
||||
struct timeval begin;
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
for (int i = 0; i < max; i++)
|
||||
msg.data_.emplace_back("zsx", "263.net", 11, true);
|
||||
|
||||
acl::json json;
|
||||
acl::json_node& node = acl::gson(json, msg);
|
||||
|
||||
struct timeval end;
|
||||
gettimeofday(&end, NULL);
|
||||
double spent = util::stamp_sub(&end, &begin);
|
||||
printf("emplace_back, struct --> json spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
printf("------------------------------------------------------\r\n");
|
||||
|
||||
printf("Enter any key to continue ...");
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
|
||||
acl::json json1;
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
const acl::string& buf = node.to_string();
|
||||
gettimeofday(&end, NULL);
|
||||
spent = util::stamp_sub(&end, &begin);
|
||||
printf("json to_string spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
json1.update(buf);
|
||||
|
||||
gettimeofday(&end, NULL);
|
||||
spent = util::stamp_sub(&end, &begin);
|
||||
printf("json parsing spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
printf("------------------------------------------------------\r\n");
|
||||
|
||||
printf("Enter any key to continue ...");
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
|
||||
message msg1;
|
||||
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
std::pair<bool, std::string> res = acl::gson(json1.get_root(), msg1);
|
||||
|
||||
gettimeofday(&end, NULL);
|
||||
|
||||
spent = util::stamp_sub(&end, &begin);
|
||||
printf("json --> struct spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
printf("------------------------------------------------------\r\n");
|
||||
|
||||
if (res.first == false)
|
||||
printf("error: %s\r\n", res.second.c_str());
|
||||
else
|
||||
{
|
||||
print_msg(msg1);
|
||||
printf("------------------- ok --------------------\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void test_mem(int max)
|
||||
{
|
||||
message1* msg = new message1;
|
||||
|
||||
struct timeval begin;
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
msg->type_ = 100;
|
||||
msg->cmd_ = "add";
|
||||
|
||||
for (int i = 0; i < max; i++)
|
||||
{
|
||||
user1* user = new user1("zsx", "263.net", 11, true);
|
||||
msg->data_.push_back(user);
|
||||
}
|
||||
|
||||
acl::json json;
|
||||
acl::json_node& node = acl::gson(json, msg);
|
||||
|
||||
struct timeval end;
|
||||
gettimeofday(&end, NULL);
|
||||
double spent = util::stamp_sub(&end, &begin);
|
||||
printf("push_back, struct --> json spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
delete msg;
|
||||
|
||||
printf("Enter any key to continue ...");
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
acl::json json1;
|
||||
json1.update(node.to_string());
|
||||
|
||||
msg = new message1;
|
||||
gettimeofday(&begin, NULL);
|
||||
|
||||
std::pair<bool, std::string> res = acl::gson(json1.get_root(), msg);
|
||||
|
||||
gettimeofday(&end, NULL);
|
||||
|
||||
spent = util::stamp_sub(&end, &begin);
|
||||
|
||||
printf("json --> struct spent: %.2f ms, count: %d, speed: %.2f\r\n",
|
||||
spent, max, (max * 1000) / (spent == 0 ? 1 : spent));
|
||||
|
||||
printf("------------------------------------------------------\r\n");
|
||||
|
||||
if (res.first == false)
|
||||
printf("error: %s\r\n", res.second.c_str());
|
||||
else
|
||||
{
|
||||
print_msg1(*msg);
|
||||
printf("------------------- ok --------------------\r\n");
|
||||
}
|
||||
|
||||
delete msg;
|
||||
}
|
||||
|
||||
static void usage(const char* procname)
|
||||
{
|
||||
printf("usage: %s -h [help]\r\n"
|
||||
" -n max_count\r\n", procname);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int ch, max = 10000;
|
||||
|
||||
while ((ch = getopt(argc, argv, "hn:")) > 0)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case 'h':
|
||||
usage(argv[0]);
|
||||
return 0;
|
||||
case 'n':
|
||||
max = atoi(optarg);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
test1();
|
||||
benchmark(max);
|
||||
printf("Enter any key to continue ...");
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
|
||||
test_mem(max);
|
||||
|
||||
printf("Enter any key to exit ...");
|
||||
fflush(stdout);
|
||||
getchar();
|
||||
|
||||
return 0;
|
||||
}
|
8
app/gson/test1/stdafx.cpp
Normal file
8
app/gson/test1/stdafx.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// stdafx.cpp : 只包括标准包含文件的源文件
|
||||
// wizard.pch 将成为预编译头
|
||||
// stdafx.obj 将包含预编译类型信息
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: 在 STDAFX.H 中
|
||||
//引用任何所需的附加头文件,而不是在此文件中引用
|
15
app/gson/test1/stdafx.h
Normal file
15
app/gson/test1/stdafx.h
Normal file
@ -0,0 +1,15 @@
|
||||
// stdafx.h : 标准系统包含文件的包含文件,
|
||||
// 或是常用但不常更改的项目特定的包含文件
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
//#include <iostream>
|
||||
//#include <tchar.h>
|
||||
|
||||
// TODO: 在此处引用程序要求的附加头文件
|
||||
|
||||
#include "lib_acl.h"
|
||||
#include "acl_cpp/lib_acl.hpp"
|
||||
#include "lib_protocol.h"
|
78
app/gson/test1/struct.h
Normal file
78
app/gson/test1/struct.h
Normal file
@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
struct user
|
||||
{
|
||||
user(const char* username, const char* domain, int age, bool male)
|
||||
: username_(username)
|
||||
, domain_(domain)
|
||||
, age_(age)
|
||||
, male_(male)
|
||||
{}
|
||||
|
||||
user() {}
|
||||
~user() {}
|
||||
|
||||
acl::string username_;
|
||||
acl::string domain_;
|
||||
int age_;
|
||||
bool male_;
|
||||
};
|
||||
|
||||
struct message
|
||||
{
|
||||
int type_;
|
||||
acl::string cmd_;
|
||||
std::list<user> data_;
|
||||
};
|
||||
|
||||
struct user1
|
||||
{
|
||||
user1(const char* username, const char* domain, int age, bool male)
|
||||
{
|
||||
size_t n = strlen(username);
|
||||
username_ = new char[n + 1];
|
||||
memcpy(username_, username, n);
|
||||
username_[n] = 0;
|
||||
|
||||
n = strlen(domain);
|
||||
domain_ = new char[n + 1];
|
||||
memcpy(domain_, domain, n);
|
||||
domain_[n] = 0;
|
||||
age_ = age;
|
||||
male_ = male;
|
||||
}
|
||||
|
||||
user1()
|
||||
{
|
||||
username_ = NULL;
|
||||
domain_ = NULL;
|
||||
}
|
||||
|
||||
~user1()
|
||||
{
|
||||
delete username_;
|
||||
delete domain_;
|
||||
}
|
||||
|
||||
char* username_;
|
||||
char* domain_;
|
||||
int age_;
|
||||
bool male_;
|
||||
};
|
||||
|
||||
struct message1
|
||||
{
|
||||
message1()
|
||||
{
|
||||
}
|
||||
|
||||
~message1()
|
||||
{
|
||||
for (auto it = data_.begin(); it != data_.end(); ++it)
|
||||
delete *it;
|
||||
}
|
||||
|
||||
int type_;
|
||||
acl::string cmd_;
|
||||
std::list<user1*> data_;
|
||||
};
|
78
app/gson/test1/struct.stub
Normal file
78
app/gson/test1/struct.stub
Normal file
@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
struct user
|
||||
{
|
||||
user(const char* username, const char* domain, int age, bool male)
|
||||
: username_(username)
|
||||
, domain_(domain)
|
||||
, age_(age)
|
||||
, male_(male)
|
||||
{}
|
||||
|
||||
user() {}
|
||||
~user() {}
|
||||
|
||||
acl::string username_;
|
||||
acl::string domain_;
|
||||
int age_;
|
||||
bool male_;
|
||||
};
|
||||
|
||||
struct message
|
||||
{
|
||||
int type_;
|
||||
acl::string cmd_;
|
||||
std::list<user> data_;
|
||||
};
|
||||
|
||||
struct user1
|
||||
{
|
||||
user1(const char* username, const char* domain, int age, bool male)
|
||||
{
|
||||
size_t n = strlen(username);
|
||||
username_ = new char[n + 1];
|
||||
memcpy(username_, username, n);
|
||||
username_[n] = 0;
|
||||
|
||||
n = strlen(domain);
|
||||
domain_ = new char[n + 1];
|
||||
memcpy(domain_, domain, n);
|
||||
domain_[n] = 0;
|
||||
age_ = age;
|
||||
male_ = male;
|
||||
}
|
||||
|
||||
user1()
|
||||
{
|
||||
username_ = NULL;
|
||||
domain_ = NULL;
|
||||
}
|
||||
|
||||
~user1()
|
||||
{
|
||||
delete username_;
|
||||
delete domain_;
|
||||
}
|
||||
|
||||
char* username_;
|
||||
char* domain_;
|
||||
int age_;
|
||||
bool male_;
|
||||
};
|
||||
|
||||
struct message1
|
||||
{
|
||||
message1()
|
||||
{
|
||||
}
|
||||
|
||||
~message1()
|
||||
{
|
||||
for (auto it = data_.begin(); it != data_.end(); ++it)
|
||||
delete *it;
|
||||
}
|
||||
|
||||
int type_;
|
||||
acl::string cmd_;
|
||||
std::list<user1*> data_;
|
||||
};
|
20
app/gson/test1/util.cpp
Normal file
20
app/gson/test1/util.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include "stdafx.h"
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include "util.h"
|
||||
|
||||
double util::stamp_sub(const struct timeval *from, const struct timeval *sub_by)
|
||||
{
|
||||
struct timeval res;
|
||||
|
||||
memcpy(&res, from, sizeof(struct timeval));
|
||||
|
||||
res.tv_usec -= sub_by->tv_usec;
|
||||
if (res.tv_usec < 0) {
|
||||
--res.tv_sec;
|
||||
res.tv_usec += 1000000;
|
||||
}
|
||||
res.tv_sec -= sub_by->tv_sec;
|
||||
|
||||
return (res.tv_sec * 1000.0 + res.tv_usec/1000.0);
|
||||
}
|
14
app/gson/test1/util.h
Normal file
14
app/gson/test1/util.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef MINGW
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
class util
|
||||
{
|
||||
public:
|
||||
util() {}
|
||||
~util() {}
|
||||
|
||||
static double stamp_sub(const struct timeval *from, const struct timeval *sub_by);
|
||||
};
|
3
app/gson/test1/valgrind.sh
Normal file
3
app/gson/test1/valgrind.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
|
||||
valgrind --tool=memcheck --leak-check=yes -v ./json -n 10000
|
Loading…
Reference in New Issue
Block a user