Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. UINT ThreadTest(LPVOID pParam);
  2.  
  3. void CMFCApplication5Dlg::OnBnClickedButton1()
  4. {
  5. // TODO: 여기에 컨트롤 알림 처리기 코드를 추가합니다.
  6.  
  7. int i;
  8.  
  9. for (i = 0; i < 80; i++)
  10. {
  11. AfxBeginThread(ThreadTest, (LPVOID)(i+1));
  12. }
  13. }
  14.  
  15. #define exit_nicely(conn) \
  16. {\
  17. PQfinish(conn);\
  18. return 0; \
  19. }
  20.  
  21.  
  22. UINT ThreadTest(LPVOID pParam)
  23. {
  24. const char *conninfo;
  25. PGconn *conn;
  26. PGresult *res;
  27. int nFields;
  28. int i, j;
  29. int nIndex = (int)pParam;
  30.  
  31. char *pData = "INSERT INTO posts (id, meta)"
  32. "VALUES("
  33. "%d,"
  34. "'{ "
  35. "\"author\": \"outsider\","
  36. "\"authorId\" : 43434,"
  37. "\"sns\" : {"
  38. "\"facebook\": \"https://facebook.com\","
  39. "\"twitter\" : \"https://twitter.com\""
  40. "},"
  41. "\"createAt\" : \"2014-06-14\","
  42. "\"category\" : [\"pg\", \"tech\", \"dev\"]"
  43. "}'"
  44. "); ";
  45.  
  46. char szData[2048];
  47.  
  48. /*
  49. * If the user supplies a parameter on the command line, use it as the
  50. * conninfo string; otherwise default to setting dbname=postgres and using
  51. * environment variables or defaults for all other connection parameters.
  52. */
  53.  
  54. conninfo = "dbname = postgres user = postgres password = *****";
  55.  
  56. /* Make a connection to the database */
  57. conn = PQconnectdb(conninfo);
  58.  
  59. /* Check to see that the backend connection was successfully made */
  60. if (PQstatus(conn) != CONNECTION_OK)
  61. {
  62. fprintf(stderr, "Connection to database failed: %s",
  63. PQerrorMessage(conn));
  64. exit_nicely(conn);
  65. }
  66.  
  67. /*
  68. * Our test case here involves using a cursor, for which we must be inside
  69. * a transaction block. We could do the whole thing with a single
  70. * PQexec() of "select * from pg_database", but that's too trivial to make
  71. * a good example.
  72. */
  73.  
  74. /* Start a transaction block */
  75. res = PQexec(conn, "BEGIN");
  76. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  77. {
  78. fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
  79. PQclear(res);
  80. exit_nicely(conn);
  81. }
  82.  
  83.  
  84. /*
  85. * Should PQclear PGresult whenever it is no longer needed to avoid memory
  86. * leaks
  87. */
  88. PQclear(res);
  89.  
  90.  
  91. //printf("safe : %d\n", PQisthreadsafe());
  92.  
  93. PQregisterThreadLock()
  94. res = PQexec(conn, "SELECT COUNT(*) FROM pg_tables WHERE schemaname='public' AND tablename='posts';");
  95. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  96. {
  97. fprintf(stderr, "SELECT COUNT(*) FROM pg_tables: %s", PQerrorMessage(conn));
  98. PQclear(res);
  99. exit_nicely(conn);
  100. }
  101. int nTable = atoi(PQgetvalue(res, 0, 0));
  102. PQclear(res);
  103.  
  104.  
  105. if (nTable == 0)
  106. {
  107. res = PQexec(conn, "CREATE TABLE posts(id serial NOT NULL, meta JSON);");
  108. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  109. {
  110. fprintf(stderr, "CREATE TABLE posts failed: %s", PQerrorMessage(conn));
  111. PQclear(res);
  112. exit_nicely(conn);
  113. }
  114. PQclear(res);
  115. }
  116.  
  117. // 시작, 끝, 주파수를 얻을 구조체를 선언한다.
  118. LARGE_INTEGER start, end, f;
  119.  
  120. // 고해상도 타이머의 주파수를 얻는다.
  121. QueryPerformanceFrequency(&f);
  122.  
  123. // 시작 시점의 CPU 클럭수를 얻는다.
  124. QueryPerformanceCounter(&start);
  125.  
  126.  
  127. sprintf_s(szData, sizeof(szData), pData, nIndex);
  128.  
  129. res = PQexec(conn, szData);
  130. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  131. {
  132. fprintf(stderr, "Insert posts failed: %s", PQerrorMessage(conn));
  133. PQclear(res);
  134. exit_nicely(conn);
  135. }
  136. PQclear(res);
  137.  
  138. // 끝 시점의 클럭수를 얻는다.
  139. QueryPerformanceCounter(&end);
  140.  
  141. // 끝 시점의 CPU 클럭수에서 시작 시점의 클럭수를 뺀 후 주파수를 1000으로 나눈 값을 나눈다.
  142. // 1초 기준의 주파수를 1000 으로 나누었기 때문에 1밀리초 동안 발생하는 진동수로 나눈 셈이다.
  143. __int64 ms_interval = (end.QuadPart - start.QuadPart) / (f.QuadPart / 1000);
  144.  
  145. // 끝 시점의 CPU 클럭수에서 시작 시점의 클럭수를 뺀 후 주파수에서 1000000으로 나눈 값을 나눈다.
  146. // 1초 기준의 주파수를 1000000 으로 나누었기 때문에 1마이크로초 동안 발생하는 진동수로 나눈 셈이다.
  147. __int64 micro_interval = (end.QuadPart - start.QuadPart) / (f.QuadPart / 1000000);
  148.  
  149. CString str;
  150. // 결과 값을 밀리초와 마이크로초로 출력한다.
  151. str.Format(_T("millisecond : %d, microsecond : %d\n"), (int)ms_interval, (int)micro_interval);
  152. TRACE(str);
  153. #if 0
  154. /*
  155. * Fetch rows from pg_database, the system catalog of databases
  156. */
  157. res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
  158. if (PQresultStatus(res) != PGRES_COMMAND_OK)
  159. {
  160. fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
  161. PQclear(res);
  162. exit_nicely(conn);
  163. }
  164. PQclear(res);
  165.  
  166. res = PQexec(conn, "FETCH ALL in myportal");
  167. if (PQresultStatus(res) != PGRES_TUPLES_OK)
  168. {
  169. fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
  170. PQclear(res);
  171. exit_nicely(conn);
  172. }
  173.  
  174. /* first, print out the attribute names */
  175. nFields = PQnfields(res);
  176. for (i = 0; i < nFields; i++)
  177. printf("%-15s", PQfname(res, i));
  178. printf("\n\n");
  179.  
  180. /* next, print out the rows */
  181. for (i = 0; i < PQntuples(res); i++)
  182. {
  183. for (j = 0; j < nFields; j++)
  184. printf("%-15s", PQgetvalue(res, i, j));
  185. printf("\n");
  186. }
  187.  
  188. PQclear(res);
  189.  
  190. /* close the portal ... we don't bother to check for errors ... */
  191. res = PQexec(conn, "CLOSE myportal");
  192. PQclear(res);
  193.  
  194. #endif
  195.  
  196. /* end the transaction */
  197. res = PQexec(conn, "END");
  198. PQclear(res);
  199.  
  200. /* close the connection to the database and cleanup */
  201. PQfinish(conn);
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement