Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UINT ThreadTest(LPVOID pParam);
- void CMFCApplication5Dlg::OnBnClickedButton1()
- {
- // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
- int i;
- for (i = 0; i < 80; i++)
- {
- AfxBeginThread(ThreadTest, (LPVOID)(i+1));
- }
- }
- #define exit_nicely(conn) \
- {\
- PQfinish(conn);\
- return 0; \
- }
- UINT ThreadTest(LPVOID pParam)
- {
- const char *conninfo;
- PGconn *conn;
- PGresult *res;
- int nFields;
- int i, j;
- int nIndex = (int)pParam;
- char *pData = "INSERT INTO posts (id, meta)"
- "VALUES("
- "%d,"
- "'{ "
- "\"author\": \"outsider\","
- "\"authorId\" : 43434,"
- "\"sns\" : {"
- "\"facebook\": \"https://facebook.com\","
- "\"twitter\" : \"https://twitter.com\""
- "},"
- "\"createAt\" : \"2014-06-14\","
- "\"category\" : [\"pg\", \"tech\", \"dev\"]"
- "}'"
- "); ";
- char szData[2048];
- /*
- * If the user supplies a parameter on the command line, use it as the
- * conninfo string; otherwise default to setting dbname=postgres and using
- * environment variables or defaults for all other connection parameters.
- */
- conninfo = "dbname = postgres user = postgres password = *****";
- /* Make a connection to the database */
- conn = PQconnectdb(conninfo);
- /* Check to see that the backend connection was successfully made */
- if (PQstatus(conn) != CONNECTION_OK)
- {
- fprintf(stderr, "Connection to database failed: %s",
- PQerrorMessage(conn));
- exit_nicely(conn);
- }
- /*
- * Our test case here involves using a cursor, for which we must be inside
- * a transaction block. We could do the whole thing with a single
- * PQexec() of "select * from pg_database", but that's too trivial to make
- * a good example.
- */
- /* Start a transaction block */
- res = PQexec(conn, "BEGIN");
- if (PQresultStatus(res) != PGRES_COMMAND_OK)
- {
- fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- /*
- * Should PQclear PGresult whenever it is no longer needed to avoid memory
- * leaks
- */
- PQclear(res);
- //printf("safe : %d\n", PQisthreadsafe());
- PQregisterThreadLock()
- res = PQexec(conn, "SELECT COUNT(*) FROM pg_tables WHERE schemaname='public' AND tablename='posts';");
- if (PQresultStatus(res) != PGRES_TUPLES_OK)
- {
- fprintf(stderr, "SELECT COUNT(*) FROM pg_tables: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- int nTable = atoi(PQgetvalue(res, 0, 0));
- PQclear(res);
- if (nTable == 0)
- {
- res = PQexec(conn, "CREATE TABLE posts(id serial NOT NULL, meta JSON);");
- if (PQresultStatus(res) != PGRES_COMMAND_OK)
- {
- fprintf(stderr, "CREATE TABLE posts failed: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- PQclear(res);
- }
- // 시작, 끝, 주파수를 얻을 구조체를 선언한다.
- LARGE_INTEGER start, end, f;
- // 고해상도 타이머의 주파수를 얻는다.
- QueryPerformanceFrequency(&f);
- // 시작 시점의 CPU 클럭수를 얻는다.
- QueryPerformanceCounter(&start);
- sprintf_s(szData, sizeof(szData), pData, nIndex);
- res = PQexec(conn, szData);
- if (PQresultStatus(res) != PGRES_COMMAND_OK)
- {
- fprintf(stderr, "Insert posts failed: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- PQclear(res);
- // 끝 시점의 클럭수를 얻는다.
- QueryPerformanceCounter(&end);
- // 끝 시점의 CPU 클럭수에서 시작 시점의 클럭수를 뺀 후 주파수를 1000으로 나눈 값을 나눈다.
- // 1초 기준의 주파수를 1000 으로 나누었기 때문에 1밀리초 동안 발생하는 진동수로 나눈 셈이다.
- __int64 ms_interval = (end.QuadPart - start.QuadPart) / (f.QuadPart / 1000);
- // 끝 시점의 CPU 클럭수에서 시작 시점의 클럭수를 뺀 후 주파수에서 1000000으로 나눈 값을 나눈다.
- // 1초 기준의 주파수를 1000000 으로 나누었기 때문에 1마이크로초 동안 발생하는 진동수로 나눈 셈이다.
- __int64 micro_interval = (end.QuadPart - start.QuadPart) / (f.QuadPart / 1000000);
- CString str;
- // 결과 값을 밀리초와 마이크로초로 출력한다.
- str.Format(_T("millisecond : %d, microsecond : %d\n"), (int)ms_interval, (int)micro_interval);
- TRACE(str);
- #if 0
- /*
- * Fetch rows from pg_database, the system catalog of databases
- */
- res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
- if (PQresultStatus(res) != PGRES_COMMAND_OK)
- {
- fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- PQclear(res);
- res = PQexec(conn, "FETCH ALL in myportal");
- if (PQresultStatus(res) != PGRES_TUPLES_OK)
- {
- fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
- PQclear(res);
- exit_nicely(conn);
- }
- /* first, print out the attribute names */
- nFields = PQnfields(res);
- for (i = 0; i < nFields; i++)
- printf("%-15s", PQfname(res, i));
- printf("\n\n");
- /* next, print out the rows */
- for (i = 0; i < PQntuples(res); i++)
- {
- for (j = 0; j < nFields; j++)
- printf("%-15s", PQgetvalue(res, i, j));
- printf("\n");
- }
- PQclear(res);
- /* close the portal ... we don't bother to check for errors ... */
- res = PQexec(conn, "CLOSE myportal");
- PQclear(res);
- #endif
- /* end the transaction */
- res = PQexec(conn, "END");
- PQclear(res);
- /* close the connection to the database and cleanup */
- PQfinish(conn);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement