多线程下Lock、Level-Lock、Lock-Free、ReaderWriterLock、ReaderWriterLockSlim性能比较

多线程环境下使用那种锁往往凭个人感觉,缺乏测试数据的支持很容易走入误区,就像我知道的很多人就觉得Lock好慢好慢,Lock-Free 就能飞起来一样。

 

下面对Lock、Level-Lock、Lock-Free、ReaderWriterLock、ReaderWriterLockSlim 这几种锁分别做下性能测试,先说明一下:

1. Lock lock关键字,排他锁

 

2. Level-Lock  就是对lock数据分块加锁,大并发时可很大降低锁的可能性

3. Lock-Free 如InterLocked 利用CPU的原子操作,CAS,Spin Lock技巧实现,一般比较复杂(这里我们使用.Net 4中的System.Collection.Concurrent.ConcurrentDictionary<,>实现)

4. ReaderWriterLock  unmananged 读写锁

5. ReaderWriterLockSlim .Net 4 中managed 读写锁

 

测试环境:T420 (I5 2520M/2.5G/8G)

测试环境比较重要,我机器CPU只是2核4线程,而现网跑的基本在16核以上。因此多线程测试和实际效果可能会有很大差距,测试数据仅仅供参考。

下面提供测试代码,有条件同学如果能弄台好点的测试机测试下,共享下结果那就不胜感激。

 

下面是测试结果:

下面是结果:(1000W的字典,做1000W次操作)
1. Dictionary 单线程操作效率:

read_*    2337ms   10000000 items  4278990 items/s
read_0    2337ms   10000000 items  4278990 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    2856ms   10000000 items  3501400 items/s
write_0    2856ms   10000000 items  3501400 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    2207ms   10000000 items  4531037 items/s
delete_0    2207ms   10000000 items  4531037 items/s
--+------+-----+--------+--------+-------+---------+----


单线程,基本都在400W次/s. CPU 占用25%。
 
2. Lock Dictionary
 

read_*    2560ms   10000000 items  3906250 items/s
read_0    2560ms   10000000 items  3906250 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    3150ms   10000000 items  3174603 items/s
write_0    3150ms   10000000 items  3174603 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    2364ms   10000000 items  4230118 items/s
delete_0    2364ms   10000000 items  4230118 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2959ms   3224949 items  1089877 items/s
read_0    2959ms   3224949 items  1089877 items/s
--+------+-----+------
delete_*    2947ms   3300323 items  1119892 items/s
delete_0    2947ms   3300323 items  1119892 items/s
--+------+-----+------
write_*    2953ms   3474728 items  1176677 items/s
write_0    2953ms   3474728 items  1176677 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2024ms   3985786 items  1969261 items/s
read_0    2024ms   2378444 items  1175120 items/s
read_1    2024ms   1607342 items  794141 items/s
--+------+-----+------
delete_*    2013ms   3364222 items  1671247 items/s
delete_0    2016ms   2479708 items  1230013 items/s
delete_1    2011ms   884514 items  439837 items/s
--+------+-----+------
write_*    2022ms   2649992 items  1310579 items/s
write_0    2024ms   1313185 items  648806 items/s
write_1    2021ms   1336807 items  661458 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    3152ms   3073497 items  975094 items/s
write_0    3159ms   877536 items  277789 items/s
write_1    3154ms   965414 items  306091 items/s
write_2    3144ms   1230547 items  391395 items/s
--+------+-----+------
delete_*    3091ms   4149466 items  1342434 items/s
delete_0    3106ms   961547 items  309577 items/s
delete_1    3087ms   811856 items  262991 items/s
delete_2    3080ms   2376063 items  771449 items/s
--+------+-----+------
read_*    3169ms   2777037 items  876313 items/s
read_0    3175ms   791828 items  249394 items/s
read_1    3169ms   908187 items  286584 items/s
read_2    3165ms   1077022 items  340291 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2673ms   10000000 items  3741114 items/s
read_0    2674ms   4928616 items  1843162 items/s
read_1    2673ms   5071384 items  1897263 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2809ms   10000000 items  3559985 items/s
read_0    2814ms   3109810 items  1105120 items/s
read_1    2808ms   3570716 items  1271622 items/s
read_2    2806ms   3319474 items  1182991 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3063ms   10000000 items  3264773 items/s
read_0    3074ms   2422274 items  787987 items/s
read_1    3069ms   2804878 items  913938 items/s
read_2    3058ms   2364131 items  773097 items/s
read_3    3053ms   2408717 items  788967 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3342ms   10000000 items  2992220 items/s
read_0    3361ms   1192407 items  354777 items/s
read_1    3356ms   2872306 items  855871 items/s
read_2    3349ms   2236479 items  667805 items/s
read_3    3336ms   842120 items  252434 items/s
read_4    3330ms   815203 items  244805 items/s
read_5    3321ms   2041485 items  614719 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3143ms   10000000 items  3181673 items/s
read_0    3166ms   1211817 items  382759 items/s
read_1    3166ms   299314 items  94540 items/s
read_2    3166ms   1140744 items  360310 items/s
read_3    3159ms   748299 items  236878 items/s
read_4    3151ms   904550 items  287067 items/s
read_5    3144ms   252157 items  80202 items/s
read_6    3132ms   3109588 items  992844 items/s
read_7    3131ms   921935 items  294453 items/s
read_8    3114ms   1159175 items  372246 items/s
read_9    3107ms   252421 items  81242 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3145ms   8816919 items  2803471 items/s
read_0    3154ms   652354 items  206833 items/s
read_1    3154ms   725380 items  229987 items/s
read_2    3154ms   593207 items  188080 items/s
read_3    3154ms   2437985 items  772981 items/s
read_4    3152ms   648044 items  205597 items/s
read_5    3151ms   710051 items  225341 items/s
read_6    3151ms   1159138 items  367863 items/s
read_7    3148ms   599157 items  190329 items/s
read_8    3121ms   616668 items  197586 items/s
read_9    3114ms   674935 items  216742 items/s
--+------+-----+------
write_*    3101ms   1183081 items  381515 items/s
write_0    3101ms   1183081 items  381515 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2965ms   9524695 items  3212376 items/s
read_0    3111ms   692907 items  222728 items/s
read_1    3104ms   535677 items  172576 items/s
read_2    3088ms   528330 items  171091 items/s
read_3    3082ms   454461 items  147456 items/s
read_4    3046ms   499212 items  163891 items/s
read_5    3010ms   487582 items  161987 items/s
read_6    2991ms   505601 items  169040 items/s
read_7    2988ms   406797 items  136143 items/s
read_8    2988ms   532080 items  178072 items/s
read_9    2978ms   558057 items  187393 items/s
read_10    2956ms   409198 items  138429 items/s
read_11    2948ms   328490 items  111428 items/s
read_12    2939ms   507699 items  172745 items/s
read_13    2930ms   458041 items  156327 items/s
read_14    2913ms   468107 items  160695 items/s
read_15    2892ms   412851 items  142756 items/s
read_16    2879ms   484964 items  168448 items/s
read_17    2849ms   467826 items  164207 items/s
read_18    2822ms   419699 items  148723 items/s
read_19    2800ms   367116 items  131112 items/s
--+------+-----+------
write_*    2790ms   475305 items  170360 items/s
write_0    2790ms   475305 items  170360 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3069ms   5479136 items  1785316 items/s
read_0    3152ms   233205 items  73986 items/s
read_1    3152ms   443136 items  140588 items/s
read_2    3152ms   357287 items  113352 items/s
read_3    3146ms   223279 items  70972 items/s
read_4    3136ms   201622 items  64292 items/s
read_5    3131ms   324597 items  103671 items/s
read_6    3113ms   251971 items  80941 items/s
read_7    3109ms   205629 items  66139 items/s
read_8    3096ms   192179 items  62073 items/s
read_9    3088ms   304387 items  98570 items/s
read_10    3056ms   262619 items  85935 items/s
read_11    3054ms   299693 items  98131 items/s
read_12    3044ms   221596 items  72797 items/s
read_13    3023ms   277341 items  91743 items/s
read_14    3015ms   291290 items  96613 items/s
read_15    3009ms   257143 items  85457 items/s
read_16    2989ms   251629 items  84185 items/s
read_17    2985ms   366428 items  122756 items/s
read_18    2973ms   206825 items  69567 items/s
read_19    2964ms   307280 items  103670 items/s
--+------+-----+------
delete_*    2449ms   1952409 items  797227 items/s
delete_0    2645ms   271151 items  102514 items/s
delete_1    2625ms   225453 items  85886 items/s
delete_2    2561ms   142052 items  55467 items/s
delete_3    2530ms   223046 items  88160 items/s
delete_4    2480ms   181484 items  73179 items/s
delete_5    2435ms   184849 items  75913 items/s
delete_6    2383ms   141900 items  59546 items/s
delete_7    2322ms   196285 items  84532 items/s
delete_8    2276ms   187952 items  82579 items/s
delete_9    2240ms   198237 items  88498 items/s
--+------+-----+------
write_*    2822ms   2568455 items  910154 items/s
write_0    2937ms   203495 items  69286 items/s
write_1    2901ms   314688 items  108475 items/s
write_2    2891ms   290887 items  100618 items/s
write_3    2879ms   308520 items  107162 items/s
write_4    2841ms   190565 items  67076 items/s
write_5    2815ms   249638 items  88681 items/s
write_6    2808ms   220699 items  78596 items/s
write_7    2774ms   295309 items  106456 items/s
write_8    2717ms   216728 items  79767 items/s
write_9    2662ms   277926 items  104404 items/s
--+------+-----+--------+--------+-------+---------+----


单线程下,效率和无锁情况下效率稍差,影响不大;在很高线程并发情况下也能达到300w次/s 的处理能力,当然基于互斥操作不可能超过单线程的速度。
单线程下CPU 25%,随着线程数增加CPU占用很容易到达100%,太多的锁竞争占用大量的CPU资源。
 
3. Level-Lock Dictionary

1.Level=20( 也就是20lock,20个dict)
 

read_*    2710ms   10000000 items  3690036 items/s
read_0    2710ms   10000000 items  3690036 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    2963ms   10000000 items  3374957 items/s
write_0    2963ms   10000000 items  3374957 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    2477ms   10000000 items  4037141 items/s
delete_0    2477ms   10000000 items  4037141 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    1398ms   3909706 items  2796642 items/s
write_0    1398ms   3909706 items  2796642 items/s
--+------+-----+------
delete_*    1398ms   2482589 items  1775814 items/s
delete_0    1398ms   2482589 items  1775814 items/s
--+------+-----+------
read_*    1398ms   3607705 items  2580618 items/s
read_0    1398ms   3607705 items  2580618 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    1258ms   2511943 items  1996775 items/s
delete_0    1340ms   906632 items  676591 items/s
delete_1    1177ms   1605311 items  1363900 items/s
--+------+-----+------
write_*    1484ms   4145435 items  2793419 items/s
write_0    1529ms   1212737 items  793156 items/s
write_1    1440ms   2932698 items  2036595 items/s
--+------+-----+------
read_*    1564ms   3342622 items  2137226 items/s
read_0    1564ms   1879593 items  1201785 items/s
read_1    1564ms   1463029 items  935440 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1483ms   2785038 items  1877975 items/s
read_0    1491ms   1283044 items  860525 items/s
read_1    1491ms   591404 items  396649 items/s
read_2    1469ms   910590 items  619870 items/s
--+------+-----+------
write_*    1324ms   4480945 items  3384399 items/s
write_0    1379ms   1783564 items  1293374 items/s
write_1    1310ms   1010326 items  771241 items/s
write_2    1283ms   1687055 items  1314929 items/s
--+------+-----+------
delete_*    1104ms   2734017 items  2476464 items/s
delete_0    1160ms   888647 items  766075 items/s
delete_1    1125ms   1121904 items  997248 items/s
delete_2    1028ms   723466 items  703760 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1322ms   10000000 items  7564296 items/s
read_0    1322ms   5194179 items  3929031 items/s
read_1    1322ms   4805821 items  3635265 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1090ms   10000000 items  9174311 items/s
read_0    1103ms   3921221 items  3555050 items/s
read_1    1103ms   3312706 items  3003359 items/s
read_2    1064ms   2766073 items  2599692 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1594ms   10000000 items  6273525 items/s
read_0    1634ms   3093719 items  1893340 items/s
read_1    1634ms   2374941 items  1453452 items/s
read_2    1602ms   2188744 items  1366257 items/s
read_3    1506ms   2342596 items  1555508 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1551ms   10000000 items  6447453 items/s
read_0    1640ms   2985340 items  1820329 items/s
read_1    1640ms   1066411 items  650250 items/s
read_2    1592ms   1648232 items  1035321 items/s
read_3    1509ms   2514308 items  1666208 items/s
read_4    1489ms   1131922 items  760189 items/s
read_5    1436ms   653787 items  455283 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1382ms   10000000 items  7235890 items/s
read_0    1671ms   1619683 items  969289 items/s
read_1    1666ms   1535556 items  921702 items/s
read_2    1628ms   1418188 items  871122 items/s
read_3    1540ms   1374893 items  892787 items/s
read_4    1474ms   536288 items  363831 items/s
read_5    1448ms   717284 items  495361 items/s
read_6    1325ms   851410 items  642573 items/s
read_7    1213ms   958245 items  789979 items/s
read_8    1052ms   638253 items  606704 items/s
read_9    811ms   350200 items  431812 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1299ms   9411579 items  7245249 items/s
read_0    1411ms   890482 items  631099 items/s
read_1    1411ms   1407221 items  997321 items/s
read_2    1411ms   1242786 items  880783 items/s
read_3    1405ms   1668207 items  1187335 items/s
read_4    1370ms   989863 items  722527 items/s
read_5    1313ms   340363 items  259225 items/s
read_6    1287ms   754871 items  586535 items/s
read_7    1177ms   719554 items  611345 items/s
read_8    1172ms   644930 items  550281 items/s
read_9    1035ms   753302 items  727828 items/s
--+------+-----+------
write_*    819ms   588421 items  718462 items/s
write_0    819ms   588421 items  718462 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    695ms   10000000 items  14388489 items/s
read_0    1525ms   906637 items  594516 items/s
read_1    1525ms   1568371 items  1028440 items/s
read_2    1489ms   1862496 items  1250836 items/s
read_3    1394ms   699952 items  502117 items/s
read_4    1393ms   948093 items  680612 items/s
read_5    1381ms   578078 items  418593 items/s
read_6    1223ms   670020 items  547849 items/s
read_7    1159ms   1083856 items  935164 items/s
read_8    1019ms   418721 items  410913 items/s
read_9    732ms   519766 items  710062 items/s
read_10    584ms   357169 items  611590 items/s
read_11    378ms   229095 items  606071 items/s
read_12    117ms   157746 items  1348256 items/s
read_13    0ms   0 items  oooo items/s
read_14    0ms   0 items  oooo items/s
read_15    0ms   0 items  oooo items/s
read_16    0ms   0 items  oooo items/s
read_17    0ms   0 items  oooo items/s
read_18    0ms   0 items  oooo items/s
read_19    0ms   0 items  oooo items/s
--+------+-----+------
write_*    0ms   0 items  oooo items/s
write_0    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----
read_*    733ms   10000000 items  13642564 items/s
read_0    1578ms   1382188 items  875911 items/s
read_1    1573ms   1253791 items  797069 items/s
read_2    1533ms   844080 items  550606 items/s
read_3    1450ms   1400782 items  966056 items/s
read_4    1447ms   787415 items  544170 items/s
read_5    1353ms   762434 items  563513 items/s
read_6    1248ms   1113228 items  892009 items/s
read_7    1132ms   981544 items  867088 items/s
read_8    981ms   449161 items  457860 items/s
read_9    839ms   187875 items  223927 items/s
read_10    668ms   404216 items  605113 items/s
read_11    460ms   213684 items  464530 items/s
read_12    307ms   66234 items  215745 items/s
read_13    108ms   153368 items  1420074 items/s
read_14    0ms   0 items  oooo items/s
read_15    0ms   0 items  oooo items/s
read_16    0ms   0 items  oooo items/s
read_17    0ms   0 items  oooo items/s
read_18    0ms   0 items  oooo items/s
read_19    0ms   0 items  oooo items/s
--+------+-----+------
write_*    0ms   0 items  oooo items/s
write_0    0ms   0 items  oooo items/s
write_1    0ms   0 items  oooo items/s
write_2    0ms   0 items  oooo items/s
write_3    0ms   0 items  oooo items/s
write_4    0ms   0 items  oooo items/s
write_5    0ms   0 items  oooo items/s
write_6    0ms   0 items  oooo items/s
write_7    0ms   0 items  oooo items/s
write_8    0ms   0 items  oooo items/s
write_9    0ms   0 items  oooo items/s
--+------+-----+------
delete_*    0ms   0 items  oooo items/s
delete_0    0ms   0 items  oooo items/s
delete_1    0ms   0 items  oooo items/s
delete_2    0ms   0 items  oooo items/s
delete_3    0ms   0 items  oooo items/s
delete_4    0ms   0 items  oooo items/s
delete_5    0ms   0 items  oooo items/s
delete_6    0ms   0 items  oooo items/s
delete_7    0ms   0 items  oooo items/s
delete_8    0ms   0 items  oooo items/s
delete_9    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----


    处理能力多线程下能轻松超过1000w每秒,充分的利用了多核CPU的能力。
    CPU在40%~70%样子,因为Lock分区线程竞争较少,但CPU也无法100%的利用。
    2.Level=200( 也就是200lock,200个dict)
     相比Level=20 速度稍快,但不明显,CPU利用率高一些了。
 
4.Lock-Free 也许我用System.ConCurrentDictionary不太合适,因为这并不是一个很理想的实现,不过dict实现成这样也算难得。
 

read_*    2640ms   10000000 items  3787878 items/s
read_0    2640ms   10000000 items  3787878 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    2877ms   10000000 items  3475842 items/s
write_0    2877ms   10000000 items  3475842 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    2441ms   10000000 items  4096681 items/s
delete_0    2441ms   10000000 items  4096681 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    1271ms   2516657 items  1980060 items/s
delete_0    1271ms   2516657 items  1980060 items/s
--+------+-----+------
write_*    1282ms   2634093 items  2054674 items/s
write_0    1282ms   2634093 items  2054674 items/s
--+------+-----+------
read_*    1283ms   4849250 items  3779618 items/s
read_0    1283ms   4849250 items  3779618 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    1247ms   2150275 items  1724358 items/s
delete_0    1274ms   1551075 items  1217484 items/s
delete_1    1221ms   599200 items  490745 items/s
--+------+-----+------
read_*    1380ms   2173328 items  1574875 items/s
read_0    1383ms   666282 items  481765 items/s
read_1    1377ms   1507046 items  1094441 items/s
--+------+-----+------
write_*    1346ms   5676397 items  4217234 items/s
write_0    1371ms   3864136 items  2818479 items/s
write_1    1322ms   1812261 items  1370847 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1398ms   3266302 items  2336410 items/s
read_0    1405ms   701388 items  499208 items/s
read_1    1405ms   1183968 items  842681 items/s
read_2    1386ms   1380946 items  996353 items/s
--+------+-----+------
delete_*    1113ms   2539759 items  2281903 items/s
delete_0    1160ms   1202885 items  1036969 items/s
delete_1    1105ms   667508 items  604079 items/s
delete_2    1076ms   669366 items  622087 items/s
--+------+-----+------
write_*    1268ms   4193939 items  3307522 items/s
write_0    1328ms   1897418 items  1428778 items/s
write_1    1277ms   760482 items  595522 items/s
write_2    1200ms   1536039 items  1280032 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1120ms   10000000 items  8928571 items/s
read_0    1121ms   5199541 items  4638305 items/s
read_1    1120ms   4800459 items  4286124 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1159ms   10000000 items  8628127 items/s
read_0    1173ms   3445348 items  2937210 items/s
read_1    1168ms   3450978 items  2954604 items/s
read_2    1136ms   3103674 items  2732107 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1093ms   10000000 items  9149130 items/s
read_0    1123ms   2283910 items  2033757 items/s
read_1    1123ms   2982767 items  2656070 items/s
read_2    1100ms   2821584 items  2565076 items/s
read_3    1027ms   1911739 items  1861479 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1041ms   10000000 items  9606147 items/s
read_0    1113ms   1726655 items  1551352 items/s
read_1    1113ms   1536080 items  1380125 items/s
read_2    1079ms   2419089 items  2241973 items/s
read_3    1015ms   2292703 items  2258820 items/s
read_4    982ms   1084573 items  1104453 items/s
read_5    949ms   940900 items  991464 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1023ms   10000000 items  9775171 items/s
read_0    1159ms   1697628 items  1464735 items/s
read_1    1159ms   929654 items  802117 items/s
read_2    1125ms   839433 items  746162 items/s
read_3    1093ms   897699 items  821316 items/s
read_4    1062ms   1179313 items  1110464 items/s
read_5    1060ms   1098892 items  1036690 items/s
read_6    981ms   1072347 items  1093116 items/s
read_7    899ms   691808 items  769530 items/s
read_8    865ms   996749 items  1152310 items/s
read_9    827ms   596477 items  721253 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1033ms   9510098 items  9206290 items/s
read_0    1234ms   1433667 items  1161804 items/s
read_1    1228ms   1337598 items  1089249 items/s
read_2    1158ms   1613906 items  1393701 items/s
read_3    1075ms   832036 items  773986 items/s
read_4    1042ms   741827 items  711926 items/s
read_5    1003ms   768494 items  766195 items/s
read_6    989ms   609960 items  616744 items/s
read_7    930ms   961334 items  1033692 items/s
read_8    865ms   711270 items  822277 items/s
read_9    807ms   500006 items  619586 items/s
--+------+-----+------
write_*    764ms   489902 items  641232 items/s
write_0    764ms   489902 items  641232 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    715ms   10000000 items  13986013 items/s
read_0    1137ms   975731 items  858162 items/s
read_1    1136ms   1127366 items  992399 items/s
read_2    1116ms   1402088 items  1256351 items/s
read_3    1063ms   942533 items  886672 items/s
read_4    1031ms   456334 items  442612 items/s
read_5    984ms   811468 items  824662 items/s
read_6    931ms   514892 items  553052 items/s
read_7    891ms   645952 items  724974 items/s
read_8    868ms   112137 items  129190 items/s
read_9    774ms   497543 items  642820 items/s
read_10    773ms   461019 items  596402 items/s
read_11    677ms   300201 items  443428 items/s
read_12    659ms   294319 items  446614 items/s
read_13    584ms   350267 items  599772 items/s
read_14    486ms   308896 items  635588 items/s
read_15    468ms   474735 items  1014391 items/s
read_16    316ms   198826 items  629196 items/s
read_17    265ms   76570 items  288943 items/s
read_18    118ms   35726 items  302762 items/s
read_19    33ms   13397 items  405969 items/s
--+------+-----+------
write_*    0ms   0 items  oooo items/s
write_0    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----
write_*    42ms   136456 items  3248952 items/s
write_0    184ms   119755 items  650842 items/s
write_1    121ms   1 items  8 items/s
write_2    100ms   16699 items  166990 items/s
write_3    23ms   1 items  43 items/s
write_4    0ms   0 items  oooo items/s
write_5    0ms   0 items  oooo items/s
write_6    0ms   0 items  oooo items/s
write_7    0ms   0 items  oooo items/s
write_8    0ms   0 items  oooo items/s
write_9    0ms   0 items  oooo items/s
--+------+-----+------
read_*    883ms   9863544 items  11170491 items/s
read_0    1272ms   1055835 items  830058 items/s
read_1    1267ms   1127178 items  889643 items/s
read_2    1235ms   998866 items  808798 items/s
read_3    1203ms   906909 items  753872 items/s
read_4    1137ms   302782 items  266299 items/s
read_5    1123ms   599917 items  534209 items/s
read_6    1061ms   507196 items  478035 items/s
read_7    1049ms   487007 items  464258 items/s
read_8    1027ms   532442 items  518444 items/s
read_9    974ms   499217 items  512543 items/s
read_10    937ms   375075 items  400293 items/s
read_11    871ms   772866 items  887331 items/s
read_12    779ms   325711 items  418114 items/s
read_13    686ms   290248 items  423102 items/s
read_14    626ms   210467 items  336209 items/s
read_15    624ms   239014 items  383035 items/s
read_16    620ms   181094 items  292087 items/s
read_17    558ms   253303 items  453948 items/s
read_18    353ms   136337 items  386223 items/s
read_19    263ms   62080 items  236045 items/s
--+------+-----+------
delete_*    0ms   0 items  oooo items/s
delete_0    0ms   0 items  oooo items/s
delete_1    0ms   0 items  oooo items/s
delete_2    0ms   0 items  oooo items/s
delete_3    0ms   0 items  oooo items/s
delete_4    0ms   0 items  oooo items/s
delete_5    0ms   0 items  oooo items/s
delete_6    0ms   0 items  oooo items/s
delete_7    0ms   0 items  oooo items/s
delete_8    0ms   0 items  oooo items/s
delete_9    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----
 
    


  非常理想的并发读写能力,在并发处理上相比Level-Lock稍差,只读性能最高。
  CPU占用为一般在25%,偶尔能到50%,资源占用很低但效率很高。但为什么不能充分利用到所有CPU资源呢?

5.ReaderWriterLock

read_*    4103ms   10000000 items  2437241 items/s
read_0    4103ms   10000000 items  2437241 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    4771ms   10000000 items  2095996 items/s
write_0    4771ms   10000000 items  2095996 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    3852ms   10000000 items  2596053 items/s
delete_0    3852ms   10000000 items  2596053 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    3593ms   3252197 items  905148 items/s
read_0    3593ms   3252197 items  905148 items/s
--+------+-----+------
delete_*    3581ms   3221796 items  899691 items/s
delete_0    3581ms   3221796 items  899691 items/s
--+------+-----+------
write_*    3593ms   3526007 items  981354 items/s
write_0    3593ms   3526007 items  981354 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    13763ms   1371728 items  99667 items/s
delete_0    13764ms   685393 items  49796 items/s
delete_1    13763ms   686335 items  49868 items/s
--+------+-----+------
read_*    13771ms   7255696 items  526882 items/s
read_0    13772ms   3582785 items  260149 items/s
read_1    13771ms   3672911 items  266713 items/s
--+------+-----+------
write_*    13765ms   1372576 items  99714 items/s
write_0    13766ms   686261 items  49851 items/s
write_1    13764ms   686315 items  49863 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    10411ms   8822216 items  847393 items/s
read_0    10424ms   2917524 items  279885 items/s
read_1    10419ms   2966443 items  284714 items/s
read_2    10391ms   2938249 items  282768 items/s
--+------+-----+------
delete_*    10350ms   565551 items  54642 items/s
delete_0    10357ms   188790 items  18228 items/s
delete_1    10348ms   188066 items  18174 items/s
delete_2    10347ms   188695 items  18236 items/s
--+------+-----+------
write_*    10380ms   612233 items  58981 items/s
write_0    10391ms   219087 items  21084 items/s
write_1    10381ms   203108 items  19565 items/s
write_2    10369ms   190038 items  18327 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2029ms   10000000 items  4928536 items/s
read_0    2029ms   5029786 items  2478948 items/s
read_1    2029ms   4970214 items  2449587 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1766ms   10000000 items  5662514 items/s
read_0    1776ms   3378630 items  1902381 items/s
read_1    1771ms   3303765 items  1865479 items/s
read_2    1751ms   3317605 items  1894691 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1622ms   10000000 items  6165228 items/s
read_0    1636ms   2553554 items  1560852 items/s
read_1    1630ms   2432244 items  1492174 items/s
read_2    1616ms   2519038 items  1558810 items/s
read_3    1606ms   2495164 items  1553651 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1608ms   10000000 items  6218905 items/s
read_0    1658ms   1082180 items  652702 items/s
read_1    1653ms   1479498 items  895038 items/s
read_2    1629ms   2065451 items  1267925 items/s
read_3    1617ms   1754152 items  1084818 items/s
read_4    1555ms   1844924 items  1186446 items/s
read_5    1539ms   1773795 items  1152563 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1458ms   10000000 items  6858710 items/s
read_0    1638ms   1217195 items  743098 items/s
read_1    1638ms   856628 items  522971 items/s
read_2    1618ms   1072861 items  663078 items/s
read_3    1527ms   1150025 items  753127 items/s
read_4    1489ms   762001 items  511753 items/s
read_5    1475ms   974214 items  660484 items/s
read_6    1405ms   1253576 items  892224 items/s
read_7    1327ms   1009046 items  760396 items/s
read_8    1285ms   803676 items  625428 items/s
read_9    1179ms   900778 items  764018 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1603ms   9976045 items  6223359 items/s
read_0    1671ms   517471 items  309677 items/s
read_1    1671ms   1129825 items  676137 items/s
read_2    1665ms   1769584 items  1062813 items/s
read_3    1662ms   1332698 items  801864 items/s
read_4    1645ms   1037248 items  630545 items/s
read_5    1631ms   925329 items  567338 items/s
read_6    1617ms   719434 items  444918 items/s
read_7    1602ms   1101918 items  687838 items/s
read_8    1574ms   931664 items  591908 items/s
read_9    1298ms   510874 items  393585 items/s
--+------+-----+------
write_*    1283ms   23955 items  18671 items/s
write_0    1283ms   23955 items  18671 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    957ms   10000000 items  10449320 items/s
read_0    1656ms   818730 items  494402 items/s
read_1    1650ms   1024485 items  620900 items/s
read_2    1638ms   1177057 items  718594 items/s
read_3    1628ms   1378852 items  846960 items/s
read_4    1542ms   894173 items  579878 items/s
read_5    1514ms   698577 items  461411 items/s
read_6    1320ms   595306 items  450989 items/s
read_7    1149ms   432096 items  376062 items/s
read_8    1128ms   541190 items  479778 items/s
read_9    1107ms   489789 items  442447 items/s
read_10    1064ms   652728 items  613466 items/s
read_11    944ms   340095 items  360270 items/s
read_12    880ms   385007 items  437507 items/s
read_13    610ms   43901 items  71968 items/s
read_14    596ms   279953 items  469719 items/s
read_15    438ms   139778 items  319127 items/s
read_16    250ms   86036 items  344144 items/s
read_17    30ms   22247 items  741566 items/s
read_18    0ms   0 items  oooo items/s
read_19    0ms   0 items  oooo items/s
--+------+-----+------
write_*    0ms   0 items  oooo items/s
write_0    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----
read_*    885ms   10000000 items  11299435 items/s
read_0    1674ms   912649 items  545190 items/s
read_1    1668ms   675863 items  405193 items/s
read_2    1652ms   1275618 items  772165 items/s
read_3    1636ms   1078886 items  659465 items/s
read_4    1589ms   747903 items  470675 items/s
read_5    1545ms   990975 items  641407 items/s
read_6    1531ms   1116800 items  729457 items/s
read_7    1495ms   987623 items  660617 items/s
read_8    1481ms   559675 items  377903 items/s
read_9    1318ms   718009 items  544771 items/s
read_10    801ms   185021 items  230987 items/s
read_11    722ms   359341 items  497702 items/s
read_12    428ms   278895 items  651623 items/s
read_13    139ms   101845 items  732697 items/s
read_14    29ms   10897 items  375758 items/s
read_15    0ms   0 items  oooo items/s
read_16    0ms   0 items  oooo items/s
read_17    0ms   0 items  oooo items/s
read_18    0ms   0 items  oooo items/s
read_19    0ms   0 items  oooo items/s
--+------+-----+------
write_*    0ms   0 items  oooo items/s
write_0    0ms   0 items  oooo items/s
write_1    0ms   0 items  oooo items/s
write_2    0ms   0 items  oooo items/s
write_3    0ms   0 items  oooo items/s
write_4    0ms   0 items  oooo items/s
write_5    0ms   0 items  oooo items/s
write_6    0ms   0 items  oooo items/s
write_7    0ms   0 items  oooo items/s
write_8    0ms   0 items  oooo items/s
write_9    0ms   0 items  oooo items/s
--+------+-----+------
delete_*    0ms   0 items  oooo items/s
delete_0    0ms   0 items  oooo items/s
delete_1    0ms   0 items  oooo items/s
delete_2    0ms   0 items  oooo items/s
delete_3    0ms   0 items  oooo items/s
delete_4    0ms   0 items  oooo items/s
delete_5    0ms   0 items  oooo items/s
delete_6    0ms   0 items  oooo items/s
delete_7    0ms   0 items  oooo items/s
delete_8    0ms   0 items  oooo items/s
delete_9    0ms   0 items  oooo items/s
--+------+-----+--------+--------+-------+---------+----


   单线程下速度下降不少,并发读写时效率很低,只读或写少是效率很不错。
   CPU占用较高一般50%,部分时候到100%,系统调用还是比较占资源。

6.ReaderWriterLockSlim

read_*    2887ms   10000000 items  3463803 items/s
read_0    2887ms   10000000 items  3463803 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    3359ms   10000000 items  2977076 items/s
write_0    3359ms   10000000 items  2977076 items/s
--+------+-----+--------+--------+-------+---------+----
delete_*    2591ms   10000000 items  3859513 items/s
delete_0    2591ms   10000000 items  3859513 items/s
--+------+-----+--------+--------+-------+---------+----
write_*    2985ms   4718018 items  1580575 items/s
write_0    2985ms   4718018 items  1580575 items/s
--+------+-----+------
read_*    2985ms   2507512 items  840037 items/s
read_0    2985ms   2507512 items  840037 items/s
--+------+-----+------
delete_*    2985ms   2774470 items  929470 items/s
delete_0    2985ms   2774470 items  929470 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1964ms   7770275 items  3956351 items/s
read_0    1964ms   3837877 items  1954112 items/s
read_1    1964ms   3932398 items  2002239 items/s
--+------+-----+------
write_*    1951ms   1141754 items  585214 items/s
write_0    1954ms   591289 items  302604 items/s
write_1    1949ms   550465 items  282434 items/s
--+------+-----+------
delete_*    1949ms   1087971 items  558220 items/s
delete_0    1950ms   556340 items  285302 items/s
delete_1    1949ms   531631 items  272771 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1892ms   8653503 items  4573733 items/s
read_0    1897ms   2661828 items  1403177 items/s
read_1    1897ms   3211396 items  1692881 items/s
read_2    1882ms   2780279 items  1477300 items/s
--+------+-----+------
write_*    1869ms   690275 items  369328 items/s
write_0    1875ms   269204 items  143575 items/s
write_1    1875ms   205358 items  109524 items/s
write_2    1859ms   215713 items  116037 items/s
--+------+-----+------
delete_*    1853ms   656222 items  354140 items/s
delete_0    1854ms   185805 items  100218 items/s
delete_1    1853ms   234311 items  126449 items/s
delete_2    1853ms   236106 items  127418 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1477ms   10000000 items  6770480 items/s
read_0    1480ms   4956391 items  3348912 items/s
read_1    1475ms   5043609 items  3419395 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1706ms   10000000 items  5861664 items/s
read_0    1719ms   3353321 items  1950739 items/s
read_1    1712ms   3329052 items  1944539 items/s
read_2    1687ms   3317627 items  1966583 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1904ms   10000000 items  5252100 items/s
read_0    1919ms   2438467 items  1270696 items/s
read_1    1918ms   2512520 items  1309968 items/s
read_2    1892ms   2499634 items  1321159 items/s
read_3    1887ms   2549379 items  1351022 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1917ms   10000000 items  5216484 items/s
read_0    1971ms   1675825 items  850240 items/s
read_1    1971ms   1881607 items  954645 items/s
read_2    1935ms   1822041 items  941623 items/s
read_3    1896ms   1577096 items  831801 items/s
read_4    1866ms   1495016 items  801187 items/s
read_5    1864ms   1548415 items  830694 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1842ms   10000000 items  5428881 items/s
read_0    1908ms   894535 items  468833 items/s
read_1    1908ms   1195727 items  626691 items/s
read_2    1875ms   966017 items  515209 items/s
read_3    1853ms   1075405 items  580358 items/s
read_4    1833ms   949024 items  517743 items/s
read_5    1815ms   960360 items  529123 items/s
read_6    1813ms   1047706 items  577885 items/s
read_7    1811ms   1003651 items  554197 items/s
read_8    1803ms   949584 items  526668 items/s
read_9    1801ms   957991 items  531921 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1855ms   9990094 items  5385495 items/s
read_0    1933ms   989543 items  511920 items/s
read_1    1933ms   998571 items  516591 items/s
read_2    1892ms   1155458 items  610707 items/s
read_3    1877ms   1086141 items  578657 items/s
read_4    1847ms   911123 items  493298 items/s
read_5    1821ms   844258 items  463623 items/s
read_6    1817ms   1012914 items  557465 items/s
read_7    1817ms   1059704 items  583216 items/s
read_8    1808ms   1066734 items  590007 items/s
read_9    1807ms   865648 items  479052 items/s
--+------+-----+------
write_*    1799ms   9906 items  5506 items/s
write_0    1799ms   9906 items  5506 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    1853ms   9993877 items  5393349 items/s
read_0    1978ms   666069 items  336738 items/s
read_1    1978ms   563541 items  284904 items/s
read_2    1970ms   588009 items  298481 items/s
read_3    1938ms   573743 items  296049 items/s
read_4    1916ms   535162 items  279312 items/s
read_5    1915ms   514100 items  268459 items/s
read_6    1898ms   514682 items  271170 items/s
read_7    1890ms   647994 items  342853 items/s
read_8    1876ms   523033 items  278802 items/s
read_9    1856ms   503310 items  271179 items/s
read_10    1850ms   415536 items  224614 items/s
read_11    1829ms   608040 items  332443 items/s
read_12    1808ms   459535 items  254167 items/s
read_13    1793ms   421532 items  235098 items/s
read_14    1786ms   353351 items  197844 items/s
read_15    1779ms   422878 items  237705 items/s
read_16    1764ms   400025 items  226771 items/s
read_17    1755ms   447114 items  254765 items/s
read_18    1751ms   434073 items  247900 items/s
read_19    1739ms   402150 items  231253 items/s
--+------+-----+------
write_*    1732ms   6123 items  3535 items/s
write_0    1732ms   6123 items  3535 items/s
--+------+-----+--------+--------+-------+---------+----
read_*    2195ms   9857949 items  4491092 items/s
read_0    2318ms   613925 items  264851 items/s
read_1    2312ms   682568 items  295228 items/s
read_2    2307ms   596734 items  258662 items/s
read_3    2233ms   558797 items  250244 items/s
read_4    2220ms   490325 items  220867 items/s
read_5    2210ms   492380 items  222796 items/s
read_6    2210ms   477611 items  216113 items/s
read_7    2197ms   598761 items  272535 items/s
read_8    2194ms   444040 items  202388 items/s
read_9    2190ms   446380 items  203826 items/s
read_10    2184ms   603915 items  276517 items/s
read_11    2181ms   432033 items  198089 items/s
read_12    2171ms   448571 items  206619 items/s
read_13    2162ms   467701 items  216327 items/s
read_14    2154ms   410388 items  190523 items/s
read_15    2153ms   343920 items  159739 items/s
read_16    2139ms   475204 items  222161 items/s
read_17    2132ms   401630 items  188381 items/s
read_18    2124ms   383813 items  180702 items/s
read_19    2114ms   489253 items  231434 items/s
--+------+-----+------
delete_*    2006ms   67925 items  33860 items/s
delete_0    2076ms   6605 items  3181 items/s
delete_1    2074ms   8156 items  3932 items/s
delete_2    2068ms   6018 items  2910 items/s
delete_3    2068ms   5191 items  2510 items/s
delete_4    1973ms   7813 items  3959 items/s
delete_5    1971ms   6973 items  3537 items/s
delete_6    1961ms   5233 items  2668 items/s
delete_7    1958ms   5984 items  3056 items/s
delete_8    1957ms   4560 items  2330 items/s
delete_9    1957ms   11392 items  5821 items/s
--+------+-----+------
write_*    2089ms   74126 items  35483 items/s
write_0    2096ms   8387 items  4001 items/s
write_1    2094ms   7390 items  3529 items/s
write_2    2094ms   5803 items  2771 items/s
write_3    2094ms   7582 items  3620 items/s
write_4    2094ms   4841 items  2311 items/s
write_5    2094ms   9001 items  4298 items/s
write_6    2091ms   5618 items  2686 items/s
write_7    2083ms   9669 items  4641 items/s
write_8    2081ms   9525 items  4577 items/s
write_9    2078ms   6310 items  3036 items/s
--+------+-----+--------+--------+-------+---------+----


   相比ReaderWriterLock,并非想象中那么出众,不过读写同时进行时效率很不错。同时每个线程的执行时间都相差无几。
   不像其他部分线程基本没有调度时间,ReaderWriterLockSlim在平等线程调度上是做得最好了。
   CPU占用相比ReaderWriterLock稍好,减少系统调用。

 

总结:

综合性能上Level-Lock 最优,Lock-Free次之,但再CPU资源利用率上Lock-Free以绝对优势最高。 (可以实现一个Level-Lock-Free,最大的发挥多核CPU的价值,不过一般用不上)。

读写锁:ReaderWriterLock,ReaderWriterLockSlim。ReaderWriterLockSlim 在各个方面(尤其是并发读写情况下远好友ReaderWriterLock) 优于ReaderWriterLock。不过只读模式下两者都能发挥相同的高效性,使用时注意尽量放在写少的业务中。ReaderWriterLockSlim 另外一个很好的地方就是系统的平均调度上非常好,避免部分线程完全没有运行的机会。

 

一般要求不高的情况下使用lock就行,高并发情况下Level-Lock是非常好的选择。

Lock-Free不像lock那么好实现,限制比较多,如果用.Net 4不烦多用用System.Collection.Concurrent下的集合。

 

 

附上测试代码:

 static class Program
    {
        static void Main()
        {
           // DictMTTest> test = new DictMTTest>(10000000, 10000000);
            // DictMTTest> test = new DictMTTest>(10000000, 10000000);
           // DictMTTest> test = new DictMTTest>(10000000, 10000000);
            // DictMTTest> test = new DictMTTest>(10000000, 10000000);
            //DictMTTest test = new DictMTTest(10000000, 10000000); 
             DictMTTestint, int>> test = new DictMTTestint, int>>(10000000, 10000000);
            test.RunTest(1, 0, 0);
            test.RunTest(0, 1, 0);
            test.RunTest(0, 0, 1);
 
            test.RunTest(1, 1, 1);
            test.RunTest(2, 2, 2);
            test.RunTest(3, 3, 3);
 
            test.RunTest(2, 0, 0);
            test.RunTest(3, 0, 0);
            test.RunTest(4, 0, 0);
            test.RunTest(6, 0, 0);
            test.RunTest(10, 0, 0);
            test.RunTest(10, 1, 0);
            test.RunTest(20, 1, 0);
            test.RunTest(20, 10, 10);
            Console.Read();
        }
    }
 
    public class DictMTTest where T : IDictionary<int, int>, new()
    {
        private T _dict;
        private int _perpare;
        private int _items;
        private Random _rand;
        private MultiThreadTest _dictTest;
 
        public DictMTTest(int perpare, int items)
        {
            _perpare = perpare;
            _items = items;
        }
 
        private void Perpare()
        {
            _dict = new T();
            _rand = new Random(DateTime.Now.Millisecond);
            for (int i = 0; i < _perpare; i++)
            {
                int v = _rand.Next(0, _perpare * 2);
                _dict[v] = v;
            }
 
            _dictTest = new MultiThreadTest();
        }
 
        private void Clear()
        {
            _dictTest = null;
            _dict = default(T);
            _rand = null;
            GC.Collect();
            GC.Collect();
            GC.Collect();
        }
 
        public void RunTest(int read, int write, int delete)
        {
            Perpare();
 
            _dictTest.AddAction(read, "read", new Action(() =>
            {
                int r = _rand.Next(0, _perpare * 2);
                int temp;
                    _dict.TryGetValue(r,out temp);
            }));
 
            _dictTest.AddAction(write, "write", new Action(() =>
            {
                int r = _rand.Next(0, _perpare * 2);
                _dict[r] = r;
            }));
 
            _dictTest.AddAction(delete, "delete", new Action(() =>
            {
                int r = _rand.Next(0, _perpare * 2);
                _dict.Remove(r);
            }));
 
            _dictTest.Run(_items);
            Clear();
        }
    }
 
    public class MultiThreadTest
    {
        private Liststring, Action, int>> _actions;
        private Liststring, int, int, int>> _results;
        private List _threads;
        private bool _start = false;
        private ManualResetEventSlim _wait;
 
        public MultiThreadTest()
        {
            _wait = new ManualResetEventSlim();
            _actions = new Liststring, Action, int>>();
            _results = new Liststring, int, int, int>>();
            _threads = new List();
        }
 
        public void AddAction(int threadCount, string actionName, Action action)
        {
            if (_start)
                throw new NotSupportedException();
 
            _actions.Add(new Tuple<string, Action, int>(actionName, action, threadCount));
        }
 
        public void Run(int iteration)
        {
            PerfmonProc.Iteration = iteration;
 
            foreach (var t in _actions)
            {
                for (int i = 0; i < t.Item3; i++)
                {
                    PerfmonProc proc = new PerfmonProc(t.Item1, i, t.Item2);
                    proc.OnCompleted = Complete;
 
                    Thread th = new Thread(proc.Proc);
                    th.IsBackground = true;
                    _threads.Add(th);
                }
            }
            _threads.ForEach((item) => { item.Start(); });
            _wait.Wait();
        }
 
        private void Complete(Tuple<string, int, int, int> tuple)
        {
            lock (((ICollection)_results).SyncRoot)
            {
                _results.Add(tuple);
                if (_results.Count == _threads.Count)
                {
                    Dictionary<string, Liststring, int, int, int>>> dict =
                        new Dictionary<string, Liststring, int, int, int>>>();
                    foreach (Tuple<string, int, int, int> r in _results)
                    {
                        Liststring, int, int, int>> list;
                        if (!dict.TryGetValue(r.Item1, out list))
                        {
                            list = new Liststring, int, int, int>>();
                            dict.Add(r.Item1, list);
                        }
                        list.Add(r);
                    }
                    int count = dict.Count;
                    int i= 0;
                    foreach (var kv in dict)
                    {
                        ConsoleOut(kv.Key + "_*", -1, (int)kv.Value.Average(p => p.Item3)
                            , kv.Value.Sum(p => p.Item4));
 
                        var value = kv.Value.OrderBy(p => p.Item2);
                        foreach (var v in value)
                        {
                            ConsoleOut(v.Item1, v.Item2, v.Item3, v.Item4);
                        }
                        i++;
                        if (i < count)
                            Console.WriteLine("--+------+-----+------");
                    }
                    Console.WriteLine("--+------+-----+--------+--------+-------+---------+----");
 
                    _wait.Set();
                }
            }
        }
 
        private void ConsoleOut(string name, int index, int ms, int items)
        {
            ConsoleColor defColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("{0}", index < 0 ? name : name + "_" + index);
            Console.ForegroundColor = defColor;
            Console.WriteLine("    {0}ms   {1} items  {2} items/s"
                , ms, items,ms<=0?"oooo" : ((long)items * 1000 / (long)ms).ToString());
        }
 
        class PerfmonProc
        {
            public static int Iteration;
            private static int _allDoCount;
 
            private int _threadIndex;
            private string _actionName;
            private int _doCount;
            private Action _action;
            public Actionstring, int, int, int>> OnCompleted;
 
            public PerfmonProc(string actionName, int threadIndex, Action action)
            {
                _threadIndex = threadIndex;
                _actionName = actionName;
                _action = action;
                _allDoCount = 0;
            }
 
            public void Proc(object ob)
            {
                long startTime = Stopwatch.GetTimestamp();
                ulong startCycles = CycleTime.Thread();
 
                while (Interlocked.Increment(ref _allDoCount) <= Iteration)
                {
                    Interlocked.Increment(ref _doCount);
                    _action();
                }
 
                UInt64 elapsedCycles = CycleTime.Thread() - startCycles;
                Int64 elapsedTime = Stopwatch.GetTimestamp() - startTime;
                Int64 milliseconds = (elapsedTime * 1000) / Stopwatch.Frequency;
 
                Tuple<string, int, int, int> tuple = new Tuple<string, int, int, int>
                    (_actionName, _threadIndex, (int)milliseconds, _doCount);
                if (OnCompleted != null)
                    OnCompleted(tuple);
            }
        }
    }

 

 
 

至于集合的实现比较简单,我也没做完整的实现,就不贴了。

~~~~~~~~~~~~~~

写篇博客真不容易啊。。。。

转载于:https://www.cnblogs.com/lulu/archive/2012/03/17/2403872.html

你可能感兴趣的:(多线程下Lock、Level-Lock、Lock-Free、ReaderWriterLock、ReaderWriterLockSlim性能比较)